1. 程式人生 > >HDU 1429勝利大逃亡(續) (bfs+狀態壓縮)

HDU 1429勝利大逃亡(續) (bfs+狀態壓縮)

勝利大逃亡(續)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6469 Accepted Submission(s): 2243


Problem Description
Ignatius再次被魔王抓走了(搞不懂他咋這麼討魔王喜歡)……

這次魔王汲取了上次的教訓,把Ignatius關在一個n*m的地牢裡,並在地牢的某些地方安裝了帶鎖的門,鑰匙藏在地牢另外的某些地方。剛開始Ignatius被關在(sx,sy)的位置,離開地牢的門在(ex,ey)的位置。Ignatius每分鐘只能從一個座標走到相鄰四個座標中的其中一個。魔王每t分鐘回地牢視察一次,若發現Ignatius不在原位置便把他拎回去。經過若干次的嘗試,Ignatius已畫出整個地牢的地圖。現在請你幫他計算能否再次成功逃亡。只要在魔王下次視察之前走到出口就算離開地牢,如果魔王回來的時候剛好走到出口或還未到出口都算逃亡失敗。


Input
每組測試資料的第一行有三個整數n,m,t(2<=n,m<=20,t>0)。接下來的n行m列為地牢的地圖,其中包括:

. 代表路
* 代表牆
@ 代表Ignatius的起始位置
^ 代表地牢的出口
A-J 代表帶鎖的門,對應的鑰匙分別為a-j
a-j 代表鑰匙,對應的門分別為A-J

每組測試資料之間有一個空行。


Output
針對每組測試資料,如果可以成功逃亡,請輸出需要多少分鐘才能離開,如果不能則輸出-1。


Sample Input

4 5 17
@A.B.
a*.*.
*..*^
c..b*

4 5 16
@A.B.
a*.*.
*..*^
c..b*



Sample Output

16

-1

一共有10把鑰匙,共計2^10種可能,搜尋的狀態除了點的座標外,還有到達該點時的鑰匙狀況,由於2^10 比較小,可以用位運算表示鑰匙的集合情況

對於集合S,判斷一個數是否在集合裡是 if(s>>i&1)

對集合S新增元素是:s|=1<<i

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

#define cl(a,b) memset(a,b,sizeof(a))

const int maxn=22;
const int inf=9999999;


int n,m,t;
char a[maxn][maxn];
int vis[1025][maxn][maxn];
int si,sj,ei,ej;
int dir[][2]={{0,1},{1,0},{0,-1},{-1,0}};
struct node{
    int x,y;
    int cnt;
    int time;
    node(int a,int b,int c,int d){
        x=a;y=b;cnt=c;time=d;
    }
};
bool pan(node s){
    if(s.x<0||s.x>=n||s.y<0||s.y>=m||a[s.x][s.y]=='*')
        return false;
    return true;
}

int bfs(){
    queue<node> q;
    cl(vis,false);
    q.push(node(si,sj,0,0));
    vis[0][si][sj]=true;
    while(!q.empty()){
        node s=q.front();q.pop();
        if(s.time>=t-1)return -1;
        for(int i=0;i<4;i++){
            node tmp=s;
            tmp.x+=dir[i][0];
            tmp.y+=dir[i][1];
            tmp.time++;
            if(pan(tmp)){
                if(a[tmp.x][tmp.y]=='^'){
                    return tmp.time;
                }
                if((a[tmp.x][tmp.y]=='.'||a[tmp.x][tmp.y]=='@')&&!vis[tmp.cnt][tmp.x][tmp.y]){
                    q.push(tmp);
                    vis[tmp.cnt][tmp.x][tmp.y]=true;

                }
                else if(a[tmp.x][tmp.y]>='a'&&a[tmp.x][tmp.y]<='j'){
                    tmp.cnt|=1<<(a[tmp.x][tmp.y]-'a');//新增鑰匙
                    if(!vis[tmp.cnt][tmp.x][tmp.y]){
                        q.push(tmp);
                        vis[tmp.cnt][tmp.x][tmp.y]=true;
                    }
                }
                else if(a[tmp.x][tmp.y]>='A'&&a[tmp.x][tmp.y]<='J'&&!vis[tmp.cnt][tmp.x][tmp.y]){

                    if(tmp.cnt>>(a[tmp.x][tmp.y]-'A')&1){//判斷是否有鑰匙開門
                        q.push(tmp);
                        vis[tmp.cnt][tmp.x][tmp.y]=true;
                    }
                }
            }
        }
    }
    return -1;
}


int main(){

    while(~scanf("%d%d%d",&n,&m,&t)){
        for(int i=0;i<n;i++){
            scanf("%s",a[i]);
            for(int j=0;j<m;j++){
                if(a[i][j]=='@'){
                    si=i;sj=j;
                }
                if(a[i][j]=='^'){
                    ei=i;ej=j;
                }
            }
        }
        printf("%d\n",bfs());
    }
    return 0;
}




