hdoj1010Tempter of the Bone(DFS+剪枝)
来源:互联网 发布:神乃木庄龙 知乎 编辑:程序博客网 时间:2024/06/13 16:44
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
题意:给你一定的步数问能不能正好从起点走到终点。
思路:DFS深搜,这道题直接搜索很好写,主要wa点就是超时,必须要对DFS进行剪枝,所谓剪枝就是减少不必要的操作,像越界什么的。这道题判断是否是墙,是否越界,是否访问过第一次剪枝,第二次剪枝是判断剩余步数和所在点到终点步数奇偶是否相同(如果一个是奇数一个是偶数肯定不可能正好到达),第三次剪枝就是已经判断可以到达终点还有所走步数已经超过所给步数。三次剪枝然后就能ac啦
代码:
#include<iostream>#include<stdio.h>#include<algorithm>#include<math.h>#include<string.h>using namespace std;char map[20][20];int vis[20][20];int n,m,k;int sx,sy;int ex,ey;int h;void dfs(int x,int y,int ans){ if(map[x][y]=='X'||vis[x][y]||x<0||x>=n||y<0||y>=m||ans>k||h) return; if((abs(ex-x)+abs(ey-y))%2!=(k-ans)%2) return; if(map[x][y]=='D'&&ans==k) { h=1; return;} if(map[x][y]=='D') return; vis[x][y]=1; if(map[x+1][y]!='X'&&vis[x+1][y]==0&&!h)dfs(x+1,y,ans+1); if(map[x-1][y]!='X'&&vis[x-1][y]==0&&!h)dfs(x-1,y,ans+1); if(map[x][y+1]!='X'&&vis[x][y+1]==0&&!h)dfs(x,y+1,ans+1); if(map[x][y-1]!='X'&&vis[x][y-1]==0&&!h)dfs(x,y-1,ans+1); vis[x][y]=0;}int main(){ while(scanf("%d%d%d",&n,&m,&k)!=EOF) { memset(map,0,sizeof(map)); memset(vis,0,sizeof(vis)); if(n==0&&m==0&&k==0) break; for(int i=0;i<n;i++) { scanf("%s",map[i]); for(int j=0;j<m;j++) { if(map[i][j]=='S') sx=i,sy=j; if(map[i][j]=='D') ex=i,ey=j;} } h=0; dfs(sx,sy,0); if(h==1) printf("YES\n"); else printf("NO\n"); } return 0;}
0 0
- hdoj1010Tempter of the Bone(DFS+剪枝)
- hdoj1010Tempter of the Bone
- Tempter of the Bone(DFS+剪枝)
- Tempter of the Bone(DFS+剪枝)
- Tempter of the Bone(DFS+剪枝)
- 【hdu】 Tempter of the Bone (dfs+剪枝)
- Tempter of the Bone(DFS + 奇偶剪枝,好题)
- Tempter of the Bone(DFS+奇偶剪枝)
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
- HDU1010-Tempter of the Bone(DFS+各种剪枝)
- hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)
- hdu1010——Tempter of the Bone(DFS+剪枝)
- hdu 1010 Tempter of the Bone (dfs+剪枝)
- hdu1010 Tempter of the Bone(DFS,剪枝,递归,回溯)
- HDU-#1010 Tempter of the Bone(DFS+剪枝)
- hdu 1010 Tempter of the Bone (dfs+奇偶剪枝)
- HDU 1010 Tempter of the Bone(DFS+剪枝)
- hdu 1010 Tempter of the Bone(dfs + 奇偶剪枝)
- C++ Builder 中对listview模拟复制某个单元格的内容
- spring事务管理
- Idea SpringMVC+Spring+MyBatis+Maven整合
- asp.net生成Excel并导出下载五种实现方法
- js判断手机浏览器屏幕方向
- hdoj1010Tempter of the Bone(DFS+剪枝)
- 单生产者 多消费者
- [jq]jquery的has,find和filter方法的区别
- HTTP中的八种方法
- Hadoop:HDFS数据组织
- PTA 5-13 日K蜡烛图 (15分)
- 动态加载HorizontalSrcollView与ViewPager关联使用
- 【miscellaneous】网络摄像机
- css学习小记