题目链接:http://acm.hust.edu.cn/vjudge/contest/124435#problem/J
密码:acm
Sample Input 3 6 5 NEESWE WWWESS SNWWWW 4 5 1 SESWE EESNW NWEEN EWSEN 0 0 0 Sample Output 10 step(s) to exit 3 step(s) before a loop of 8 step(s)
分析:
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <stdlib.h> 6 #include<queue> 7 #include<math.h> 8 using namespace std; 9 10 #define N 1001 11 char s[N][N]; 12 int a[N][N],b[N][N]; 13 14 int main() 15 { 16 int n,m,c,ans; 17 while(scanf("%d%d%d",&n,&m,&c),n+m+c) 18 { 19 memset(a,0,sizeof(a)); 20 memset(b,0,sizeof(b)); 21 ans=0; 22 23 for(int i=1;i<=n;i++) 24 scanf("%s",s[i]); 25 26 int i=1; 27 int j=c-1;///字符串里面从零开始记录的,老是忘~~~~(>_<)~~~~ 28 while(1) 29 { 30 b[i][j]=1; 31 if(s[i][j]=='N') 32 i=i-1; 33 else if(s[i][j]=='S') 34 i=i+1; 35 else if(s[i][j]=='E') 36 j=j+1; 37 else 38 j=j-1; 39 ans++; 40 if(b[i][j]) 41 { 42 printf("%d step(s) before a loop of %d step(s)\n",a[i][j],ans-a[i][j]); 43 break; 44 } 45 a[i][j]=ans; 46 47 if(i==0||j==-1||i==n+1||j==m) 48 { 49 printf("%d step(s) to exit\n",ans); 50 break; 51 } 52 } 53 } 54 return 0; 55 }