DFS(剪枝)_hdu_1010

自第一次接触搜索到现在一年多了,搜索一直是我引以为傲的,没想到今天栽在这一题上

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010

Tempter of the Bone

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


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
题意很简单给定一个n*m的迷宫,和一个t;可以在迷宫中生存的最大时间为t。S为起点,D为终点。并且,每个格子只能踩一次,每走一步耗时为1,每个格子只能走一次,且到D点时,所用时间为T。没多想,7*7而已,直接深搜。居然超时了,不可思议。7*7而已。想办法优化,分析&参考其他的思路之后,没TLE居然是WA。该题输入数据可能有问题。

AC代码:

<pre name="code" class="cpp"><pre name="code" class="cpp"><span style="color:#362e2b;">#include<stdio.h>
#include<string.h>
int map[10][10],n,m,X,Y;
short mark[10][10],Mark;
int Abs(int x)
{
    return x>0?x:-x;
}
void visit(int x,int y,int t)
{
    int temp;
    if(Mark)return;
    if(x==X&&y==Y&&t==0){Mark=1;return;}
    temp=t-Abs(x-X)-Abs(y-Y);
    if(temp<0||temp&1)return;//非常重要的剪枝
    /*********
    *就是当前剩下的时间与当前位置到目的地的距离的差如果小于0
    *则肯定没办法到达
    *如果为奇数,一样不可能在要求的时间到达
    ************************************************************/
    if(map[x-1][y]&&mark[x-1][y]==0&&t)//向上
    {
        mark[x-1][y]=1;
        visit(x-1,y,t-1);
        mark[x-1][y]=0;
    }
    if(map[x+1][y]&&mark[x+1][y]==0&&t)//向下
    {
        mark[x+1][y]=1;
        visit(x+1,y,t-1);
        mark[x+1][y]=0;
    }
    if(map[x][y-1]&&mark[x][y-1]==0&&t)//向左
    {
        mark[x][y-1]=1;
        visit(x,y-1,t-1);
        mark[x][y-1]=0;
    }
    if(map[x][y+1]&&mark[x][y+1]==0&&t)//向右
    {
        mark[x][y+1]=1;
        visit(x,y+1,t-1);
        mark[x][y+1]=0;
    }
}
int main()
{
    int i,j,x,y,t;
    char c;
    while(</span><strong><span style="color:#ff0000;">scanf("%d%d%d%*c",&n,&m,&t)</span></strong><span style="color:#362e2b;">,n+m+t)
    {
        Mark=0;
        memset(map,0,sizeof(map));
        memset(mark,0,sizeof(mark));
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                scanf("%c",&c);
                if(c=='.')map[i][j]=1;
                else if(c=='S'){x=i;y=j;}
                else if(c=='D'){X=i;Y=j;map[i][j]=1;}
            }
            </span><strong><span style="color:#ff0000;">getchar();</span></strong><span style="color:#362e2b;">
        }
        visit(x,y,t);
        printf(Mark?"YES\n":"NO\n");
    }
    return 0;
}
</span>


 

 
WA代码: 

<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
int map[10][10],n,m,X,Y;
short mark[10][10],Mark;
int Abs(int x)
{
    return x>0?x:-x;
}
void visit(int x,int y,int t)
{
    int temp;
    if(Mark)return;
    if(x==X&&y==Y&&t==0){Mark=1;return;}
    temp=t-Abs(x-X)-Abs(y-Y);
    if(temp<0||temp&1)return;//非常重要的剪枝
    if(map[x-1][y]&&mark[x-1][y]==0&&t)//向上
    {
        mark[x-1][y]=1;
        visit(x-1,y,t-1);
        mark[x-1][y]=0;
    }
    if(map[x+1][y]&&mark[x+1][y]==0&&t)//向下
    {
        mark[x+1][y]=1;
        visit(x+1,y,t-1);
        mark[x+1][y]=0;
    }
    if(map[x][y-1]&&mark[x][y-1]==0&&t)//向左
    {
        mark[x][y-1]=1;
        visit(x,y-1,t-1);
        mark[x][y-1]=0;
    }
    if(map[x][y+1]&&mark[x][y+1]==0&&t)//向右
    {
        mark[x][y+1]=1;
        visit(x,y+1,t-1);
        mark[x][y+1]=0;
    }
}
int main()
{
    int i,j,x,y,t;
    char c;
    while(<strong><span style="color:#ff0000;">scanf("%d%d%d",&n,&m,&t)</span></strong>,n+m+t)
    {
        Mark=0;
        memset(map,0,sizeof(map));
        memset(mark,0,sizeof(mark));
        for(i=1;i<=n;i++)
        {
            <strong><span style="color:#ff0000;">getchar();</span></strong>
            for(j=1;j<=m;j++)
            {
                scanf("%c",&c);
                if(c=='.')map[i][j]=1;
                else if(c=='S'){x=i;y=j;}
                else if(c=='D'){X=i;Y=j;map[i][j]=1;}
            }
        }
        visit(x,y,t);
        printf(Mark?"YES\n":"NO\n");
    }
    return 0;
}


 

注意我加粗的部分,该题输入部分,某行可能有多余的空格

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值