您的位置:首页 > 其它

hdu 1010 Tempter of the Bone

2017-02-17 11:58 246 查看

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 115817 Accepted Submission(s): 31439


[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
分析:用深搜,题目只问能不能,所以需要立一个flag = 0,所有情况搜索完之后(有情况符合,则flag = 1),最后判断flag是否为1,不用奇偶剪枝的会超时(。。。奇偶剪枝现学,之前没听过,网上找了奇偶剪枝的原理)。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

char maze[10][10];
bool visited[10][10];
int N, M, T, sx, sy, ex, ey;
bool flag;

int dir_x[4] = {-1, 0, 1, 0};
int dir_y[4] = {0, 1, 0, -1};

void dfs(int i, int j, int cnt){
if(flag == true)
return;
if((maze[i][j] == 'D') &&  (cnt == T)){
flag = true;
return;
}
if(cnt > T)
return;
int temp = T - cnt - abs(i - ex) - abs(j - ey);//奇偶剪枝的关键
if((temp < 0) || (temp & 1))
return;
for(int k = 0; k < 4; k++){
int dx = i + dir_x[k];
int dy = j + dir_y[k];
if((dx >= 0) && (dx < N) && (dy >= 0) && (dy < M) && (maze[dx][dy] != 'X') && (!visited[dx][dy])) {
visited[dx][dy] = true;
dfs(dx, dy, cnt+1);
visited[dx][dy] = false;
}
}
return;
}

int main(){
while(cin >> N >> M >> T){
if(N == 0 && M == 0 && T == 0)
break;
int xnum = 0;//X即墙的数目
memset(visited, false, sizeof(visited));
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
cin >> maze[i][j];
if(maze[i][j] == 'S'){
sx = i;
sy = j;
}
if(maze[i][j] == 'D'){
ex = i;
ey = j;
}
if(maze[i][j] == 'X'){
xnum++;
}
}
}
if((N * M - xnum) <= T){//可通过的格子数小于T,狗肯定没法活
cout << "NO" << endl;
continue;
}
flag = false;
visited[sx][sy] = true;//起点视为以访问过
dfs(sx, sy, 0);
if(flag == true)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: