UVa 10452 - Marcus
2016-04-05 18:58
369 查看
#include <iostream> #define EOF (-1) using namespace std; char Path[12][100]; int Step[7]={0};//记录走过的足迹 char example[8] = {'@','I','E','H','O','V','A','#'}; void Dfs(int n,int m,int ex); int main(){ int line,m,n; string Commands[3]={"forth","left","right"}; //freopen("D:\\t.txt","r",stdin); while((cin>>line)&&(line!=EOF)){ for(int i = 0;i < line;i++){ cin>>n>>m;//输入鹅卵石路的宽m,长n for(int j=0;j<n;j++){ for(int k=0;k<m;k++){ cin>>Path[j][k]; } } for(int q = 0;q < m;q++){ if(example[0] == Path[n-1][q]){ Dfs(n-1,q,0); break; } } for(int t = 0;t < 7;t++){ cout<<Commands[Step[t]]; if(t<6)cout <<" "; } cout<<endl; } } return 0; } void Dfs(int n,int m,int ex) { if( (Path[n-1][m] == example[ex+1]) && (ex<7)) { Step[ex] = 0; Dfs(n-1,m,ex+1); } if( (Path [m-1] == example[ex+1]) && (ex<7)){ Step[ex] = 1; Dfs(n,m-1,ex+1); } if( (Path [m+1] == example[ex+1]) && (ex<7)){ Step[ex] = 2; Dfs(n,m+1,ex+1); } }
原来递归运算时陷入了死循环,加上“ex>7”的判定,就好了;
输入的时候注意m,n(我是新手(*^__^*) )
题目意思很简单,就是沿着一个路径走到底就行,用dfs解决。
相关文章推荐
- UVa144 Student Grants
- Poj 2051 Argus
- POJ 2062 Card Game Cheater
- Uva 10336 Rank the Languages
- 找不到visual studio模板信息 解决方法
- 26.Remove Duplicates from Sorted Array
- 第四周 项目5(3)
- Serializable 和 Parcelable 区别
- Go语言核心之美 4.2-递归函数
- 【FFMPEG】各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式
- Hadoop学习第一次复习
- 菜鸟学习之smartupload+servlet简单的文件上传
- Tsinsen A1124 进制转换
- Scripts after the Linux server installation
- Activity之间使用intent传递大量数据带来问题总结
- imx6 板卡移植官方yocto版本(2_定制系统)
- linux文件系统介绍
- 构建之法:第四章读后感
- 求长方柱的体积和表面积
- imx6 板卡移植官方yocto版本(1_安装环境)