hdu1010 Tempter of the Bone

本文介绍了一个关于迷宫中狗寻找出路的问题,通过特定的时间限制和迷宫布局,使用深度优先搜索算法来解决这一挑战。文章详细阐述了如何利用奇偶性进行剪枝优化,以减少搜索空间并提高算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tempter of the Bone

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


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

 

这里涉及到了奇偶性的剪枝

从0->1或者1->0都是走奇数步,如果时间限制是偶数的话,那么这条路就可以直接pass掉。

从0->0或者1->1都是走偶数步,如果时间限制是奇数的话,那么这条路就可以直接pass掉。

这里是相当重要的剪枝。

 

</pre><pre>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#define mes(x) memset(x, 0, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
typedef long long ll;
using namespace std;
const int MAX = 10;
#define MOD 1000000007
#define test
#define time
char mp[8][8];
int flag;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int si, sj, di, dj, n, m, _time, tmp, wall;
void dfs(int si, int sj, int cnt)
{
    if(si > n || si <= 0 || sj > m || sj <= 0){ //判断越界
        return;
    }
    if(_time == cnt && si == di && sj == dj){
        flag = 1;
    }
    if(flag == 1){  //判断胜利
        return;
    }
    //pruning
    tmp = _time - cnt - (abs(si - di) + abs(sj - dj));// 奇偶性剪枝
    if(tmp < 0 || tmp & 1){
        return;
    }
    for(int i = 0; i < 4; ++i){
        if(mp[si+dx[i]][sj+dy[i]] != 'X'){
            mp[si+dx[i]][sj+dy[i]] = 'X';
            dfs(si+dx[i], sj+dy[i], cnt+1);
            mp[si+dx[i]][sj+dy[i]] = '.';  //回溯
        }
    }
    return;
}
int main()
{
#ifdef test
    freopen("/home/ostreambaba/文档/input.txt", "r", stdin);
 // freopen("/home/ostreambaba/文档/input.txt", "w", stdout);
#endif
    while(~scanf("%d%d%d", &n, &m, &_time))
    {
        cin.ignore();
        if(0 == n && 0 == m && 0 == _time){
            break;
        }
        wall = 0;
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= m; ++j){
                //cin >> mp[i][j];
                scanf("%c", &mp[i][j]);
                if(mp[i][j] == 'S'){
                    si = i;
                    sj = j;
                }
                else if(mp[i][j] == 'D'){
                    di = i;
                    dj = j;
                }
                else if(mp[i][j] == 'X'){
                    ++wall;
               }
            }
            cin.ignore();
        }
        if(n*m - _time <= wall){ //小剪枝
            cout << "NO" << endl;
            continue;
        }
        flag = 0;
        mp[si][sj] = 'X';
        dfs(si, sj, 0);
        if(flag == 1){
            cout << "YES" << endl;
        }
        else{
            cout << "NO" << endl;
        }
    }
    return 0;
}

 

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define inf 0x3f3f3f3f
#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const int maxn = 1000+10;
using namespace std;

inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int mp[8][8];
int si,sj,di,dj;
int n,m,cnt;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
bool flag;
bool vis[8][8];
void dfs(int cnt,int x,int y){
    if(x<1||x>n||y<1||y>m||mp[x][y]==1){
        return;
    }
    if(x==di&&y==dj&&cnt==0){
        flag=true;
    }
    if(flag){
        return;
    }
    int tmp=cnt-abs(di-x)-abs(dj-y);
    if(tmp<0||tmp&1){
        return;
    }
    for(int i=0;i<4;++i){
        if(!vis[x+dx[i]][y+dy[i]]){
            vis[x+dx[i]][y+dy[i]]=true;
            dfs(cnt-1,x+dx[i],y+dy[i]);
            vis[x+dx[i]][y+dy[i]]=false;
        }
    }
    return;
}
int main(){
    //freopen("/home/ostreambaba/文档/input.txt", "r", stdin);
    //freopen("/home/ostreambaba/文档/output.txt", "w", stdout);
    char st[9];
    while(~scanf("%d%d%d",&n,&m,&cnt),n,m,cnt){
        mes(mp,0);
        mes(vis,false);
        flag=false;
        int sum=n*m;
        int cotX=0;
        for(int i=1;i<=n;++i){
            scanf("%s",st);
            for(int j=0;j<m;++j){
                if(st[j]=='S'){
                    si=i,sj=j+1;
                }else if(st[j]=='D'){
                    di=i,dj=j+1;
                }else if(st[j]=='X'){
                    ++cotX;
                    mp[i][j+1]=1;
                }
            }
        }
        if(sum-cotX-1<cnt){
            puts("NO");
            continue;
        }
        vis[si][sj]=true;
        dfs(cnt,si,sj);
        if(flag){
            puts("YES");
        }else{
            puts("NO");
        }
    }
    return 0;
}

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值