hdu 1010 深搜+奇偶剪枝
来源:互联网 发布:无主之地2mac汉化 编辑:程序博客网 时间:2024/06/05 09:08
Problem Description
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.
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.
Input
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.
'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.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5S.X...X...XD....3 4 5S.X...X....D0 0 0
Sample Output
NOYES
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int row,line,times,sx,sy,ex,ey,result;char map[51][51];void dfs(int x,int y,int count){ if(result) return ; if(x==ex&&y==ey&&count==times) { result=1; return; } if(count>times||abs(x-sx)+abs(y-sy)>times||(count-abs(x-sx)-abs(y-sy))%2==1)//每次都要剪枝 return ; char t=map[x][y]; map[x][y]='X'; if(!result&&x>-1&&(map[x-1][y]=='.'||map[x-1][y]=='D')) dfs(x-1,y,count+1); if(!result&&x<line&&(map[x+1][y]=='.'||map[x+1][y]=='D')) dfs(x+1,y,count+1); if(!result&&y<row&&(map[x][y-1]=='.'||map[x][y-1]=='D')) dfs(x,y-1,count+1); if(!result&&x>-1&&(map[x][y+1]=='.'||map[x][y+1]=='D')) dfs(x,y+1,count+1); map[x][y]=t;//map[][]值回溯}int main(){ int i,j; while(scanf("%d%d%d",&line,&row,×)&&line&&row&×) { for(i=0;i<line;i++) { for(j=0;j<row;j++) { cin >> map[i][j]; if(map[i][j]=='S') {sx=i;sy=j;} if(map[i][j]=='D') {ex=i;ey=j;} } } if(abs(ex-sx)+abs(ey-sy)>times||(times-abs(ex-sx)-abs(ey-sy))%2==1)//奇偶剪枝 { printf("NO\n");continue; } result=0; dfs(sx,sy,0); if(result) printf("YES\n"); else printf("NO\n"); } return 0;}
- hdu 1010 深搜+奇偶剪枝
- hdu 1010 奇偶剪枝
- hdu 1010 奇偶剪枝
- DFS深搜 + 奇偶剪枝 HDU-1010
- hdu 1010 dfs+奇偶剪枝
- HDU 1010(dfs+奇偶剪枝)
- HDU 1010 DFS+奇偶剪枝
- hdu 1010 dfs,奇偶剪枝
- hdu 1010 dfs+奇偶剪枝
- HDU 1010 【搜索+奇偶剪枝】
- DFS 奇偶剪枝 HDU 1010
- hdu 1010 dfs+奇偶剪枝
- HDU 1010 dfs+奇偶剪枝
- hdu 1010 dfs(奇偶剪枝)
- HDU 1010 Tempter of the Bone 深搜+奇偶剪枝
- HDU 1010 Tempter of the Bone 【深搜+奇偶剪枝】
- HDU 1010 经典深搜+奇偶剪枝
- HDU 1010( DFS+奇偶剪枝)
- 一个游戏程序员的学习资料
- SHELL 编程入门与提高(一)第一个shell程序
- Flex主题教程系列4--Flex组件生命周期之生活周期
- linux c -- 环境变量和参数
- JAVA类集----List接口
- hdu 1010 深搜+奇偶剪枝
- 编程珠矶 第八章 第9题
- Oracle10.2下载地址
- C语言的那些小秘密——异常处理(转bigloomy)
- 基本数据类型 (代码大全 第十一章)
- 利用FreeMarker如何生成静态页面(转)
- 使用AutoMake轻松生成Makefile
- uva 196 - Spreadsheet
- 玩转BeagleBoard xM——建立虚拟机开发环境和嵌入式Linux系统