A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are
N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)
For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.
Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.
You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.
Input
There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.
Output
For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word “step” is always immediately followed by “(s)” whether or not the number before it is 1.
Sample Input
3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
题目描述的例子其实就是这两个样例。还不会把图片写进来,见谅。
题目描述的就是一个机器人可以走上下左右(分别对应NSWE)。然后给你一个起点判断机器人是否能够走出迷宫,走出输出总共走了多少步,如果走不出,那么机器人一定是在里边走了重复的路,输出在重复的节点之前机器人走的路步数和之后再次遇到重复节点机器人走的步数。
这个题目其实就是BFS然后根据NSWE分别判断就好了。
/*下一次到达的点位于矩阵外*/
#include<stdio.h>
#include<string.h>
const int maxn = 1000 + 10;
char str[maxn][maxn];
int vis[maxn][maxn];
int n, m, f;
struct node{
int x, y, step;
};
void BFS(int x1, int y1){
memset(vis, 0, sizeof(vis));
node e1, e2;
e1.x = x1, e1.y = y1, e1.step = 0;
vis[x1][y1] = 1;
int ans = -1, s = 0;
while (true){
if (str[e1.x][e1.y] == 'N') {
e2.x = e1.x - 1; e2.y = e1.y;
}
if (str[e1.x][e1.y] == 'S') {
e2.x = e1.x + 1; e2.y = e1.y;
}
if (str[e1.x][e1.y] == 'E') {
e2.y = e1.y + 1; e2.x = e1.x;
}
if (str[e1.x][e1.y] == 'W') {
e2.y = e1.y - 1; e2.x = e1.x;
}
e1.step ++;
if (e2.x < 0 || e2.y < 0 || e2.x >= n || e2.y >= m){
ans = e1.step;
break;
}
if (vis[e2.x][e2.y]){
int x = x1; int y = y1;
while (true){
if (x == e2.x && y == e2.y) break;
if (str[x][y] == 'N') x--;
else if (str[x][y] == 'S') x++;
else if (str[x][y] == 'E') y++;
else if (str[x][y] == 'W') y--;
s++;
}
break;
}
vis[e2.x][e2.y] = 1;
e1.x = e2.x, e1.y = e2.y;
}
if (ans == -1)
printf ("%d step(s) before a loop of %d step(s)\n", s, e1.step - s);
else printf ("%d step(s) to exit\n", ans);
}
int main()
{
while (scanf ("%d %d %d", &n, &m, &f)){
if (n == 0 && m == 0 && f == 0) break;
for (int i = 0; i < n; i++) scanf ("%s", str[i]);
int x1 = 0;
int y1 = f-1;
BFS(x1, y1);
}
return 0;
}