HDOj 1010 Tempter of the Bone(DFS+奇偶性剪枝)
2017-08-15 01:14
429 查看
Tempter of the Bone |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 1720 Accepted Submission(s): 563 |
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 |
Author ZHANG, Zheng |
Source ZJCPC2004 |
Recommend JGShining |
不过不知道是奇偶性剪枝。
http://blog.csdn.net/libin56842/article/details/8962512
参考了这个大佬的题解。
因为v标记再结束前还原回去,导致WA了一次。
下面是我的代码;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n, m, t;
char map[8][8];
int v[8][8];
int dir[4][2] = { 0,1,1,0,-1,0,0,-1 };
bool flag;
void dfs(int starx, int stary, int endx, int endy,int deep)
{
int tem,s1,s2;
if (flag)return;
if (starx == endx&&stary == endy&&deep == t) { flag = true; return; }
tem = t - deep - abs(endx - starx) - abs(endy - stary);
if (tem < 0 || tem & 1)return;
for (int i = 0; i < 4; i++)
{
int tx, ty;
tx = starx + dir[i][0];
ty = stary + dir[i][1];
if (tx >= 0 && ty >= 0 && tx < n&&ty < m&&map[tx][ty] != 'X'&&v[tx][ty] == 0)
{
v[tx][ty] = 1;
dfs(tx, ty, endx, endy, deep + 1);
v[tx][ty] = 0;
}
}
}
int main()
{
while (scanf("%d%d%d",&n,&m,&t)!=EOF)
{
int starx, stary, endx, endy;
if (n == 0 && m == 0 && t == 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')
{
starx = i; stary = j;
}
if (map[i][j] == 'D')
{
endx = i; endy = j;
}
}
}
memset(v, 0, sizeof(v));
flag = false;
v[starx][stary] = 1;
dfs(starx, stary, endx, endy, 0);
if (flag)printf("YES\n");
else printf("NO\n");
}
return 0;
}
相关文章推荐
- HDU 1010 Tempter of the Bone(DFS+奇偶性剪枝)
- HDOJ 1010 Tempter of the Bone DFS 奇偶优化
- HDOJ.1010 Tempter of the Bone (DFS)
- HDOJ1010 Tempter of the Bone(DFS)
- zoj 2110 || hdoj 1010 Tempter of the Bone(DFS)
- HDOJ1010 Tempter of the Bone(DFS+奇偶剪枝)
- HDOJ 1010 Tempter of the Bone--DFS+奇偶剪枝(转)
- HDOJ 1010-Tempter of the Bone【dfs】
- HDOJ 1010 Tempter of the Bone(DFS奇偶剪枝)
- HDoj-1010-Tempter of the Bone-DFS
- 杭电OJ(HDOJ)1010题:Tempter of the Bone(DFS,迷宫求解)
- hdoj1010 Tempter of the Bone(迷宫问题dfs,奇偶剪枝)
- (DFS)hdoj1010-Tempter of the Bone
- hdoj 1010 Tempter of the Bone 【dfs+奇偶剪枝】
- HDOJ1010 Tempter of the Bone(DFS)
- hdoj 1010 Tempter of the Bone 【DFS】+【奇偶剪枝】
- hdoj 1010 Tempter of the Bone (dfs 奇偶剪枝)
- hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】
- hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
- Hdu 1010 Tempter of the Bone (DFS 经典奇偶剪枝)