您的位置:首页 > 其它

HDOJ_ACM_Tempter of the Bone

2013-04-18 18:59 204 查看
[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


[align=left]Code[/align]

View Code

#include <stdio.h>
#include <math.h>
#define N 7
#define M 7
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
char map[N + 5][M +5];
int n, m, t, flag, desx, desy;
void DFS(int sx, int sy, int step)
{
int i, nx, ny, temp;
//the exit condition
if (flag == 1)
return;
//pruning stategy, one is shortest path, the other is odd-even pruning
temp = (t - (step - 1)) - abs(desx - sx) - abs(desy - sy);
if (temp < 0 || temp & 1)
return;
for (i = 0; i < 4; i++)
{
nx = sx + dx[i];
ny = sy + dy[i];
//out of index
if (nx < 0 || nx >= n || ny < 0 || ny >= m)
continue;
//find it
if (map[nx][ny] == 'D' && step == t)
{
flag = 1;
return;
}
//recursion
if (map[nx][ny] == '.')
{
map[nx][ny] = 'X';
DFS(nx, ny, step + 1);
map[nx][ny] = '.';
}
}
return ;
}
int main()
{
int i, j, sx, sy, countP;
while (scanf("%d %d %d",&n, &m, &t) != EOF && (n || m || t))
{
countP = 0;
flag = 0;
for (i = 0; i < n; i++)
{
scanf("%s", map[i]);
for (j = 0; j < m; j++)
{
if (map[i][j] == 'S')
{
sx = i;
sy = j;
}
if (map[i][j] == 'D')
{
desx = i;
desy = j;
}
if (map[i][j] == '.')
countP++;
}
}
//pruning stategy
if (countP + 1 < t)
{
puts("NO");
continue;
}
DFS(sx, sy, 1);
if (flag == 1)
puts("YES");
else
puts("NO");

}
return 0;
}


[align=left]Ideas[/align]
I think Actually, there is other pruning strategy, I can use BFS to pretreatment to get the shortest path. On the other hand, I should learn how to using circle to implement DFS.

[align=left]Recommend[/align]
JGShining

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: