4.3.1--Tempter of the Bone--深搜(优化剪枝)
2017-06-23 23:17
281 查看
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 Input4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0 |
Sample OutputNO YES |
参考:http://blog.csdn.net/akof1314/article/details/4429658
代码:
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; int n,m,T,ei,ej; bool flag; char a[8][8]; bool ok[8][8]; int dx[4]={0,0,1,-1}; int dy[4 bd55 ]={1,-1,0,0}; void dfs(int x,int y,int t) { if(flag||t>50) return; //找到结果或时间超过上限,返回 if(x==ei&&y==ej&&t==T) { flag=true; //到达终点且结果刚好 return; } int temp=(T-t)-(abs(x-ei)+abs(y-ej)); //剩余时间-到达终点的最短路径 if(temp<0||temp%2==1) return; //时间不够到终点或为奇数剪枝 for(int k=0;k<4;k++) { int xx=x+dx[k],yy=y+dy[k]; if(xx>=0&&xx<n&&yy>=0&&yy<m) { if(a[xx][yy]!='X'&&t<T) { a[xx][yy]='X'; dfs(xx,yy,t+1); a[xx][yy]='.'; } } } } int main() { int i,j,bi,bj,wall; while(scanf("%d%d%d",&n,&m,&T)&&(n+m+T)) { wall=0; for(i=0;i<n;i++) { scanf("%s",a[i]); for(j=0;j<m;j++) { if(a[i][j]=='S') {bi=i; bj=j;} else if(a[i][j]=='D') {ei=i; ej=j;} else if(a[i][j]=='X') wall++; } } if((n*m-wall)<T) {printf("NO\n"); continue;} flag=false; a[bi][bj]='X'; dfs(bi,bj,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- hdu 1010 Tempter of the Bone(DFS+剪枝优化)
- HDUOJ-1010 Tempter of the Bone(深搜+剪枝优化)
- Tempter of the Bone--dfs加剪枝优化
- hdu 1010 Tempter of the Bone(DFS+剪枝优化)
- hduoj1010,Tempter of the Bone,持续dfs剪枝剪枝
- hdu 1010 Tempter of the Bone 深搜+剪枝
- HDOJ 1010 Tempter of the Bone DFS 奇偶优化
- hdu 1010 Tempter of the Bone DFS+奇偶性剪枝
- ZOJ 2110(HDU 1010) Tempter of the Bone(经典剪枝-奇偶剪枝)
- HDU 1010 Tempter of the Bone (dfs + 剪枝)
- HDU1010 Tempter of the Bone 【DFS】+【剪枝】
- HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝
- (hdu step 4.3.1)Tempter of the Bone(在特定的时间约束下,判断是否能够从起点达到终点)
- 基于深度优先搜索的回溯算法(递归剪枝及奇偶性剪枝好题!):HDOJ 1010 - Tempter of the Bone
- hdu-1010-Tempter of the Bone(搜索 优化)
- hdu1010 Tempter of the Bone(DFS,剪枝,递归,回溯)
- (step4.3.1) hdu 1010(Tempter of the Bone——DFS)
- 【剪枝】HDU 1010——tempter of the bone
- HDU 1010 Tempter of the Bone (深度搜索+减枝优化)
- hdu 1010 Tempter of the Bone (dfs+剪枝)