hdu1035

#include<stdio.h>
#include<string.h>
int step,n,m;
int a[1010][1010];
char map[11][11];
void DFS(int x,int y)
{
while(x>=0&&y>=0&&x<n&&y<m&&map[x][y]!='O')
{
if(map[x][y]=='N')
{
map[x][y]='O';
a[x][y]=++step;
x--;
}
else if(map[x][y]=='S')
{
map[x][y]='O';
a[x][y]=++step;
x++;
}
else if(map[x][y]=='E')
{
map[x][y]='O';
a[x][y]=++step;
y++;
}
else if(map[x][y]=='W')
{
map[x][y]='O';
a[x][y]=++step;
y--;
}
}
if(map[x][y]=='O')
printf("%d step(s) before a loop of %d step(s) ",a[x][y]-1,step+1-a[x][y]);
else
printf("%d step(s) to exit ",step);
}
int main()
{
int i,k,x,y;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
if(n==0||m==0)
break;
for(i=0;i<n;i++)
scanf("%s",map[i]);
x=0;
y=k-1;
step=0;
DFS(x,y);
}
return 0;
}

原文地址:https://www.cnblogs.com/wangkun1993/p/6286391.html