HDU1010 Tempter of the Bone -奇偶剪枝

来源:互联网 发布:linux好看的桌面 知乎 编辑:程序博客网 时间:2024/05/12 06:41
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

奇偶剪枝 http://baike.baidu.com/link?url=GPYaFuIuUZ4m2mLfmEeWVuPeUNbhIvcVlid9XxAdZ-z9aZ-OTSnOMxb-ClmvAiN-6Wmy0Vn0XUsr5OBs6viPgq

测试数据:

7 7 26
.....X.
XSXX..X
..X.X.X
..X...X
..X..X.
..X....
..X...D
4 4 12
....
.SX.
..X.
..XD


#include <stdio.h>#include <string.h>#include <stdlib.h>int fuck,n,m,max,di,dj,t;char maze[10][10];int z[4][2]={{-1,0},{1,0},{0,-1},{0,1}};int find(int i,int j,int step){    if(fuck) return 0;    if(maze[i][j]=='X') return 0;    if(step>t) return 0;    if(t-step<abs(i-di)+abs(j-dj)) return 0;    if((t-step-abs(i-di)+abs(j-dj))%2) return 0;        //奇偶剪枝    if(maze[i][j]=='D'&&step==t)    {        fuck=1;        return 0;    }    if(maze[i][j]=='.')    for(int x=0;x<4;++x)    {        maze[i][j]='X';        find(i+z[x][0],j+z[x][1],step+1);        if(fuck) return 0;          //非常重要的一个剪枝。省去约三分之一的时间        maze[i][j]='.';    }    return 0;}int main(){    int si,sj,x,num;    while(scanf("%d%d%d",&n,&m,&t)!=EOF,n||m||t)    {        getchar();        num=0;        memset(maze,'X',sizeof(maze));        for(int i=2;i<n+2;++i)        {            for(int j=2;j<m+2;++j)            {                scanf("%c",&maze[i][j]);                if(maze[i][j]=='S') si=i,sj=j,maze[i][j]='.';                if(maze[i][j]=='D') di=i,dj=j;            }            getchar();        }        fuck=0; //       printf("%d\n",min);        if((t-abs(si-di)+abs(sj-dj))%2||t<abs(si-di)+abs(sj-dj)) printf("NO\n");        else        {            find(si,sj,0);            if(fuck) printf("YES\n");            else printf("NO\n");        }    }    return 0;}


神的代码:


#include <cstdio>#include <cstring>#include <iostream>#include <stdlib.h>using namespace std;char map[9][9];int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};int n,m,t,sum;int sx,sy,ddx,ddy;bool dfs(int x,int y,int time){    int px,py;    if(x==ddx && y==ddy && time==t)    {        return true;//正确    }    else    {        if(abs(ddx-x)+abs(ddy-y)>t-time)return false;//小剪枝        for(int i=0;i<4;i++)        {            px=x+dx[i];            py=y+dy[i];            if(map[px][py]!='X' && time+1<=t)            {                map[px][py]='X';                if(dfs(px,py,time+1))return true;//这句话是强力剪枝                map[px][py]='.';            }        }    }    return false;}int main(){    while(scanf("%d%d%d",&n,&m,&t),n || m || t)    {        memset(map,'X',sizeof(map)); //利用扩大矩阵省去麻烦的边界判断        sum=0;        getchar();//把回车读掉        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                scanf("%c",&map[i][j]);                if(map[i][j]=='S')                {                    sx=i;                    sy=j;                }                if(map[i][j]=='D')                {                    ddx=i;                    ddy=j;                }                if(map[i][j]=='.')sum++;            }            getchar();        }        if(sum+1<t || (t+sx+sy+ddx+ddy)%2==1)printf("NO\n"); //奇偶性判别与可达性判别        else        {            map[sx][sy]='X';            if(dfs(sx,sy,0))printf("YES\n");            else printf("NO\n");        }    }    return 0;}


0 0
原创粉丝点击