标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7693 Accepted Submission(s):
3513
1 #include <stdio.h> 2 #include <string.h> 3 4 const int maxn = 1000 + 5; 5 6 char map[maxn][maxn]; 7 int m, n, x, y, b; 8 int dir[5][2] = {{0,1},{1,0},{0,-1},{-1,0}}, vis[maxn][maxn]; 9 10 int check(char c) 11 { 12 if(c == ‘E‘) 13 return 0; 14 if(c == ‘S‘) 15 return 1; 16 if(c == ‘W‘) 17 return 2; 18 if(c == ‘N‘) 19 return 3; 20 } 21 22 int main() 23 { 24 while(scanf("%d%d%d", &m, &n, &b) != EOF && m && n) 25 { 26 memset(vis, 0, sizeof(vis)); 27 int cnt = 1, t, flow = 0, tt; 28 for(int i = 0; i < m; i++) 29 { 30 scanf("%s", map[i]); 31 } 32 x = 0; y = b-1; 33 while(1) 34 { 35 if(x < 0 || x >= m || y < 0 || y >= n) break; 36 if(vis[x][y]) 37 { 38 flow = 1; 39 tt = cnt-vis[x][y]; 40 cnt = vis[x][y]-1; 41 break; 42 } 43 vis[x][y] = cnt; 44 t = check(map[x][y]); 45 x += dir[t][0]; 46 y += dir[t][1]; 47 cnt++; 48 } 49 if(flow == 0) 50 printf("%d step(s) to exit\n", cnt-1); 51 else printf("%d step(s) before a loop of %d step(s)\n", cnt, tt); 52 } 53 return 0; 54 }
标签:
原文地址:http://www.cnblogs.com/hsq666/p/4506730.html