标签:
Description
Input
Output
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 <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 int main() 7 { 8 int a,b,c; //行数,列数,机器人所在的列 9 char str[15][15]; 10 bool vis[15][15]; 11 int count[15][15]; 12 while(cin>>a>>b>>c,a||b||c) 13 { 14 int step=0; 15 char ch; 16 memset(str,‘O‘,sizeof(str)); //初始化为字符‘O’ 17 memset(vis,false,sizeof(vis)); //标记是否访问过 18 memset(count,0,sizeof(count)); //记录步数 19 for(int i=1; i<=a; i++) 20 { 21 for(int j=1; j<=b; j++) 22 cin>>str[i][j]; 23 } 24 int i=1,j=c; //机器人所在的位置 25 while(1) 26 { 27 if(vis[i][j]==true) //这个点以走过,构成了循环 28 { 29 cout<<count[i][j]<<" step(s) before a loop of "<<step-count[i][j]<<" step(s)"<<endl; 30 break; 31 } 32 ch=str[i][j]; 33 if(ch==‘O‘) //已经走出 34 { 35 cout<<step<<" step(s) to exit"<<endl; 36 break; 37 } 38 count[i][j]=step; 39 vis[i][j]=true; 40 switch(ch) 41 { 42 case ‘N‘: 43 i--; 44 step++; 45 continue; 46 case ‘S‘: 47 i++; 48 step++; 49 continue; 50 case ‘E‘: 51 j++; 52 step++; 53 continue; 54 case ‘W‘: 55 j--; 56 step++; 57 continue; 58 } 59 } 60 } 61 return 0; 62 }
标签:
原文地址:http://www.cnblogs.com/cxbky/p/4954140.html