现在的位置: 首页 > 综合 > 正文

hdu1010——Tempter of the Bone(DFS+剪枝)

2013年11月29日 ⁄ 综合 ⁄ 共 5092字 ⁄ 字号 评论关闭
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

分析:

 

题意:这个题目的意思是给定你起点S,和终点D,问你是否能在 T 时刻恰好到达终点D。
分析:这样一看很明显是DFS,不过里面涉及到很多剪枝。
 
奇偶剪枝:
是数据结构的搜索中,剪枝的一种特殊小技巧。
现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,
 
s        
|        
|        
|        
+ e
 
如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数,记做step,此处step1=8;
  
s  
  +  
| +      
|        
+ e
 
如图,为一般情况下非最短路径的任意走法举例,step2=14;
step2-step1=6,偏移路径为6,偶数(易证);
故,若t-[abs(ex-sx)+abs(ey-sy)]结果为奇数,则无法在t步恰好到达;
返回,false;
反之亦反。

解题思路:这道题,要用到剪枝搜索来做,否则会超时。剪掉的条件是,如果可走地板数目小于给定的时间,绝对不可能得救。还有就是狗走到门的时间必须和题目给定的时间是同奇同偶的,否则也不能在指定的那秒到达门,也不可能得救,剪掉这两种情况后。就用DFS来做。从起点出发,DFS周围的路,走过的路就标记为不可走,一只搜索下去,如果是一条不归路(就是活不了啦)就回溯,把可能的路都搜索一遍过去,看看是否有可行方案。

源码:(TLE)——未剪枝、未加优化

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,t;
char map[10][10];
int flag;
int di,dj,wall;
int i,j,si,sj;
int to[4][2] = {-1,0,1,0,0,-1,0,1};
void dfs(int si,int sj,int cnt)//深搜
{
    if(si>=n || sj>=m || si<0 || sj < 0)//出界
        return ;
    if(cnt == t && si == di && sj == dj)//到达终点
        flag = 1;
    if(flag)
        return ;
    for(int i = 0; i<4; i++)
    {
        int nextx=si+to[i][0];
        int nexty=sj+to[i][1];
        if(nextx>=0&&nextx<m&&nexty>=0&&nexty<n&&map[nextx][nexty]!='X')        {
            map[nextx][nexty]='X';//走过的地方变为墙
            dfs(nextx,nexty,cnt+1);
            map[nextx][nexty]='.';//迷宫还原,以便下次广搜
        }
    }
    return ;
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&t)&&(n!=0||m!=0||t!=0))
    {
        getchar();
        wall = 0;
        for(i = 0; i<n; i++)
            for(j = 0; j<m; j++)
            {
                cin>>map[i][j];
                if(map[i][j] == 'S')
                {
                    si = i;
                    sj = j;
                    //标记起点
                }
                else if(map[i][j] == 'D')
                {
                    di = i;
                    dj = j;
                    //标记终点
                }
                else if(map[i][j] == 'X')
                    wall++;//对“#计数”
            }
               flag = 0;
        map[si][sj] = 'X';//出发点是不可能再走的了,变为墙
        dfs(si,sj,0);
        if(flag)
        printf("YES\n");
        else
            printf("NO\n");
        }
return 0;
}

 

剪枝优化后——Accept

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,t;
char map[10][10];
int flag;
int di,dj,wall;
int i,j,si,sj;
int to[4][2] = {-1,0,1,0,0,-1,0,1};
void dfs(int si,int sj,int cnt)   //深搜
{
    if(si>=n || sj>=m || si<0 || sj < 0)   //出界
        return ;
    if(cnt == t && si == di && sj == dj)  //到达终点
        flag = 1;
    if(flag)
        return ;
    for(int i = 0; i<4; i++)
    {
        int nextx=si+to[i][0];
        int nexty=sj+to[i][1];
        if(map[nextx][nexty]!='X')
        {
            map[nextx][nexty]='X';//走过的地方变为墙
            dfs(nextx,nexty,cnt+1);
            map[nextx][nexty]='.';//迷宫还原,以便下次广搜
        }
    }
    return ;
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&t)&&(n!=0||m!=0||t!=0))
    {
        getchar();
        wall = 0;
        for(i = 0; i<n; i++)
            for(j = 0; j<m; j++)
            {
                cin>>map[i][j];
                if(map[i][j] == 'S')
                {
                    si = i;
                    sj = j;
                    //标记起点
                }
                else if(map[i][j] == 'D')
                {
                    di = i;
                    dj = j;
                    //标记终点
                }
                else if(map[i][j] == 'X')
                    wall++;//对“#计数”
            }
        if(n*m-wall<=t||abs(si-di)+abs(sj-dj)>t)//t是代表要走的步数,步数加墙数必须小于总格子数的,因为所有格子中还包括了S和D,这是剪枝
        {
            printf("NO\n");
            continue;
            //abs(si-ei)+abs(sj-ej)为起点到终点的最短步数
        }
        if((abs(si-di)+abs(sj-dj))%2!=(t%2))
        {
            //狗走到门的时间必须和题目给定的时间是同奇同偶的,否则也不能在指定的那秒到达门
            printf("NO\n");
            continue;
        }
        flag = 0;
        map[si][sj] = 'X';//出发点是不可能再走的了,变为墙
        dfs(si,sj,0);
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

 

 以下代码也能通过,证明,此题主要是奇偶剪枝:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,t;
char map[10][10];
int flag;
int di,dj,wall;
int i,j,si,sj;
int to[4][2] = {-1,0,1,0,0,-1,0,1};
void dfs(int si,int sj,int cnt)   //深搜
{
    if(si>=n || sj>=m || si<0 || sj < 0)   //出界
        return ;
    if(cnt == t && si == di && sj == dj)  //到达终点
        flag = 1;
    if(flag)
        return ;
    for(int i = 0; i<4; i++)
    {
        int nextx=si+to[i][0];
        int nexty=sj+to[i][1];
        if(map[nextx][nexty]!='X')
        {
            map[nextx][nexty]='X';//走过的地方变为墙
            dfs(nextx,nexty,cnt+1);
            map[nextx][nexty]='.';//迷宫还原,以便下次广搜
        }
    }
    return ;
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&t)&&(n!=0||m!=0||t!=0))
    {
        getchar();
        wall = 0;
        for(i = 0; i<n; i++)
            for(j = 0; j<m; j++)
            {
                cin>>map[i][j];
                if(map[i][j] == 'S')
                {
                    si = i;
                    sj = j;
                    //标记起点
                }
                else if(map[i][j] == 'D')
                {
                    di = i;
                    dj = j;
                    //标记终点
                }
                else if(map[i][j] == 'X')
                    wall++;//对“#计数”
            }
       /* if(n*m-wall<=t||abs(si-di)+abs(sj-dj)>t)//t是代表要走的步数,步数加墙数必须小于总格子数的,因为所有格子中还包括了S和D,这是剪枝
        {
            printf("NO\n");
            continue;
            //abs(si-ei)+abs(sj-ej)为起点到终点的最短步数
        }*/
        if((abs(si-di)+abs(sj-dj))%2!=(t%2))
        {
            //狗走到门的时间必须和题目给定的时间是同奇同偶的,否则也不能在指定的那秒到达门
            printf("NO\n");
            continue;
        }
        flag = 0;
        map[si][sj] = 'X';//出发点是不可能再走的了,变为墙
        dfs(si,sj,0);
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

 

抱歉!评论已关闭.