找了很久没找出错误
题目要求输入0 0 0时结束
附我的代码给你参考:
#include
#include
int m,n,s,step,u[20][20],c;
char t[20][20];
void dfs(int x,int y)
{
if(x<0||y<0||x>=m||y>=n)
return;
if(u[x][y])
{
c=step-u[x][y]+1;
step=u[x][y]-1;
return;
}
step++;
u[x][y]=step;
if(t[x][y]=='W')
dfs(x,y-1);
if(t[x][y]=='E')
dfs(x,y+1);
if(t[x][y]=='N')
dfs(x-1,y);
if(t[x][y]=='S')
dfs(x+1,y);
}
int main()
{
while(scanf("%d%d%d",&m,&n,&s),m+n+s)
{
c=step=0;
memset(u,0,sizeof(u));
for(int i=0;i
dfs(0,s-1);
if(c)
printf("%d step(s) before a loop of %d step(s)\n",step,c);
else
printf("%d step(s) to exit\n",step);
}
}