`
Dev|il
  • 浏览: 121823 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

HDU1010Tempter of the Bone

 
阅读更多
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26363    Accepted Submission(s): 7218


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

题意:意思是在地图中S代表起始点,每个点只能停留1秒,便会沉落,而D代表门的所在,将在t秒后打开然后关闭,问狗是否能安全逃出迷宫
//剪枝:当格子数小于t时候,不必搜索
AC代码:
#include <iostream>
#include <queue>
using namespace std;

char map[8][8];
int visit[8][8];
int n, m, t;
int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
typedef struct{
    int x, y, t;
}Block;

Block s_n, e_n;

bool dfs(Block temp)
{
    int i, cnt;
//    cout<<"x = "<<temp.x<<"  y = "<<temp.y<<"  t="<<temp.t<<endl;
    if(map[temp.x][temp.y] == 'D' && temp.t == t)
        return true;
    cnt = (t - temp.t) - abs(temp.x - e_n.x) - abs(temp.y - e_n.y);
    if(cnt < 0 || cnt & 1)
        return false;
    for(i = 0; i < 4; i++)
    {
        Block cur;
        cur.x = temp.x + d[i][0];
        cur.y = temp.y + d[i][1];
        cur.t = temp.t + 1;
        if(cur.t > t)
            continue;
        if(cur.x >= 0 && cur.x < n && cur.y >=0 && cur.y < m &&
             (map[cur.x][cur.y] == '.' || map[cur.x][cur.y] == 'D') && !visit[cur.x][cur.y])
        {

            visit[cur.x][cur.y] = 1;
            if(dfs(cur))
                return true;
            visit[cur.x][cur.y] = 0;
        }
    }
    return false;
}
int main()
{
    int i, j, wall;
    while(cin>>n>>m>>t)
    {
        wall = 0;
        if(!n && !m && !t)
            break;
        for(i = 0; i < n; i++)
            for(j = 0; j < m; j++)
            {
                cin>>map[i][j];
                if(map[i][j] == 'S')
                {
                    s_n.x = i; s_n.y = j;
                }else if(map[i][j] == 'D')
                {
                    e_n.x = i; e_n.y = j;
                }else if(map[i][j] == 'X')
                {
                    wall++;
                }
            }
        if(n * m - wall < t)
        {
            cout<<"NO"<<endl;
            continue;
        }
        s_n.t = 0;
        memset(visit, 0, sizeof(visit));
        if(dfs(s_n))
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics