Robot Motion(POJ 1573 模拟题)

~题目链接~

http://poj.org/problem?id=1573

输入

3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0

结果

10 step(s) to exit
3 step(s) before a loop of 8 step(s)

输入行数、列数、机器人的位置

机器人可以出去,则输出步数

否则,则输出循环的始端和终端

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 
 5 int fx[4]= {-1,1,0,0};
 6 int fy[4]= {0,0,-1,1};
 7 int map[20][20],vis[20][20],st[20][20];
 8 int flag,step,n,m,k,x,y;
 9 
10 void DFS(int i,int j)
11 {
12     int I,J;
13     st[i][j]=++step;
14     I=i+fx[map[i][j]];
15     J=j+fy[map[i][j]];
16     if(I<0 || I>=n || J<0 || J>=m)
17         return;
18     if(!vis[I][J])
19     {
20         vis[i][j]=1;
21         DFS(I,J);
22     }
23     else
24     {
25         flag=1;
26         x=st[I][J]-1;
27         y=st[i][j]-st[I][J]+1;
28         return;
29     }
30 }
31 
32 int main()
33 {
34     char str;
35     while(~scanf("%d%d%d",&n,&m,&k) && (n!=0 || m!=0 || k!=0))
36     {
37         getchar();
38         memset(vis,0,sizeof(vis));
39         memset(map,0,sizeof(map));
40         memset(st,0,sizeof(st));
41         for(int i=0; i<n; i++)
42         {
43             for(int j=0; j<m; j++)
44             {
45                 scanf("%c",&str);
46                 if(str=='N') map[i][j]=0;
47                 if(str=='S') map[i][j]=1;
48                 if(str=='W') map[i][j]=2;
49                 if(str=='E') map[i][j]=3;
50             }
51             getchar();
52         }
53         flag=0;
54         step=0;
55         DFS(0,k-1);
56         if(flag)
57             printf("%d step(s) before a loop of %d step(s)\n",x,y);
58         else
59             printf("%d step(s) to exit\n",step);
60     }
61 
62     return 0;
63 }
View Code

 

 

转载于:https://www.cnblogs.com/guoyongzhi/p/3243354.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值