#include <stdio.h>#include <stdlib.h>#include <string.h>#define N 1000typedef struct{ int to,next,cap;} E;E edge[N*N];int V[N];int p[N];int M;void insert(int from,int to){ edge[M].to=to; edge[M].cap=1; edge[M].next=V[from]; V[from]=M++;}int main(){ int n,m,s,i,j; char str[N][N],c; while(scanf("%d%d",&n,&m)) { if(n==0&&m==0)break; scanf("%dn",&s); M=1; memset(V,-1,sizeof(V)); for(i=1; i<=n; i++) { for(j=1; j<=m ; j++) { scanf("%c",&str[i][j]); } getchar(); } i=1; while(1) { c=str[i][s]; if(V[i]==-1) { insert(i,s); } else { for(j=V[i]; j+1; j=edge[j].next) if(edge[j].to==s) { break; } if(!(j+1)) insert(i,s); else break; } switch(c) { case 'N': i-=1; break; case 'W': s-=1; break; case 'E': s+=1; break; case 'S': i+=1; break; } if(s>m||i>n||!i||!s)break; } if(!i||!s||i>n||s>m) printf("%d step(s) to exitn",M-1); else { printf("%d step(s) before a loop of %d step(s)n",j-1,M-j); } } return 0;}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)