19102A0056 - AI - Bot Save Princess

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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