HDOJ-1885 Key Task 迷宫问题

本文通过BFS+状态压缩的方法解决了一个迷宫问题,重点在于如何正确处理钥匙和门的状态,确保能够顺利到达终点。文章分享了实现过程中的关键思路及遇到的一些常见错误。

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

BFS+状态压缩
跟1429很想,重敲了一遍,加深了理解吧
有些问题没理清造成了bug
1.门next.state&t 钥匙next.state|=t
有无赋值很重要,不能弄混
2.最后的else if 不光是’通道’的情况,还有出口的情况,不然出口无法入队,就无法访问,ans恒为-1

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<algorithm>
#include<iostream>

using namespace std;

const int N = 105;
const int M = 105;
const int inf = 1000000007;
const int mod = 1000000007;

int n,m,ans;
char map[N][M];
int sx,sy,ex,ey;
int vis[N][M][1<<5];

struct node{
    int x;
    int y;
    int step;
    int state;
};

int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

bool judge(int x,int y){
    if(x>=1&&x<=n&&y>=1&&y<=m &&map[x][y]!='#')return true;
    else return false;
}

void BFS(){
    memset(vis,0,sizeof(vis));
    int t;
    node a,next;
    a.x=sx;
    a.y=sy;
    a.step=0;
    a.state=0;

    queue<node> q;
    vis[a.x][a.y][a.state]=1;
    q.push(a);
    while(!q.empty()){
        a=q.front();
        q.pop();
        //cout<<map[a.x][a.y]<<" "<<a.step<<" "<<a.state<<endl;

        if(map[a.x][a.y]=='X'){
            ans=a.step;
            return;
        }

        for(int i=0;i<=3;i++){
            next.x=a.x+dir[i][0];
            next.y=a.y+dir[i][1];
            next.step=a.step+1;
            next.state=a.state;

            if(judge(next.x,next.y)){///非墙
                if(map[next.x][next.y]=='b'){
                    t=1<<0;
                    next.state|=t;
                    if(vis[next.x][next.y][next.state]==0){
                        vis[next.x][next.y][next.state]=1;
                        q.push(next);
                    }
                }
               else if(map[next.x][next.y]=='y'){
                    t=1<<1;
                    next.state|=t;
                    if(vis[next.x][next.y][next.state]==0){
                        vis[next.x][next.y][next.state]=1;
                        q.push(next);
                    }
                }
                else if(map[next.x][next.y]=='r'){
                    t=1<<2;
                    next.state|=t;
                    if(vis[next.x][next.y][next.state]==0){
                        vis[next.x][next.y][next.state]=1;
                        q.push(next);
                    }
                }
                else if(map[next.x][next.y]=='g'){
                    t=1<<3;
                    next.state|=t;
                    if(vis[next.x][next.y][next.state]==0){
                        vis[next.x][next.y][next.state]=1;
                        q.push(next);
                    }
                }



                else if(map[next.x][next.y]=='B'){
                    t=1<<0;
                    if((next.state&t)&&vis[next.x][next.y][next.state]==0){
                        vis[next.x][next.y][next.state]=1;
                        q.push(next);
                    }
                }
                else if(map[next.x][next.y]=='Y'){
                    t=1<<1;
                    if((next.state&t)&&vis[next.x][next.y][next.state]==0){
                        vis[next.x][next.y][next.state]=1;
                        q.push(next);
                    }
                }
                else if(map[next.x][next.y]=='R'){
                    t=1<<2;
                    if((next.state&t)&&vis[next.x][next.y][next.state]==0){
                        vis[next.x][next.y][next.state]=1;
                        q.push(next);
                    }
                }
                else if(map[next.x][next.y]=='G'){
                    t=1<<3;
                    if((next.state&t)&&vis[next.x][next.y][next.state]==0){
                        vis[next.x][next.y][next.state]=1;
                        q.push(next);
                    }
                }

                else if(vis[next.x][next.y][next.state]==0){///是'通道'或者是'出口'的情况
                    vis[next.x][next.y][next.state]=1;
                    q.push(next);
                }
            }
        }
    }

}

int main()
{
    while(cin>>n>>m&& n+m){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>map[i][j];
                if(map[i][j]=='*'){sx=i;sy=j;}
                if(map[i][j]=='X'){ex=i;ey=j;}
            }
        }

        ans=-1;
        BFS();
        if(ans==-1)cout<<"The poor student is trapped!"<<endl;
        else cout<<"Escape possible in "<<ans<<" steps."<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值