hdu 1429 勝利大逃亡(續) bfs+狀態壓縮
阿新 • • 發佈:2019-02-06
勝利大逃亡(續)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5738 Accepted Submission(s): 2006
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
Author
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5738 Accepted Submission(s): 2006
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
Author
LL
做法:因為鑰匙最多有10把,2^10 =1024,所以可以把10把鑰匙有沒有的情況記錄在 一個數中。 num的第三維就是 鑰匙 擁有的狀態。然後就和普通的bfs一樣了。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #include <stack> #include <queue> #include <vector> #include <deque> #include <set> #include <map> using namespace std; int n,m,t; char mp[22][22]; int num[22][22][1050];//位置 鑰匙狀態 int dir[4][2]={-1,0,1,0,0,1,0,-1}; int ex,ey; int stax,stay; struct point { int x,y,step,key; }; int ok(int x,int y) { if(!(x>=0&&x<n&&y>=0&&y<m)) return 0; char ch=mp[x][y]; if(ch<='J'&&ch>='A') return 3; //門 if(ch<='j'&&ch>='a') return 2; //key if(ch!='*') return 1; return 0; } int bfs() { if(t==0) return -1; point sta,now,tem; sta.x=stax; sta.y=stay; sta.step=0; sta.key=0; queue<point> q; q.push(sta); while(!q.empty()) { now=q.front(); q.pop(); if(now.step!=0&&now.step%t==0) { return -1; //now.x=stax; //now.y=stay; } if(now.x==ex&&now.y==ey) return now.step; for(int i=0;i<4;i++) { int xx=now.x+dir[i][0]; int yy=now.y+dir[i][1]; int cdt=ok(xx,yy);//condition int key=now.key; if(cdt==0) continue; if(cdt==3&&(key&(1<<(mp[xx][yy]-'A')))==0) continue; if(cdt==2) key|=(1<<(mp[xx][yy]-'a')); if(num[xx][yy][key]==-1||num[xx][yy][key]>now.step+1) { tem.x=xx; tem.y=yy; tem.step=now.step+1; tem.key=key; num[xx][yy][key]=now.step+1; q.push(tem); } } } return -1; } int main() { while(scanf("%d%d%d",&n,&m,&t)!=EOF) { memset(num,-1,sizeof num); for(int i=0;i<n;i++) scanf("%s",mp[i]); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(mp[i][j]=='@') stax=i,stay=j; if(mp[i][j]=='^') ex=i,ey=j; } } printf("%d\n",bfs()); } return 0; }