[go: up one dir, main page]

0% found this document useful (0 votes)
45 views5 pages

19102A0056 - AI - Bot Save Princess

This document describes an experiment to program a bot to save a princess trapped in a grid. The bot starts in the center and the princess is in one of the four corners. The program takes the grid size and grid as input, determines which corner the princess is in, and outputs the sequence of moves (UP, DOWN, LEFT, RIGHT) needed to reach her as few moves as possible. It implements a function that analyzes the grid and outputs the optimal path. The score is calculated as the grid size minus the number of moves, divided by 10.

Uploaded by

Pratik Rathod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views5 pages

19102A0056 - AI - Bot Save Princess

This document describes an experiment to program a bot to save a princess trapped in a grid. The bot starts in the center and the princess is in one of the four corners. The program takes the grid size and grid as input, determines which corner the princess is in, and outputs the sequence of moves (UP, DOWN, LEFT, RIGHT) needed to reach her as few moves as possible. It implements a function that analyzes the grid and outputs the optimal path. The score is calculated as the grid size minus the number of moves, divided by 10.

Uploaded by

Pratik Rathod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

6

Department of Computer Exp.6


`
Semester VI
Subject Artificial Intelligence
Subject Professor In- Prof. Sachin Deshpande
charge
Assisting Teachers Prof. Sachin Deshpande
Laboratory

Student Name Pratik Rathod


Roll Number 19102A0056
Grade and Subject
Teacher’s Signature

Experiment Number 6

Experiment Title Implementation of Bot save Princess.


Resources / Hardware: Software:
Apparatus Required Computer System C
Theory

This is the problem :- Princess Peach is trapped in one of the


four corners of a square grid. You are in the center of the grid and
can move one step at a time in any of the four directions. Can you
rescue the princess?

Input format :- The first line contains an odd integer N (3 <= N <
100) denoting the size of the grid. This is followed by an NxN grid.
Each cell is denoted by ‘ - ’ (ASCII value: 45). The bot position is
denoted by ‘m’ and the princess position is denoted by ‘ p ’.

Output format :- Print out the moves you will take to rescue the
princess in one go. The moves must be separated by ‘\n’, a
newline.(The valid moves are LEFT or RIGHT or UP or DOWN.)

Task :- Complete the function displayPathtoPrincess which takes


in two parameters — the integer N and the character array grid.
The grid will be formatted exactly as you see it in the input, so for
the sample input, the princess is at grid[2][0]. The function shall
output moves (LEFT, RIGHT, UP, or DOWN) on consecutive lines
to rescue/reach the princess. The goal is to reach the princess in
as few moves as possible.
6

Department of Computer Exp.6


`
Scoring :- Your score is calculated as follows : (NxN — number of

moves made to rescue the princess)/10, where N is the size of the

grid (3x3 in the sample test case).


6

Department of Computer Exp.6


Program Code #include <stdio.h> `
#include <string.h>
#include <math.h>
void displayPathtoPrincess(int n, char grid[101][101]){
int found=0;
if(grid[0][0]=='p') //top-left position
{ found=1;
}
else if(grid[0][n-1]=='p'){ //top-right position
found=2;
}
else if(grid[n-1][0]=='p') //bottom-left position
{ found=3;
}
else //bottom-right position
{ found=4;
}
if(found==1)
{ for(int i=n/2; i>0; i--)
printf("UP\n");
for(int j=n/2; j>0; j--)
printf("LEFT\n");

}
else if(found==2)
{ for(int i=n/2; i>0; i--)
printf("UP\n");
for(int j=n/2; j>0; j--)
printf("RIGHT\n");

}
else if(found==3)
{ for(int i=n/2; i>0; i--)
printf("DOWN\n");
for(int j=n/2; j>0; j--)
printf("LEFT\n");

}
else if(found==4)
{ for(int i=n/2; i>0; i--)
printf("DOWN\n");
for(int j=n/2; j>0; j--)
printf("RIGHT\n");

}
int main(void) {

int m; //grid size


6

Department of Computer Exp.6


scanf("%d", &m); `
char grid[101][101]={}; //grid
char line[101];

for(int i=0; i<m; i++) {


scanf("%s", line);
strcpy(grid[i], line);
}
displayPathtoPrincess(m,grid);
return 0;
}
6

Department of Computer Exp.6


Output `

Conclusion In this experiment we learnt bot save princess in AI.

You might also like