hdu1010 dfs奇偶剪枝
2016-03-19 09:34
225 查看
[align=left]Problem Description[/align]
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately
to get out of this maze.
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the
T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for
more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
[align=left]Input[/align]
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively.
The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
[align=left]Output[/align]
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately
to get out of this maze.
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the
T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for
more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
[align=left]Input[/align]
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively.
The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
[align=left]Output[/align]
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
[align=left]Sample Input[/align]
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
[align=left]Sample Output[/align]
NO YES
#include<iostream> #include<cstring> #include<string> #include<cmath> using namespace std; int dx[4]={-1,1,0,0}; int dy[4]={0,0,-1,1}; int map[10][10]={0}; int n,m,t,a1,a2,b1,b2; bool flag=0; bool dfs(int x,int y,int t1) { if(flag==1||t1==0&&x==a2&&y==b2) { flag=1; return true; } int temp=t1-abs(x-a2)-abs(y-b2); if(temp<0||temp%2) return false;<span style="color: rgb(255, 0, 0); font-family: Arial, Helvetica, sans-serif;">//temp</span><span style="color: rgb(255, 0, 0); font-family: 宋体;">必须为偶数才行</span> map[x][y]=1; int i,x0,y0; for(i=0;i<=3;i++) { x0=x+dx[i]; y0=y+dy[i]; if(x0>=1&&x0<=n&&y0>=1&&y0<=m&&!map[x0][y0]) { dfs(x0,y0,t1-1); if(flag) return true; } } map[x][y]=0; } int main() { int i,j; char ch; while(cin>>n) { cin>>m; cin>>t; if(m==0&&n==0&&t==0) return 0; flag=0; for(i=1;i<=n;i++) for(j=1;j<=m;j++) { cin>>ch; if(ch=='S') { map[i][j]=0; a1=i; b1=j; } if(ch=='D') { map[i][j]=0; a2=i; b2=j; } if(ch=='.') map[i][j]=0; if(ch=='X') map[i][j]=1; } dfs(a1,b1,t); if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
相关文章推荐
- 利用Arrays.fill()方法给二维数组设定元素值
- 奇怪的分式
- 服务端开发(JAVA)开源项目的官方网址
- 正则表达式入门教程
- [Android进阶]Binder学习(初始篇)
- 【小魔法】console的一点小指南
- iOS数据持久化之偏好设置
- hdoj 2111 Saving HDU (贪心)
- 关于“Scrum敏捷项目管理”
- iOS代理设计模式
- Mustache的简单使用
- 性能优化——SQL语句(续)
- linux下的sed用于分类标签的建立
- 《Concurrency Programming Guide》 笔记
- 【POJ1204】Word Puzzles——AC自动机
- ScrollView中嵌套GridView,ListView只显示一行的解决办法
- Winform开发框架主界面设计展示
- SmartGit使用总结之忽略文件.gitignore
- UVA 11134 Fabled Rooks
- 敏捷带来的新东西