HDU1010 dfs奇偶剪枝
来源:互联网 发布:java政府驻场开发 编辑:程序博客网 时间:2024/05/16 19:05
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 116557 Accepted Submission(s): 31603
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
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1010
题意
给定NxM的矩阵和时刻K,矩阵中'S'代表起点,'D'代表终点,X不可走,'.'可以行走,但不能重复走,问从'S'出发,是否能恰好在时刻K时到达'D'。
题解
dfs回溯+奇偶剪枝。关键: 网格中两点的所有可行路径,长必定同奇偶。 根据这一信息,在求解过程中比较 当前点(r, c)到终点(bx, by)之间的最短路abs(bx-r)+abs(by-c) 和 当前还必须要走的路径长度(也就是当前剩余时间)k-cnt 的奇偶性,奇偶性不同时可以剪枝。 判断奇偶性相同用两路径之差&1(或%2)==1即可判断。注意: (1)有终点的dfs中要用flag标记是否已经到达终点 (2)vis数组记得清零!!!
#include <iostream>#include <string.h>#include <algorithm>#include <stdio.h>#include <math.h>#include <stack>#include <queue>#include <vector>#define INF 0x3f3f3f3f using namespace std;const int maxn = 10;int used[maxn][maxn];char pic[maxn][maxn];int m, n, k;int flag;int ax, ay, bx, by;int dr[4] = {-1,0,1,0};int dc[4] = {0,1,0,-1};void dfs(int r, int c, int cnt){ //flag,判定是否已经到达终点了 if(flag || cnt>k || r<0 || r>=m || c<0 || c>=n) return; if(bx == r && by == c && cnt == k){ flag = 1; return; } //奇偶剪枝 int temp = abs(bx-r)+abs(by-c); temp = k-cnt-temp; if(temp&1) return; for(int i=0; i<4; i++){ int r1 = r+dr[i]; int c1 = c+dc[i]; if(!used[r1][c1] && pic[r1][c1]!='X'){ used[r1][c1] = 1; dfs(r1, c1, cnt+1); used[r1][c1] = 0; } }}int main(){ while(scanf("%d%d%d", &m, &n, &k) && (m||n||k)){ memset(used, 0, sizeof(used)); for(int i=0;i<m;i++){ scanf("%s", pic[i]); for(int j=0;j<n; j++){ if(pic[i][j] == 'S'){ ax = i; ay = j; used[ax][ay] = 1; } if(pic[i][j] == 'D'){ bx = i; by = j; } } } int cnt = 0; flag = 0; dfs(ax, ay, cnt); if(flag){ printf("YES\n"); } else{ printf("NO\n"); } } return 0;}
- HDU1010-奇偶剪枝(DFS)
- hdu1010 dfs奇偶剪枝
- HDU1010奇偶剪枝DFS
- HDU1010 DFS+奇偶剪枝
- HDU1010 dfs奇偶剪枝
- HDU1010 DFS+奇偶剪枝
- hdu1010(DFS + 奇偶剪枝)
- hdu1010(DFS+奇偶剪枝)
- hdu1010 (dfs+奇偶剪枝)
- hdu1010(dfs+奇偶剪枝)
- HDU1010(dfs+奇偶剪枝)
- hdu1010 dfs(奇偶剪枝,路径剪枝)
- HDU1010 奇偶剪枝 + 预先筛选 + DFS搜索
- hdu1010 经典的DFS+奇偶剪枝
- HDU1010 深搜+奇偶剪枝
- hdu1010 奇偶剪枝
- hdu1010 Tempter of the Bone(dfs+奇偶剪枝)
- hdu1010 Tempter of the Bone --DFS & 奇偶剪枝
- 深入浅出RxJava_06[传输过程的条件&组合操作]
- L1-009. N个数求和
- Unity3D Player角色移动控制脚本
- Corrective 3D Reconstruction of Lips from Monocular Video 简读
- centos u盘安装
- HDU1010 dfs奇偶剪枝
- GAP平台
- 获取数据转换成JSON
- 437. Path Sum III
- canvas与html5实现视频截图功能
- springMVC 几种页面跳转方式
- 关于使用 MongoDB Connector for Hadoop 的经验和教训
- Atitit 如何在外包找到自己的技术深度
- 笔记9