hdu 1010 Tempter of the Bone (dfs + 剪枝)

来源:互联网 发布:办卡软件 编辑:程序博客网 时间:2024/06/05 15:28
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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
Sample Output
NOYES
从S出发每次只能移动一格,且不能走回头路,问能否恰好经过t步到达D
有一种很强的剪枝办法,就是判断在每个位置时距终点的最短步数的奇偶性与剩余步数的奇偶性是否相同,如果相同就继续搜索,否则返回;
#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;const int M = 10;char mapp[M][M];bool book[M][M];int sx, sy, dx, dy, n, m, t;int cnt;bool f;int mv[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};void dfs(int x, int y){    if(f)        return;    int dis = abs(dx-x)+abs(dy-y);//求出最短步数    if(dis%2!=(t-cnt)%2)//判断奇偶        return;    if(x==dx&&y==dy)    {        if(cnt==t)            f = true;        return;    }    int tx, ty;    for(int i=0;i<4;i++)    {        tx = x + mv[i][0];        ty = y + mv[i][1];        if(tx<1||tx>n||ty<1||ty>m)            continue;        if(mapp[tx][ty]!='X'&&!book[tx][ty])        {            book[tx][ty] = true;            cnt++;            dfs(tx, ty);            cnt--;            book[tx][ty] = false;        }    }}int main(){    while(scanf("%d%d%d", &n, &m, &t)&&(n||m||t))    {        f = false;        memset(book, false, sizeof(book));        cnt = 0;        for(int i=1;i<=n;i++)            for(int j=1;j<=m;j++)            {                scanf(" %c", &mapp[i][j]);                if(mapp[i][j]=='S')                {                    sx = i;                    sy = j;                }                else if(mapp[i][j]=='D')                {                    dx = i;                    dy = j;                }            }        book[sx][sy] = true;        dfs(sx, sy);        if(f)            printf("YES\n");        else            printf("NO\n");    }    return 0;}