`

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): 24023    Accepted Submission(s): 6616

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 Input
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
Sample Output
NO YES
 

      这题与普通的走迷宫有些不相同之处:1.规定那个时间到达(不是规定时间内到达);2.走过的路不能再走。第一次做超时了,要剪枝,后来想到有一个重要剪枝---奇偶剪枝!写了就AC了。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010

代码:

#include <iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;

bool step[8][8];
char ch[15][15];
int n, m, s, flag;

void init()
{
    flag = 0;
    memset(step, false, sizeof(step));
}

void dfs(int x, int y, int t)
{
    if(flag) return;                    //若已经找到,返回
    if(x<0 || y<0 || x>=n || y>=m) return;  //若越界,返回
    if(t > s) return;       //若时间已经超过出现时间,返回
    if(ch[x][y] == 'X') return;             //若是墙,返回
    if(ch[x][y] == 'D')
    {
        if(t==s)    //若时间刚刚好
        {
            flag = 1;
            return;
        }
        //重要剪枝:去到'D'点是偶数(奇数)步,那它出来的时间一定要偶数(奇数)才能去到
        if(t%2 != s%2)
        {
            flag = 2;
            return;
        }
    }
    if(step[x][y]) return;  //若该点已经走过,则返回
    step[x][y] = true;      //否则,更新该点为走过,并开始搜索

    //往4个方向进行dfs深搜
    dfs(x+1, y, t+1);
    dfs(x-1, y, t+1);
    dfs(x, y+1, t+1);
    dfs(x, y-1, t+1);

    step[x][y] = false;     //若该点是走不通的,更新该点为未走过,往上一步退

}

int main()
{
    int i, j;
    while(scanf("%d %d %d", &n, &m, &s) != EOF)
    {
        if(!m && !n && !s) break;
        init();
        for(i = 0; i < n; i++)
            for(j = 0; j < m; j++)
                cin>>ch[i][j];
        for(i = 0; i < n; i++)
            for(j = 0; j < m; j++)
                if(ch[i][j] == 'S')
                    dfs(i, j, 0);   //找到入口,开始搜索

        if(flag == 1) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}

 

3
16
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics