Key Task HDU - 1885

题意


自己的思路错了,我想着每次找到新的钥匙就memset  vis 为0,但是其实是不可以的。。(自己其实应该花一定时间悄悄主代码,然后就应该是看看那别人的代码的,这样自己就不用花那么多时间的。(自己要把时间用在提升的地方,比如抄书的地方是没用的。)

这里看了别人的用状态压缩的方法,,

然后两个judge  还能用来加钥匙, 判断钥匙有没有,,然后vis表示的时候是拥有这些钥匙的时候经过这里。


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<stack>
#include<cmath>
#include<map>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
#define INF 1000000000
#define bug1 cout<<"bug1"<<endl;
#define bug2 cout<<"bug2"<<endl;
#define bug3 cout<<"bug3"<<endl;
using namespace std;

const int N=105;
int n,m,sx,sy,ex,ey;
char g[N][N];
bool vis[N][N][16];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
    int x,y,key,step;
};
int judged(char ch)
{
    if(ch=='B')return 1<<0;
    if(ch=='Y')return 1<<1;
    if(ch=='R')return 1<<2;
    if(ch=='G')return 1<<3;
}
int judgek(char ch)
{
    if(ch=='b')return 1<<0;
    if(ch=='y')return 1<<1;
    if(ch=='r')return 1<<2;
    if(ch=='g')return 1<<3;  //这个地方的状态压缩很好。
    return 0;
}
bool judgeok(node t){
    if(t.x<1||t.x>n||t.y<1||t.y>m||g[t.x][t.y]=='#'||vis[t.x][t.y][t.key])return false;
    return true;

}

void solve(){
    memset(vis,0,sizeof(vis));
    queue<node>q;
    node u,v;
    u.x=sx;u.y=sy;u.key=0;u.step=0;vis[u.x][u.y][u.key]=1;
    q.push(u);
    while(!q.empty()){
        u=q.front();q.pop();
        if(g[u.x][u.y]=='X'){printf("Escape possible in %d steps.\n",u.step);return;}
        for(int i=0;i<4;++i){
            v.x=u.x+dir[i][0];v.y=u.y+dir[i][1];v.step=u.step+1;v.key=u.key|judgek(g[v.x][v.y]);
            if(!judgeok(v))continue;
            if(g[v.x][v.y]=='B'||g[v.x][v.y]=='Y'||g[v.x][v.y]=='R'||g[v.x][v.y]=='G'){
                if(!(u.key&judged(g[v.x][v.y]))) continue;
            }
            vis[v.x][v.y][v.key]=1;
            q.push(v);
        }
    }
    printf("The poor student is trapped!\n");
}



int main(){

    while(scanf("%d%d",&n,&m)&&n+m){
        for(int i=1;i<=n;i++)scanf("%s",g[i]+1);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(g[i][j]=='*'){sx=i;sy=j;g[sx][sy]='.';break;break;}
            }
        }
        solve();
    }
    return 0;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值