HDU2128Tempter of the Bone II(bfs+狀態壓縮),hdu2128tempterbfs
Tempter of the Bone IITime Limit: 10000/5000 MS (Java/Others) Memory Limit: 98304/32768 K (Java/Others)
Total Submission(s): 1529 Accepted Submission(s): 407
Problem DescriptionThe doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze was changed and the way he came in was lost.He realized that the bone was a trap, and he tried desperately to get out of this maze.
The maze was a rectangle with the sizes of N by M. The maze is made up of a door,many walls and many explosives. Doggie need to reach the door to escape from the tempter. In every second, he could move one block to one of the upper, lower, left or right neighboring blocks. And if the destination is a wall, Doggie need another second explode and a explosive to explode it if he had some explosives. Once he entered a block with explosives,he can take away all of the explosives. Can the poor doggie survive? Please help him.
InputThe input consists of multiple test cases. The first line of each test case contains two integers N, M,(2 <= N, M <= 8). which denote the sizes of the maze.The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'X': a block of wall;
'S': the start point of the doggie;
'D': the Door;
'.': an empty block;
'1'--'9':explosives in that block.
Note,initially he had no explosives.
The input is terminated with two 0's. This test case is not to be processed.
OutputFor each test case, print the minimum time the doggie need to escape if the doggie can survive, or -1 otherwise.
Sample Input
4 4SX..XX......1..D4 4S.X1......XX..XD0 0
Sample Output
-19
AuthorXHD
SourceHDU 2007-10 Programming Contest
題意:要求從 S 點走到 D 點的最短時間,‘1’~‘9’:表示當前位置有炸藥數量,X:表示牆。" . ":表示可走的路。牆可以用一包炸藥炸通,需花一個單位時間。
分析:從S到D,所經過的每個位置得到的炸藥數量可能不等,就算得到的炸藥數量相等,但每個到達當前位置時的地圖的狀態不同。所以我們不僅必須判斷在每個位置得到的炸藥數是否存在過,而且還要判斷得到的這些炸藥數在相同情況下不同地圖的狀態。
#include<stdio.h>#include<iostream>#include<queue>#include<vector>#include<string.h>using namespace std;struct State{ short state[8];//地圖每行壓縮的狀態,};struct node{ int x,y; //當前走到的位置 int time,explosiveNumb; //走過的時間,得到的炸藥數量 State s; //走過後的地圖狀態 friend bool operator<(const node &a,const node &b) //優先隊列以時間最小的順序取 { return a.time>b.time; }};int n,m;char map[10][10];vector<State>vist[8][8][560]; //走到地圖每個位置得到不同炸藥數量的地圖狀態有沒有存在過void init(){ for(int i=0; i<n; i++) for(int j=0; j<m; j++) for(int k=0; k<559; k++) vist[i][j][k].clear();}bool judge(int x,int y,int numb,short state[]){ int length=vist[x][y][numb].size(); for(int i = 0; i < length ;i++) { int j; for(j = 0; j<n ; j++) if(vist[x][y][numb][i].state[j]!=state[j]) break; if(j==n) //當前將要走的state[]地圖狀態,不必再排入佇列(即己經存在過) return false; } //當前將要走的地圖狀態沒存在過,需排入佇列 return true;}int bfs(int sx,int sy){ int dir[4][2]={0,1,0,-1,1,0,-1,0}; priority_queue<node>q; node p,tp; init(); p.x=sx; p.y=sy; p.time=0; p.explosiveNumb=0; for(int i=0;i<n;i++) { p.s.state[i]=0; //壓縮每行地圖的狀態,位置是 1 表示不存在任何東西 for(int j=0;j<m;j++) if(map[i][j]=='.') p.s.state[i]|=(1<<j); } vist[sx][sy][0].push_back(p.s); q.push(p); while(!q.empty()) { p=q.top(); q.pop(); for(int e=0;e<4;e++) { tp=p; tp.x+=dir[e][0]; tp.y+=dir[e][1]; tp.time++; if(tp.x>=0&&tp.x<n&&tp.y>=0&&tp.y<m) { if(!(tp.s.state[tp.x]&(1<<tp.y))&&map[tp.x][tp.y]=='X'&&!tp.explosiveNumb) //表示將要走的位置是現在還沒炸的牆,並且身上沒有炸藥 continue ; if(!(tp.s.state[tp.x]&(1<<tp.y))&&map[tp.x][tp.y]>'0'&&map[tp.x][tp.y]<='9') //表示將要走的位置是沒有取走的炸藥 { tp.explosiveNumb+=map[tp.x][tp.y]-'0'; tp.s.state[tp.x]|=1<<tp.y; //取走後變成 "." } else if(!(tp.s.state[tp.x]&(1<<tp.y))&&map[tp.x][tp.y]=='X'&&tp.explosiveNumb) //表示將要走的位置是現在還沒炸的牆,身上有炸藥 { tp.explosiveNumb--; tp.s.state[tp.x]|=1<<tp.y; //炸完後變成 "." tp.time++; } else if(map[tp.x][tp.y]=='D') return tp.time; if(judge(tp.x,tp.y,tp.explosiveNumb,tp.s.state)) //判斷走到當前位置的炸藥數量與地圖狀態是否沒有存在過?為真沒存在過,否則為存在過 vist[tp.x][tp.y][tp.explosiveNumb].push_back(tp.s),q.push(tp); } } } return -1;}int main(){ int sx,sy; while(scanf("%d%d",&n,&m)>0) { if(n==0&&m==0) break; for(int i=0;i<n;i++) scanf("%s",map[i]); for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(map[i][j]=='S') sx=i,sy=j; printf("%d\n",bfs(sx,sy)); }}