题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1035
表示这个题目很简单,基本上没什么陷井,至于我wa了两次是因为起点没设置成1,也就是把回到起点的情况多加了1,有点不应该
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define maxn 20
int map[maxn][maxn];
char rec[maxn][maxn];
int find_ans(int t)
{
int i=1,j=t,step=1;
if(rec[i][j]==0)
{
printf("%d step(s) to exit\n",0);
return 0;
}
map[i][j]=1;
while(1)
{
switch(rec[i][j])
{
case 'N':i--;break;
case 'S':i++;break;
case 'E':j++;break;
case 'W':j--;break;
}
step++;
if(map[i][j]>0)
{
printf("%d step(s) before a loop of %d step(s)\n",map[i][j]-1,step-map[i][j]);
return 0;
}
if(rec[i][j]==0)
{
printf("%d step(s) to exit\n",step-1);
return 0;
}
else
map[i][j]=step;
}
return 0;
}
int main()
{
int n,m,t,i;
while(scanf("%d%d",&n,&m))
{
if(n==0 && m==0)
return 0;
scanf("%d",&t);
memset(map,0,sizeof(map));
memset(rec,0,sizeof(rec));
for(i=1;i<=n;i++)
scanf("%s",rec[i]+1);
find_ans(t);
}
return 0;
}