[Interview Street] Track 1

来源:互联网 发布:音频测试软件 编辑:程序博客网 时间:2024/05/22 16:37

[Problem]

In this version of “Bot saves princess”, Princess Peach and bot’s position are randomly set. Can you save the princess?

Task

Complete the function nextMove which takes in 4 parameters - an integern, an integer x and y indicating the position of the bot and the character arraygrid and output only the next move the bot makes to rescue the princess.

Input Format

The first line of the input is N (<100), the size of the board (NxN). The second line of the input contains two space seperated integers, which is the position of the bot in row-column format. The Board is indexed at (0,0) on the top left and (N-1,N-1) on the bottom right. The x co-ordinate increases from top to bottom and y co-ordinate increases left to right.N lines follow each line containing N characters which is the grid data.

The position of the princess is indicated by the character ‘p’ and the position of the bot is indicated by the character ‘m’ and each cell is denoted by ‘-‘ (ascii value 45).

Output Format

Output only the next move you take to rescue the princess. Valid moves are LEFT or RIGHT or UP or DOWN

Sample Input

52 3----------p--m-----------

Sample Output

LEFT

Scoring
Your score for every testcase would be (NxN minus number of moves made to rescue the princess)/10 where N is the size of the grid (5x5 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 nextMove(int n, int x, int y, vector <string> grid){//your logic herePos me(x, y);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] == 'p'){princess.x = i;princess.y = j;}visited[i][j] = false;}}// BFSqueue<Pos> myQueue;myQueue.push(me);visited[me.x][me.y] = true;while(!myQueue.empty()){Pos pos = myQueue.front();myQueue.pop();// reach the princessif(pos.x == princess.x && pos.y == princess.y){if(pos.path.size() > 0){cout << pos.path[0];}cout << endl;break;}// move in 4 directionsfor(int i = 0; i < 4; i++){Pos next(pos.x + direction[i].x, pos.y + direction[i].y);// invalid positionif(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 pathnext.path.insert(next.path.end(), pos.path.begin(), pos.path.end());next.path.push_back(dir_name[i]);// insertmyQueue.push(next);}}}/* Tail starts here */int main(){int n, x, y;string line;vector<string> grid;// inputcin >> n >> x >> y;for(int i = 0; i < n; ++i){cin >> line;grid.push_back(line);}// solutionnextMove(n, x, y, grid);return 0;}
说明:版权所有,转载请注明出处。Coder007的博客
原创粉丝点击