HDU 1010 Tempter of the Bone

来源:互联网 发布:js判断偶数 编辑:程序博客网 时间:2024/06/12 00:02

HDU 1010 Tempter of the Bone


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


一条小狗去捡一个骨头,进入了一个迷宫里,然后就出不去了,而且小狗一到达一个block,就需要立马离开,不然的话就会掉下去。而且迷宫的门只有在t秒时才打开,也就是说小狗必须在t秒恰好到达门口。

使用深搜一步步走就行了,但是会TLE。。。。(⊙ ︿ ⊙)

怎么办呢?这就需要枝减了~
什么情况下需要枝减呢
1. 当前已经走的步数加上当前位置到终点的曼哈顿距离(所谓曼哈顿距离,就是两点横纵坐标之差的绝对值之和。。(@﹏@)~晕了吧简单的说就是( |x1-x2| + |y1 - y2|))大于所给时间t。
but。。。仅仅这样还是会TLE的<(-︿-)>
那就需要另外的枝减条件喽~

2 当前已经走的步数就上曼哈顿距离需要和所给时间同奇同偶。

这两个条件下来,一下减去很多无用的树枝

这样就不会TLE了~O(∩_∩)O~


 #include<iostream> #include <cstdio> #include <math.h> #include <string.h>int n, m, time, sx, sy, ex, ey;char a[9][9];int vis[9][9];int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};bool flag;using namespace std;inline int mah(int x, int y){    return abs(x - ex) + abs(y - ey);}void dfs(int x, int y, int t){    if (flag)    {        return ;    }    if (t > time)    {        return ;    }    if (x < 0 || x >= n || y < 0 || y >= m )    {        return ;    }    if (((time + t + mah(x, y)) & 1))    {        return ;    }    if (a[x][y] == 'D' && t == time)    {        flag = true;        return ;    }    for (int i = 0; i < 4; i++)    {        int nx = x + dir[i][0];        int ny = y + dir[i][1];        if(!vis[nx][ny]&&a[nx][ny] != 'X')         {            vis[nx][ny]=true;            dfs(nx, ny, t+1);            vis[nx][ny]=false;        }    }}int main(){ #ifndef ONLINE_JUDGE    freopen("1.txt", "r", stdin); #endif    int i, j, k;    while(scanf("%d%d%d", &n, &m, &time), n)    {        memset(vis, 0, sizeof(vis));        k = 0;        for (i = 0; i < n; i++)        {            scanf("%s", a[i]);        }        for(i = 0; i < n; i++)        {            for (j = 0; j < m; j++)            {                //cin >> a[i][j];                if (a[i][j] == 'S')                {                    sx = i;                    sy = j;                    vis[i][j] = 1;                }                if (a[i][j] == 'D')                {                    ex = i;                    ey = j;                }                if(a[i][j] == 'X')                {                    k++;                }            }        }        flag = false;        if(n * m - k - 1 >= time)        {            dfs(sx, sy, 0);        }        if (flag)        {            cout << "YES" << endl;        }        else        {            cout << "NO" << endl;        }    }    return 0;}
0 0
原创粉丝点击