hduoj1010,Tempter of the Bone,持续dfs剪枝剪枝
来源:互联网 发布:淘宝网后台管理 编辑:程序博客网 时间:2024/04/28 04:09
Tempter of the Bone
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.
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.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES
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.
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.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES
分析:
两个剪枝:
1.以走的最短路径小于时间
2.可以把map看成这样:
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
从为 0 的格子走一步,必然走向为 1 的格子
从为 1 的格子走一步,必然走向为 0 的格子
即:
0 ->1或1->0必然是奇数步
0->0 走1->1必然是偶数步
直接判断行与列相加对2取余的值来判断0,1
code:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<cstdlib>using namespace std;char area[10][10];int n,m,t,si,sj,di,dj;int dir[4][2]={-1,0,0,-1,1,0,0,1};bool ok;void dfs(int si,int sj,int step){ if(ok==true) return; if(step==t) { if(si==di&&sj==dj) { ok=true; printf("YES\n"); return; } else return; } for(int i=0;i<4;i++) { if(si+dir[i][0]>0&&si+dir[i][0]<=n&&sj+dir[i][1]>0&&sj+dir[i][1]<=m&&area[si+dir[i][0]][sj+dir[i][1]]!='X') { area[si][sj]='X'; dfs(si+dir[i][0],sj+dir[i][1],step+1); if(!ok) area[si][sj]='.'; } }}int main(){ while(scanf("%d %d %d",&n,&m,&t),n||m||t) { ok=false; memset(area,'X',sizeof(area)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { cin>>area[i][j];//cin可以忽略换行符,避免了getchar()的使用 if(area[i][j]=='S') {si=i;sj=j;} else if(area[i][j]=='D') {di=i;dj=j;} } if(abs(si-di)+abs(sj-dj)>t||(t%2!=0&&((si+sj)%2==(di+dj)%2))||t%2==0&&((si+sj)%2!=(di+dj)%2)) ok=false; else dfs(si,sj,0); if(!ok) printf("NO\n"); } return 0;}
- hduoj1010,Tempter of the Bone,持续dfs剪枝剪枝
- Tempter of the Bone DFS+奇偶剪枝
- Tempter of the Bone(DFS+剪枝)
- HDU1010 Tempter of the Bone 【DFS】+【剪枝】
- HDU1010 Tempter of the Bone DFS+剪枝
- DFS+剪枝 hdu1010 Tempter of the Bone
- Tempter of the Bone-DFS剪枝
- Tempter of the Bone(DFS+剪枝)
- hdu1010 Tempter of the Bone(DFS+剪枝)
- Tempter of the Bone(DFS+剪枝)
- hdu1010 Tempter of the Bone(dfs+剪枝)
- Tempter of the Bone (dfs + 奇偶剪枝)
- Tempter of the Bone(DFS+剪枝)
- Tempter of the Bone-dfs+奇偶剪枝
- Tempter of the Bone(dfs, 奇偶剪枝)
- ZOJ 2110 Tempter of the Bone DFS搜索+奇偶剪枝
- 【hdu】 Tempter of the Bone (dfs+剪枝)
- hdu 1010 Tempter of the Bone(DFS+剪枝优化)
- 结构体在内存中所占空间大小的计算
- CTRL、SHIFT、ALT及WIN键的功能
- 分页查询 (用row_number() 和开窗函数over()更方便)
- CDT 快捷键
- Tcl 表达式
- hduoj1010,Tempter of the Bone,持续dfs剪枝剪枝
- fragment 实例化引起的错误及活动上下文对象Context 进行查找控件
- sql中的子查询
- 一个简单的java小应用程序
- JSP技术(一)
- VBScript:err.clear与On error goto 0的区别
- Learing Maven 3 - Optimizing and Refactoring POMs
- JS比较两个数值的大小
- VC动态改变CComboBox风格