http://acm.hdu.edu.cn/showproblem.php?pid=1035
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
Sample Output
10 step(s) to exit 3 step(s) before a loop of 8 step(s)
题目大意: 就是给一张地图, 根据对应的字母走就行了, 如果走出边界就算走出了迷宫, 输出所用的步数; 如果进入了死循环, 那么先输出进入死循环前的步数再输出循环的步数。(注意格式)第一行分别给出迷宫的行数、列数以及起点在第几列(起点默认是在第一行的)。
思路: dfs模拟就好了,本来挺简单一道题,WA到我怀疑人生。(至今不知道改了什么才AC的 最后是把输出语句复制粘贴了一下 然后过了 惊了)我们用vis[i][j]表示走到地图i、j时候所需要的步数,初始化为-1且另起点为0,那么就很好判断循环的情况了。其他的也没什么好说的,就是普通的dfs~
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<algorithm>
#include<vector>
#define INF 0x3f3f3f3f
using namespace std;
char s[15][15];
int vis[15][15];
int n,m,t;
void dfs(int x,int y);
int main()
{
while(~scanf("%d %d",&n,&m)&&n+m)
{
scanf("%d",&t);
memset(vis,-1,sizeof(vis));
for(int i=1;i<=n;i++)
scanf("%s",s[i]+1);
vis[1][t]=0;
dfs(1,t);
}
return 0;
}
void dfs(int x,int y)
{
int dx,dy;
if(s[x][y]=='N')
dx=x-1,dy=y;
else if(s[x][y]=='W')
dx=x,dy=y-1;
else if(s[x][y]=='E')
dx=x,dy=y+1;
else if(s[x][y]=='S')
dx=x+1,dy=y;
if(dx<=0||dx>n||dy>m||dy<=0)
{
printf("%d step(s) to exit\n",vis[x][y]+1);
return ;
}
if(vis[dx][dy]>=0)//已经被走过了
{
printf("%d step(s) before a loop of %d step(s)\n",vis[dx][dy],vis[x][y]+1-vis[dx][dy]);
return ;
}
vis[dx][dy]=vis[x][y]+1;
dfs(dx,dy);
return ;
}