poj 1573 Robot Motion
题目:http://poj.org/problem?id=1573
题意:已知坐标系大小,和初始的最上面的位置,根据命令求是否能出去,或者一直循环
这就是我建的系,画的有点难看哈!
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<stack> 6 #include<queue> 7 #include<cmath> 8 #include<algorithm> 9 using namespace std; 10 char map[500][500]; 11 int vis[500][500]; 12 13 int dx[5]={0,0,-1,1};//分别代表N,S,W,E 14 int dy[5]={1,-1,0,0}; 15 int main() 16 { 17 int i,j,a,b,t,f,sum,x; 18 while(cin>>a>>b>>t&&(a!=0||b!=0||t!=0)) 19 { 20 sum=0; 21 memset(vis,0,sizeof(vis)); 22 for(j=a; j>=1; j--) 23 { 24 for(i=1; i<=b; i++) 25 cin>>map[i][j]; 26 } 27 i = t; j = a; 28 while(1) 29 { 30 sum++; 31 vis[i][j]=sum; 32 if(map[i][j]=='N') {i+=dx[0]; j+=dy[0];} 33 else if(map[i][j]=='S') {i+=dx[1]; j+=dy[1];} 34 else if(map[i][j]=='W') {i+=dx[2]; j+=dy[2];} 35 else if(map[i][j]=='E') {i+=dx[3]; j+=dy[3];} 36 if(vis[i][j]) 37 { 38 f=1; 39 x=vis[i][j]; 40 break; 41 } 42 if(i>b||j>a || i<=0 ||j<=0) 43 { 44 f=2; 45 break; 46 } 47 } 48 if(f==2) 49 { 50 printf("%d step(s) to exit\n",sum); 51 } 52 else 53 { 54 printf("%d step",x-1); 55 printf("(s)"); 56 printf(" before a loop of %d step",sum-x+1); 57 printf("(s)"); 58 printf("\n"); 59 } 60 } 61 return 0; 62 }