很简单的模拟题
代码如下
#include <stdio.h>
#include <string.h>
char map[11][11];
int used[11][11];
int main()
{
int col,row,stpos,i,j,posx,posy,k,lcnt,scnt;
char dir;
while(scanf("%d%d%d",&row,&col,&stpos)!=EOF)
{
if(row==0&&col==0&&stpos==0) break;
for(i=1;i<=row;i++)
{
scanf("%s",map[i]);
}
memset(used,0,sizeof(used));
used[1][stpos-1]=1;
posx=1;posy=stpos-1;
dir=map[posx][posy];
k=2;scnt=1;lcnt=0;
while(posx>=1&&posx<=row&&posy>=0&&posy<col)
{
if(dir=='N') posx--;
if(dir=='S') posx++;
if(dir=='E') posy++;
if(dir=='W') posy--;
if(used[posx][posy]) {lcnt=k-used[posx][posy];scnt-=lcnt;break;}
else
{
used[posx][posy]=k;
k++;
dir=map[posx][posy];
scnt++;
}
}
if(lcnt)
printf("%d step(s) before a loop of %d step(s)\n",scnt,lcnt);
else
printf("%d step(s) to exit\n",scnt-1);
}
}