[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 (< 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’.
The top left of the grid is indexed at (0,0) and the bottom right is indexed at (N-1,N-1)
Output format
Print out all the moves you take to rescue the princess in one go. Moves must be separated by ‘\n’ a newline. The valid outputs are LEFT or RIGHT or UP or DOWN.
Sample input
3
---
-m-
p--
Sample output
DOWN
LEFT
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.
The above sample input is just to help you understand the format. The princess (‘p’) can be in any one of the four corners
Scoring
Your score is calculated as follows : (NxN - moves made to rescue the princess)/10, where N is the size of the grid (3x3 in the sample testcase).
[Analysis]
宽度优先搜索[Solution]
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/* definition for Pos */
struct Pos{
int x, y;
vector<string> path;
Pos(int _x, int _y) : x(_x), y(_y){}
};
/* definition for directions and direction names */
Pos direction[4] = {Pos(-1, 0), Pos(1, 0), Pos(0, -1), Pos(0, 1)};
string dir_name[4] = {"UP", "DOWN", "LEFT", "RIGHT"};
/* Head ends here */
void displayPathtoPrincess(int n, vector <string> grid){
//your logic here
Pos me(-1, -1);
Pos princess(-1, -1);
bool visited[n][n];
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(grid[i][j] == 'm'){
me.x = i;
me.y = j;
}
else if(grid[i][j] == 'p'){
princess.x = i;
princess.y = j;
}
visited[i][j] = false;
}
}
// BFS
queue<Pos> myQueue;
myQueue.push(me);
visited[me.x][me.y] = true;
while(!myQueue.empty()){
Pos pos = myQueue.front();
myQueue.pop();
// reach the princess
if(pos.x == princess.x && pos.y == princess.y){
for(int i = 0; i < pos.path.size(); ++i){
cout << pos.path[i] << endl;
}
break;
}
// move in 4 directions
for(int i = 0; i < 4; i++){
Pos next(pos.x + direction[i].x, pos.y + direction[i].y);
// invalid position
if(next.x < 0 || next.y < 0 || next.x >= n || next.y >= n || visited[next.x][next.y] == true)continue;
visited[next.x][next.y] = true;
// update path
next.path.insert(next.path.end(), pos.path.begin(), pos.path.end());
next.path.push_back(dir_name[i]);
// insert
myQueue.push(next);
}
}
}
/* Tail starts here */
int main(){
int n;
string line;
vector<string> grid;
// input
cin >> n;
for(int i = 0; i < n; ++i){
cin >> line;
grid.push_back(line);
}
// solution
displayPathtoPrincess(n, grid);
return 0;
}