#include <stdio.h>
#include <string.h>
int r,c,n,record[11][11],f[11][11],step;
char s[11][11];
void dfs(int x,int y)
{
int nx,ny;
step++;
if(x<0||x>=r||y<0||y>=c){
printf("%d step(s) to exit\n",step);
return;
}
if(f[x][y]){
printf("%d step(s) before a loop of %d step(s)\n",record[x][y],step-record[x][y]);
return;
}
record[x][y]=step;
f[x][y]=1;
if(s[x][y]=='N'){
nx=x-1;
ny=y;
}else if(s[x][y]=='S')
{
nx=x+1;
ny=y;
}else if(s[x][y]=='W')
{
nx=x;
ny=y-1;
}else
{
nx=x;
ny=y+1;
}
dfs(nx,ny);
}
int main()
{
//int r,c,n,s[11][11],record[11][11],f[11][11];
int i;
while(1){
scanf("%d%d",&r,&c);
if(r==0&&c==0)break;
scanf("%d",&n);
getchar();
for(i=0;i<=r-1;i++)gets(s[i]);
step=-1;
memset(f,0,sizeof(f));
dfs(0,n-1);
}
return 0;
}
这道题的意思就是按照途中标的方向移动,如果不能走出去就输出绕圈用了多少步,绕圈之前走了几步;能走出去就输出总共用了多少步
用数组储存输入的图,每挪动一步,就判断是否走出,并记录所走的步数。