您的位置:首页 > 其它

HDU1010-Tempter of the Bone(DFS模板,例题)

2014-08-07 21:06 441 查看
原博客链接:http://www.cnblogs.com/Dreamcaihao/archive/2013/05/25/3099216.html


HDU 1010 Tempter of the Bone(DFS)

Tempter of the Bone

       Time Limit: 2000/1000 MS (Java/Others) Memory
Limit: 65536/32768 K (Java/Others)

              Total Submission(s): 49637 Accepted Submission(s):
13320
[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

#include <stdio.h>
#include <string.h>
#include <math.h>

bool flag;
int  si, sj;
int  ei, ej;
int  N, M, T;
char maze[10][10];
int  dir[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}};

void dfs(int curX, int curY, int curT)
{
if(flag)return ;       //如果找到出口,直接返回
if(curT > T)return ;   //如果当前花费的时间大于T,就返回

//花费的时间加上剩余需要的时间若大于T,返回
if(curT + abs(ei-curX) + abs(ej-curY) > T)return ;

//若到达出口最短距离和剩余时间的奇偶性不同,返回
if( (abs(ei-curX) + abs(ej-curY))%2 != abs(T-curT)%2 )return;

if(curX == ei && curY == ej && curT == T)//找到出口
{
printf("YES\n");
flag = true;
return ;
}

for(int k = 0; k < 4; k++)
{
int nextX = curX + dir[k][0];
int nextY = curY + dir[k][1];

if(nextX >= 0 && nextX < N && nextY >= 0 && nextY < M)//在迷宫的范围内
{
if(maze[nextX][nextY] != 'X')
{
maze[nextX][nextY] = 'X';  //使其不可达到
dfs(nextX, nextY, curT + 1);
maze[nextX][nextY] = '.';  //回溯时,恢复原来的状态
}
}
}
}

int main()
{
while(scanf("%d %d %d", &N, &M, &T) && M+N+T)
{
int k = 0;
getchar();
flag = false;
memset(maze, 0, sizeof(maze));
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
scanf("%c", &maze[i][j]);
if(maze[i][j] == 'S')
{
si = i; sj = j;
maze[i][j] = 'X';  //将开始位置置为不可达状态
}
if(maze[i][j] == 'D')
{
ei = i; ej = j;
}
if(maze[i][j] != 'X')
k++;
}
getchar();
}
if(k < T)            //若可走的点数小于T,则输出NO
{
printf("NO\n");
continue;
}

dfs(si, sj, 0);

if(!flag)
printf("NO\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: