题目传送门
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char Map[12][12];
int vis[12][12];
int ro,co,lo;
int cnt;
void dfs(int a,int b,int step);
int main()
{
while(scanf("%d%d%d",&ro,&co,&lo)!=EOF)
{
if(ro==0&&co==0&&lo==0)
break;
cnt=0;
memset(vis,0,sizeof(vis));
for(int i=0;i<ro;i++)
scanf("%s",Map[i]);
dfs(0,lo-1,0);
}
return 0;
}
void dfs(int a,int b,int step)
{
if(a<0||a>=ro||b<0||b>=co)
{
printf("%d step(s) to exit\n",step);
return ;
}
if(vis[a][b])
{
printf("%d step(s) before a loop of %d step(s)\n",vis[a][b]-1,step-vis[a][b]+1);
return;
}
cnt++;
vis[a][b]=cnt;
if(Map[a][b]=='N')
dfs(a-1,b,step+1);
else if(Map[a][b]=='S')
dfs(a+1,b,step+1);
else if(Map[a][b]=='E')
dfs(a,b+1,step+1);
else if(Map[a][b]=='W')
dfs(a,b-1,step+1);
return ;
}