相關推薦

HDU 1429勝利逃亡() bfs+狀態壓縮

勝利大逃亡(續) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6469 Accepted Submission(s

hdu 1429 勝利逃亡() bfs+狀態壓縮

Problem Description Ignatius再次被魔王抓走了(搞不懂他咋這麼討魔王喜歡)…… 這次魔王汲取了上次的教訓,把Ignatius關在一個n*m的地牢裡,並在地牢的某些地方安裝了帶鎖的門,鑰匙藏在地牢另外的某些地方。剛開始Ignatius被關在(sx,s

HDU 1429 勝利逃亡()bfs+狀態壓縮,很經典

傳送門: 勝利大逃亡(續) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10648    Accepted Submission(s): 386

hdu1429 勝利逃亡()bfs+狀態壓縮

勝利大逃亡(續) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10777    Accepted Submi

HDU 1429--勝利逃亡()【BFS &amp;&amp; 狀態壓縮

sizeof ott 擁有 之間 數據 memset tdi mes mod 勝利大逃亡(續) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To

HDU 1429 勝利逃亡() (BFS

思路來自上面這篇大佬的題解,解題思路源自上面這篇題解 解題關鍵在於:如何表示是否帶了鑰匙 一共有10把鑰匙,共有1024種狀態,用二進位制表示 j i h g f e d c b a

hdu 1429 勝利逃亡() bfs+狀態壓縮

勝利大逃亡(續) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5738    Accepted Submis

HDU 1429 勝利逃亡() 解題報告

Description Ignatius再次被魔王抓走了(搞不懂他咋這麼討魔王喜歡)……  這次魔王汲取了上次的教訓,把Ignatius關在一個n*m的地牢裡,並在地牢的某些地方安裝了帶鎖的門,鑰匙藏在地牢另外的某些地方。剛開始Ignatius被關在(sx,

hdu 1429 勝利逃亡

Ignatius再次被魔王抓走了 這次魔王汲取了上次的教訓,把Ignatius關在一個n*m的地牢裡,並在地牢的某些地方安裝了帶鎖的門,鑰匙藏在地牢另外的某些地方。剛開始Ignatius被關在(sx,sy)的位置,離開地牢的門在(ex,ey)的位置。Ignatius每分鐘只能從一個座標走到相鄰四個座標中的其

1429勝利逃亡(bfs與二進位制按位操作

 思路: 有點像我們以前玩的魔塔,拿小寫字母表示鑰匙,去開大寫字母的門,如果用模板的bfs去寫,將vis設為二維陣列,記錄地圖上的二維座標,來標記走過的點,那麼這樣很明顯會失敗,因為勇士是可能要拿鑰匙後,走之前標記過的回頭路,所以我們要用vis三維陣列存狀態。第三維存鑰匙的

hdu 1253 勝利逃亡 三維bfs 解題報告

勝利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 31687    Accepted Submis

hdu1429 勝利逃亡() 廣搜+狀態壓縮

#include <iostream> #include <queue> #include <cstring> #include <cstdio> using namespace std; const int maxn = 22; int dir[4][2]

UVA12569-Planning mobile robot on Tree (EASY Version)BFS+狀態壓縮

print imu pop ret int height rst next c++ Problem UVA12569-Planning mobile robot on Tree (EASY Version) Accept:138 Submit:686 Time Limi

【POJ】1753Flip GameBFS+狀態壓縮

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 52748   Accepted:&n

勝利逃亡() (hdu 1429) (bfs+狀態壓縮)

勝利大逃亡(續) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10847    Accepted Submi

1429 勝利逃亡

相關演算法:廣度優先搜尋(百度連結) #include <iostream>#include <queue>using namespace std;int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};    //移動

HDU-1253-勝利逃亡bfs

                                           勝利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,這可是Ignatius逃亡的好機會. 魔王住在一個城堡裡,城堡是一個A*B*C的立方

勝利逃亡() (bfs+狀壓)

                                        勝利大逃亡(續) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota

勝利逃亡() HDU

題意:小明被抓到n*m的迷宮中,@表示小明的初始位置, ^表示迷宮出口, *表示障礙物,  ·  表示空地, A~J表示門, a~j表示鑰匙, 對應的鑰匙開對應字元的門;問小明能否在t時間內逃出迷宮?(若在第t時間到達迷宮出口, 記為未

HDU1429勝利逃亡()BFS+狀態壓縮

這題的算是BFS中應用狀壓的一個模板題吧,沒啥難度,用key來儲存已獲得的鑰匙,狀壓一下就可以了 不過我寫的過程中,犯了好多SB錯誤,導致除錯了好久才A,本來仔細可以1A的說 #include <cstdio> #include <cstring>