hdu 1429 bfs 狀態壓縮

2022-09-23 08:36:12 字數 1034 閱讀 8394

//開乙個三維標記陣列,標記ignatius有沒有以拿到鑰匙的狀態到達該位置

//由於鑰匙的個數最多十個,所以可以用狀態壓縮來做

//用1和0表示有沒有第i種鑰匙,這樣對於這個人拿到的鑰匙狀態就可以用二進位制數表示

//用bfs找到最小值

#include

#include

#include

#include

using namespace std ;

const int maxn = 30;

const int inf = 0x7fffffff;

int vis[maxn][maxn][1<<10];

char map[maxn][maxn] ;

int ans = ans;

struct node

;int st_x , st_y ;

int n , m;

int dx[4] = ;

int dy[4] = ;

queue que;

void bfs()

;vis[st_x][st_y][0] = 1;

que.push(first) ;

while(que.size())

for(int i = 0;i < 4;i++)

next.state = now.state ;

next.step = now.step + 1;

if(vis[next.x][next.y][next.state])

continue ;

vis[next.x][next.y][next.state] = 1;

que.push(next) ;}}

}int main()

ans = inf ; //可能到不了終點,所以有可能bfs沒有更新ans

bfs(); //所以ans每次都要初始化

if(ans < time)

printf(%d , ans);

else

printf(-1 );

}return 0;} 

hdu1429(bfs 狀態壓縮)

思路 有十個門,有十把鑰匙,每把鑰匙對應乙個門,相同的門可以有多個。這樣,我們就得按照狀態來搜尋,用0000000001代表第乙個門有鑰匙了,1000000000代表第十個門鑰匙有了.一次類推,可以用二進位制來表示.遇到鑰匙,可以先拾起這個鑰匙,用 遇到門,可以判斷是否有這個門的鑰匙,把門狀態位移後...

Hdu 1429(狀態壓縮)

有鑰匙和門的 bfs 狀態壓縮 乙個十把鑰匙和鎖,分別為a,b.j 當所用的步數小於 t即可。include include include includeusing namespace std int n,m,t,ans char map 21 21 int dir 4 2 int vis 21 ...

hdu1429勝利大逃亡 續 bfs 狀態壓縮

原先打算用乙個class裡面的set 儲存鑰匙,每次遇上門,就看當前結點的set有沒有對應的key 每次遇上鑰匙就將鑰匙插入的這個節點中,同時還要將父節點的鑰匙插入這個節點中,顯然這樣子很花費時間 於是考慮狀態壓縮,問題是怎麼壓縮?比如怎麼用二進位制儲存鑰匙 當走到 xx,yy 時 首先明確鑰匙只有...