poj1537 Robot Motion
来源:互联网 发布:医学英文单词软件 编辑:程序博客网 时间:2024/06/04 01:38
题意很清楚,就是一道搜索的题,按照字母代表的方向走,一步步来就行了,把走过的点从0开始依次变为0、1、2、3、……,当出边或者又再次碰到相同的点时候返回;
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>int m,n,st,step,flag;int b[50][50],a[50][50];char s[50][50];int dfs(int x,int y){ int next[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};//定义一个方向数组,方便搜索 int tx,ty,k; int t=0; while(1) { t++; if(s[x][y]=='N')//四种搜索情况 k=0; if(s[x][y]=='S') k=1; if(s[x][y]=='W') k=2; if(s[x][y]=='E') k=3; tx=x+next[k][0]; ty=y+next[k][1]; step++;//确定k值后往后走一步,步骤加1 if(b[tx][ty]==1)//判断该点是否走过 { flag=1; return a[tx][ty];//如果走过,标记一下,并记录该点的坐标,然后返回,方便计算从什么时候开始出现回路 } if(tx<0||tx>m-1||ty<0||ty>n-1)//判断出边 return step; a[tx][ty]=t;//将走过的点依次标记为0、1、2、3、…… b[tx][ty]=1; x=tx;//更新x,y的值 y=ty; }}int main(){ int i; while(scanf("%d%d%d",&m,&n,&st),m!=0||n!=0||st!=0) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(s,0,sizeof(s)); for(i=0; i<m; i++) scanf("%s",s[i]); b[0][st-1]=1; step=0; flag=0;//不能忘记标志变量的初始化 int h=dfs(0,st-1); if(flag==1) printf("%d step(s) before a loop of %d step(s)\n",h,step-h); else printf("%d step(s) to exit\n",step); } return 0;}
0 0
- poj1537 Robot Motion
- Robot Motion
- Robot Motion
- Robot Motion
- Robot Motion
- Robot Motion
- Robot Motion
- Robot Motion
- Robot Motion
- Robot Motion
- Robot Motion
- poj 1573 Robot Motion
- Poj 1573 Robot Motion
- hdu 1035 Robot Motion
- HDU 1035 Robot Motion
- hdoj 1035 Robot Motion
- hdu 1035 Robot Motion
- poj 1573 Robot Motion
- Nginx 项目部署和配置
- 断言assert()用法
- Mysql学习总结(6)——MySql之ALTER命令用法详细解读
- Codeforces 268E Playlist
- 特征选择--scikit-learn
- poj1537 Robot Motion
- JVM类加载时经典的双亲委派模型
- Mysql学习总结(7)——MySql索引原理与使用大全
- AAC 音频数据的两种格式
- HBase Phoenix 机制
- mysql全解
- Multiplication Table 【二分】
- bzoj1834(网络流+费用流)
- 广告推荐算法学习