hdu 1035 Robot Motion

http://acm.hdu.edu.cn/showproblem.php?pid=1035  模拟 

View Code
 1 #include<iostream>
2 #include<cstring>
3 #include<cstdlib>
4 using namespace std;
5 const int N=200;
6 char mat[N][N];
7 int step[N][N];//到达改点的步数
8 int n,m,s;
9 void initMat()
10 {
11 for(int i=0;i<N;i++)
12 for(int j=0;j<N;j++) mat[i][j]='#',step[i][j]=0;//
13 }
14 void print()
15 {
16 int count=0;
17 for(int i=1;i<=n;i++)
18 for(int j=1;j<=m;j++)
19 {
20 cout<<mat[i][j]<<" ";
21 if(++count%m==0) cout<<endl;
22 }
23 cout<<"***************************"<<endl;
24 }
25 void find()
26 {
27 int x=1,y=s,count=0;
28 char cur=mat[x][y];//开始时位置
29 step[x][y]=0;
30 mat[x][y]='$';//visited
31 while(true)
32 {
33
34 if(cur=='N') x--;//up
35 else if(cur=='E') y++;//right
36 else if(cur=='S') x++;//down
37 else if(cur=='W') y--;//left
38 cur=mat[x][y];
39
40 //print();
41
42 if(mat[x][y]=='#')//get out
43 {
44 cout<<count+1<<" step(s) to exit"<<endl;
45 return ;
46 }
47 else if(mat[x][y]=='$')//这里走过了
48 {
49 cout<<step[x][y]<<" step(s) before a loop of "<<++count-step[x][y]<<" step(s)"<<endl;
50 return ;
51 }
52 mat[x][y]='$';//走过,then mark
53 count++;//步数
54 step[x][y]=count;
55 }
56 }
57 int main()
58 {
59 while(cin>>n>>m && n && m && cin>>s)
60 {
61 initMat();
62 int i,j;
63 for(i=1;i<=n;i++)
64 for(j=1;j<=m;j++) cin>>mat[i][j];
65 find();
66 }
67 return 0;
68 }


 

posted @ 2012-03-31 17:08  keepmoving89  阅读(138)  评论(0编辑  收藏  举报