【狀態壓縮】HDU 1429 勝利大逃亡(續)

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1429

 

 

用二進制每一位表示是否拿到某個門的鑰匙,記錄狀態

若第二次走到同一位置,判斷若狀態不變則不再繼續入隊

 

#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;

struct node{
    int x,y;
    int step;
    int stu;
};

int n,m,t;
char map[30][30];
int vis[30][30][2000];
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
queue<node> q;
int sx,sy,ex,ey;

bool jud(node next){
    int x=next.x,y=next.y;
    if(x<0||x>=n||y<0||y>=m)
        return false;
    if(map[x][y]=='*')
        return false;
    if(vis[x][y][next.stu])
        return false;
    if(map[x][y]>='A'&&map[x][y]<='J'&&(next.stu&(1<<(map[x][y]-'A')))==0)
        return false;
    if(next.step>=t)
        return false;
    return true;
}

int bfs(){
    while(!q.empty())q.pop();
    memset(vis,0,sizeof(vis));
    node now,next;
    int i,j;
    now.x=sx,now.y=sy;
    now.step=0;
    now.stu=0;
    q.push(now);
    while(!q.empty()){
        now=q.front();
        q.pop();
        for(i=0;i<4;++i){
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            next.step=now.step+1;
            next.stu=now.stu;

            if(!jud(next))continue;
            int nx=next.x,ny=next.y;
            if(map[nx][ny]>='a'&&map[nx][ny]<='j'){
                next.stu=next.stu|(1<<(map[nx][ny]-'a'));
            }
            if(nx==ex&&ny==ey){
                return next.step;
            }
            vis[nx][ny][next.stu]=1;
            q.push(next);
        }
    }
    return -1;
}


int main(){
    int i,j;
    
    int ans;
    while(scanf("%d%d%d",&n,&m,&t)!=EOF){
        for(i=0;i<n;++i){
            scanf("%s",map[i]);
            for(j=0;j<m;++j){
                if(map[i][j]=='@'){
                    sx=i,sy=j;
                    map[i][j]='.';
                }
                if(map[i][j]=='^'){
                    ex=i,ey=j;
                }
            }
        }
        ans=bfs();
        printf("%d\n",ans);
    }
    return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章