HDU1010 Tempter of the Bone 【DFS】+【剪枝】
2014-08-14 20:34
363 查看
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 70895 Accepted Submission(s): 19535
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
[align=left]Sample Input[/align]
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
[align=left]Sample Output[/align]
NO
YES
这题真恶心,用scanf("%c")或者getchar一直WA,换成cin就A了。
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#define maxn 10
using namespace std;
char map[maxn][maxn];
int n, m, t, dx, dy;
const int mov[][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
bool check(int x, int y){
return x >= 0 && x < n && y >= 0 &&
y < m && map[x][y] != 'X';
}
bool DFS(int x, int y, int step)
{
if(x == dx && y == dy && step == t)
return 1;
int i, a, b, tmp;
tmp = t - step - abs(dx - x) - abs(dy - y);
if(tmp < 0 || tmp & 1) return 0;
for(i = 0; i < 4; ++i){
a = x + mov[i][0];
b = y + mov[i][1];
if(check(a, b)){
map[a][b] = 'X';
if(DFS(a, b, step + 1)) return 1;
map[a][b] = '.';
}
}
return 0;
}
int main()
{
//freopen("stdin.txt", "r", stdin);
int i, j, sx, sy, wall;
while(scanf("%d%d%d", &n, &m, &t), n || m || t){
for(i = wall = 0; i < n; ++i){
//getchar();
for(j = 0; j < m; ++j){
cin >> map[i][j];
if(map[i][j] == 'S'){
sx = i; sy = j;
}else if(map[i][j] == 'D'){
dx = i; dy = j;
}else if(map[i][j] == 'X')
++wall;
}
}
if(m * n - wall <= t){ //cut1
printf("NO\n"); continue;
}
map[sx][sy] = 'X';
if(DFS(sx, sy, 0)) printf("YES\n");
else printf("NO\n");
}
return 0;
}
原奇了怪的WA代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxn 10
char map[maxn][maxn];
int n, m, t, dx, dy, ok;
const int mov[][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
bool check(int x, int y){
return x >= 0 && x < n && y >= 0 && y < m
&& map[x][y] != 'x' && map[x][y] != 'X';
}
bool DFS(int x, int y, int step)
{
if(x == dx && y == dy && step == t){
return 1;
}
int i, a, b, tmp;
tmp = (t - step) - abs(dx - x) - abs(dy - y);
if(tmp < 0 || tmp & 1) return 0;
for(i = 0; i < 4; ++i){
a = x + mov[i][0];
b = y + mov[i][1];
if(check(a, b)){
map[a][b] = 'X';
if(DFS(a, b, step + 1)) return 1;
map[a][b] = '.';
}
}
return 0;
}
int main()
{
int i, j, sx, sy, wall;
while(scanf("%d%d%d", &n, &m, &t), n || m || t){
for(i = wall = 0; i < n; ++i){
getchar();
for(j = 0; j < m; ++j){
scanf("%c", &map[i][j]);
if(map[i][j] == 'S'){
sx = i; sy = j;
}else if(map[i][j] == 'D'){
dx = i; dy = j;
}else if(map[i][j] == 'X')
++wall;
}
}
if(m * n - wall <= t){ //cut1
printf("NO\n"); continue;
}
map[sx][sy] = 'X'; ok = 0;
if(DFS(sx, sy, 0)) printf("YES\n");
else printf("NO\n");
}
return 0;
}
相关文章推荐
- hdu1010 Tempter of the Bone(DFS,剪枝,递归,回溯)
- hdu1010 Tempter of the Bone dfs搜索 剪枝
- HDU1010-Tempter of the Bone(DFS+各种剪枝)
- hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝
- DFS+剪枝 hdu1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝
- HDU 1010 Tempter of the Bone(DFS + 剪枝)
- HDU1010 Tempter of the Bone (DFS入门)
- hdu 1010 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 + 剪枝)
- HDU 1010----Tempter of the Bone (dfs+剪枝)
- HDU 1010 Tempter of the Bone(逃离地狱,DFS,剪枝,错题集)
- HDU1010 Tempter of the Bone(回溯 + 剪枝)
- HDU1010:Tempter of the Bone(DFS)
- HDU1010 Tempter of the Bone(深度优先搜索DFS+奇偶性剪枝)
- hdu1010 Tempter of the Bone (DFS)
- hdu1010 Tempter of the Bone(DFS)