HDU1010 Tempter of the Bone(深度优先搜索DFS+奇偶性剪枝)

题目:

Tempter of the Bone

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


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
 

Author
ZHANG, Zheng
 

代码:

题意:给了一个n*m的地图,让在第t步走完,求是否可行

#include <stdio.h>
#include <string.h>
#include <cmath>
#include <algorithm>
using namespace std;
char map[10][10];
int n,m,t;
int wall=0,di,dj,flag;
int go[4][2]=
{
    {0,-1},//向后
    {0,1},//向前
    {-1,0},//向左
    {1,0}//向右
};
void dfs(int ssi,int ssj,int ans)
{
    int i,temp;
    if(ssi>n||ssj>m||ssi<=0||ssj<=0)//判断是否越界
        return;
    if(ans==t&&ssi==di&&ssj==dj)//判断是否到达终点
        flag=1;
    if(flag) return;
    int s1=ssi-di;
    int s2=ssj-dj;
    if(s1<0)s1=-s1;
    if(s2<0)s2=-s2;
    temp=(t-ans)-(s1+s2);
    if(temp<0||temp%2==1)return;//奇偶性剪枝,详见博客下文
    for(i=0; i<4; i++)
    {
        if(map[ssi+go[i][0]][ssj+go[i][1]]!='X')
        {
            map[ssi+go[i][0]][ssj+go[i][1]]='X';//走过的标记为墙
            dfs(ssi+go[i][0],ssj+go[i][1],ans+1);//深搜DFS
            map[ssi+go[i][0]][ssj+go[i][1]]='.';//标记回来,迷宫还原,以便下次深搜
        }
    }
    return;
}

int main()
{
    int si,sj;
    while(1)
    {
        scanf("%d%d%d%*c",&n,&m,&t);
        if(n==0&&m==0&&t==0)return 0;
        wall=0;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                scanf("%c",&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++;//记录墙的数量
            }
            getchar();//因为输入的是%c所以要吸收字符
        }
        if(n*m-wall<=t)//t是代表要走的步数,步数加墙数必须小于总格子数的,因为所有格子中还包括了S和D,这是剪枝
        {
            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;
}

附上一个杭电LCY奇偶性剪枝原理图:


其中t-anst味剩余的步数或者说时间,另其为T

s1+s2为剩余步数,另其为S

如果走偶数步要求的时间是奇数,或者走奇数步要求的时间是偶数,都明显不可行

而轻易得出奇数-偶数 = 奇数,反之亦然

而奇数-奇数= 偶数,偶数-偶数=偶数

所以temp必须为偶

------------------------------------------------------------------------------

2017年7月10日17:52:07重新写了一下,理解又加深了


#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <iostream>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 10000007
#define debug() puts("what the fuck!!!")
#define N (1010000)
#define ll long long
using namespace std;
int n,m,t,sx,sy,ex,ey,flag;
char map[60][60];
int vis[60][60];
int go[4][2]= {0,1,0,-1,1,0,-1,0};
void dfs(int x,int y,int step)
{
	if(flag)return;
	if(x==ex&&y==ey&&step==t)
	{
		flag=1;
		return;
	}
	int s1=x-ex;
	int s2=y-ey;
	if(s1<0)s1=-s1;
	if(s2<0)s2=-s2;
	int temp=(t-step)-(s1+s2);
	if(temp<0||temp&1)return;
	for(int i=0; i<4; i++)
	{
		int xx=x+go[i][0];
		int yy=y+go[i][1];
		if(!vis[xx][yy]&&map[xx][yy]!='X'&&xx>=0&&xx<n&&yy>=0&&yy<m)
		{
			vis[xx][yy]=1;
			dfs(xx,yy,step+1);
			vis[xx][yy]=0;
		}
	}
}
int main()
{
	while(~scanf("%d%d%d",&n,&m,&t)&&(n||m||t))
	{
		int wall=0;
		mem(map,'\0');
		mem(vis,0);
		for(int i=0; i<n; i++)
		{
			scanf("%s",map[i]);
			for(int j=0; j<m; j++)
			{
				if(map[i][j]=='S')
				{
					sx=i;
					sy=j;
				}
				if(map[i][j]=='D')
				{
					ex=i;
					ey=j;
				}
				if(map[i][j]=='X')
					wall++;
			}
		}
		if(n*m-wall<=t)
		{
			puts("NO");
			continue;
		}
		flag=0;
		vis[sx][sy]=1;
		dfs(sx,sy,0);
		if(flag)
			puts("YES");
		else
			puts("NO");
	}
	return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值