HDU-1010Tempter of the Bone(dfs+奇偶剪枝)

Tempter of the Bone

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1010
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 157407 Accepted Submission(s): 41882

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


这看起来是一道送分题,但实际上是一道送命题emmmm。。。
题目大意是这样的,一条dog要走出迷宫,它不能回头,且迷宫的们准时打开,只开一次,迷宫内有障碍物。给定时间T,和dog的坐标,门的坐标,判断是否它巧好能在T时刻准点到达门口。。。
但是蒟蒻的我没有看出来,于是随便写了一下:

#include <cstdio>
#include <queue>
#include <cstring>
#define BYJ(a,b,n,m) (a>=0&&a<n&&b>=0&&b<m)
using namespace std;
char map[15][15];
int vis[15][15],dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
void dfs(int x,int y,int p);
int n,m,ans=0,t;
int main() {

    while (scanf ("%d%d%d",&n,&m,&t)) {
        if (!n && !m && !t) break;
        int sx,sy; 
        for (int i=0; i<n; i++)
            scanf ("%s",map[i]);
        memset(vis,0,sizeof(vis));
        for (int i=0; i<n; i++)
            for (int j=0; j<m; j++)
                if (map[i][j]=='S') {
                    sx=i,sy=j;
                    break;
                }
        vis[sx][sy]=1;
        dfs(sx,sy,1);
        if (ans) printf ("YES\n");
        else printf ("NO\n");
    }
    return 0;
}
void dfs(int x,int y,int p)
{
    for (int i=0; i<4; i++){
        int xx=x+dx[i],yy=y+dy[i];
        if (BYJ(xx,yy,n,m) && map[xx][yy]!='X' && !vis[xx][yy]){
            if (map[xx][yy]=='D' && p==t) {
                ans=1;return;
            }
            vis[xx][yy]=1;
            dfs(xx,yy,p+1);
            vis[xx][yy]=0;
        }
    }
}

然后果断T了,当时我很懵逼,结果改了很久。。。于是放弃了
在这里插入图片描述
T了三发之后我觉得emmm。。。该优化的我都优化了,还是别头铁了,肯定是剪枝的姿势不对。于是上网搜了波题解——(本题需要奇偶剪枝)。。。第一次听说。。。于是学了波奇偶剪枝。
这篇博客讲的很详细,我就不多说了 https://blog.csdn.net/code_pang/article/details/8839432

#include <cstdio>
#include <queue>
#include <cstring>
#define BYJ(a,b,n,m) (a>=0&&a<n&&b>=0&&b<m)
using namespace std;
char map[15][15];
int vis[15][15],dx[4]= {1,-1,0,0},dy[4]= {0,0,1,-1};
void dfs(int x,int y,int p);
int abs(int x)
{
	return x>0?x:-x;
}
int n,m,ans=0,t;
int main() {
	while (scanf ("%d%d%d",&n,&m,&t)) {
		if (!n && !m && !t) break;
		int sx,sy,fx,fy,wall=0;
		for (int i=0; i<n; i++)
			scanf ("%s",map[i]);
		memset(vis,0,sizeof(vis));
		ans=0;
		for (int i=0; i<n; i++)
			for (int j=0; j<m; j++)
				if (map[i][j]=='S') {
					sx=i,sy=j;
				}
				else if (map[i][j]=='X') wall++;
				else if (map[i][j]=='D'){
					fx=i,fy=j;
				}
		int bbb=t-(abs(fx-sx)+abs(fy-sy));
		if (n*m-wall<=t || bbb<0 || bbb%2!=0)printf ("NO\n");
		else {
			vis[sx][sy]=1;
			dfs(sx,sy,1);
			if (ans) printf ("YES\n");
			else printf ("NO\n");
		}
	}
	return 0;
}
void dfs(int x,int y,int p) {
	for (int i=0; i<4; i++) {
		int xx=x+dx[i],yy=y+dy[i];
		if (BYJ(xx,yy,n,m) && map[xx][yy]!='X' && !vis[xx][yy]) {
			if (map[xx][yy]=='D' && p==t) {
				ans=1;
				return;
			}
			vis[xx][yy]=1;
			dfs(xx,yy,p+1);
			vis[xx][yy]=0;
		}
	}
}

这份代码跑得有点慢,920ms,于是,优化一下,62ms
在这里插入图片描述
还是那句话,找到之后就一直return,不用再回溯,否则920ms就是这样了。

#include <cstdio>
#include <cstring>
#include <cmath>
void dfs(int x,int y);
int n,m,t,a[10][10],mark,sx,sy,fx,fy,time,wall;
int dx[4]= {1,0,-1,0},dy[4]= {0,1,0,-1},v[10][10];
int main() {
    while (scanf ("%d%d%d",&n,&m,&t)) {
        if (n==0 && m==0 && t==0) break;
        memset(v,0,sizeof(v));
        memset(a,0,sizeof(a));
        time=0;
        mark=0;wall=0;
        for (int i=1; i<=n; i++)
            for (int j=1; j<=m; j++) {
                int ch=getchar();
                while (ch!='X' && ch!='.' && ch!='S' && ch!='D') ch=getchar();
                if (ch=='X') wall++,a[i][j]=1;
                else if (ch=='S') sx=i,sy=j;
                else if (ch=='D') fx=i,fy=j;
            }
        int bbb=t-(abs(fx-sx)+abs(fy-sy));
        if (n*m-wall<=t || bbb<0 || bbb%2!=0)printf ("NO\n");
        else {
            v[sx][sy]=1;
            dfs(sx,sy);
            if (mark) printf ("YES\n");
            else printf ("NO\n");
        }
    }
    return 0;
}
void dfs(int x,int y) {
    if (mark) return;                     //已经找到了,不用无谓地回溯
    if (x==fx && y==fy) {
        if (time==t) mark=1;
        return;
    } else {
        for (int i=0; i<4; i++) {
            if (mark) return;
            int cx=x+dx[i],cy=y+dy[i];
            if (!v[cx][cy] && !a[cx][cy] && cx>=1 && cx<=n && cy>=1 && cy<=m) {
                v[cx][cy]=1;
                time++;
                dfs(cx,cy);
                v[cx][cy]=0;
                time--;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43906000/article/details/88294594