[Interview Street] Track 1 - Bot saves princess - 2

[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 integer n, an integer x and y indicating the position of the bot and the character array grid 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

5
2 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 here
	Pos 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;
		}
	}

	// 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){
			if(pos.path.size() > 0){
				cout << pos.path[0];
			}
			cout << 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, x, y;
	string line;
	vector<string> grid;

	// input
	cin >> n >> x >> y;
	for(int i = 0; i < n; ++i){
		cin >> line;
		grid.push_back(line);
	}

	// solution
	nextMove(n, x, y, grid);
	return 0;
}

说明:版权所有,转载请注明出处。 Coder007的博客
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值