DFS 奇偶剪枝 HDU 1010

来源:互联网 发布:久久网络 编辑:程序博客网 时间:2024/05/16 10:14

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 120609    Accepted Submission(s): 32558


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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
 

Sample Output
NOYES


分析:这个题目打眼一看是dfs,虽然ygy推荐给我时我就知道是奇偶剪枝了,还是按照正常的dfs做的,交了一下,超时了,莫名的高兴,然后去看了一下有关就剪枝的百科,

剪枝后就过了。但是还不够快800多ms,于是又改了一下。


代码如下:


#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int dir[][2]={-1,0,1,0,0,-1,0,1};char map[10][10];int vi[10][10];int N,M,T;int flag;int judge(int x,int y){    if(x<0 || y<0 || x>N-1 || y>M-1 || vi[x][y] || map[x][y]=='X')        return 0;    return 1;}void dfs(int x,int y,int t){    if(t>T)        return ;    if(map[x][y]=='D' && t==T)    {        printf("YES\n");        flag=1;        return ;    }    else    {        for(int i=0;i<4;i++)        {            int x1=x+dir[i][0]; int y1=y+dir[i][1];            if(judge(x1,y1))            {                vi[x1][y1]=1;                dfs(x1,y1,t+1);                vi[x1][y1]=0;                if(flag)    //flag判断放在此处会更快一些                    return ;            }        }    }}int ff(int si,int sj,int ei,int ej){    int tt;    tt=abs(ei-si)+abs(ej-sj);    if( tt>T || (T-tt)%2==1)    //此处如0果先判断(T-tt)%2==1会很慢,将近300ms但是个人认为只要加了该有的判断及时再慢也不会超过1000ms        return 1;    return 0;}int main(){    int si,sj,ei,ej;    while(scanf("%d%d%d",&N,&M,&T),N+M+T)    {        memset(vi,0,sizeof(vi));        flag=0;        for(int i=0;i<N;i++)        {            getchar();            for(int j=0;j<M;j++)            {                scanf("%c",&map[i][j]);                if(map[i][j]=='S')                {                    si=i; sj=j;                }                if(map[i][j]=='D')                {                    ei=i; ej=j;                }            }        }        getchar();        if(ff(si,sj,ei,ej))        {            printf("NO\n");            continue ;        }        vi[si][sj]=1;        dfs(si,sj,0);        if(!flag)            printf("NO\n");    }    return 0;}





0 0
原创粉丝点击