ACM--steps--4.2.1--Rescue(BFS+priority)
2015-03-09 19:46
176 查看
Rescue |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 932 Accepted Submission(s): 390 |
Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards. You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.) |
Input First line contains two integers stand for N and M. Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend. Process to the end of the file. |
Output For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." |
Sample Input7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........ |
Sample Output13 |
Author CHEN, Xue |
Source ZOJ Monthly, October 2003 |
Recommend Eddy #include<iostream> #include<queue> #include<cstring> #include<cstdio> using namespace std; struct dyx { int x,y,time;//x,y表示坐标,time表示所用的时间, friend bool operator<(dyx a,dyx b) { return b.time<a.time;//time小的优先级大。 } }; char map[209][209];//输入地图; //dyx now,next;//wyx表示当前的位置,next表示接下来进行搜索的位置。 int n,m,vis[209][209];//n,m分别为行列数,访问过的状态。 int x1,x2,y1,y2;//表示起始目标和终止目标; int dx[4]={1,0,-1,0};//表示方向向量。 int dy[4]={0,1,0,-1}; //检查当前的状态。 int wyx(int x,int y) { if(x<0||y<0||x>=n||y>=m||map[x][y]=='#'||!vis[x][y]) return 1; return 0; } int bfs() { dyx now,next;//wyx表示当前的位置,next表示接下来进行搜索的位置。 int i; priority_queue<dyx> que; now.x=x1; now.y=y1; now.time=0; que.push(now); vis[x1][y1]=0; while(!que.empty()) { now=que.top(); que.pop(); if(now.x==x2&&now.y==y2) return now.time; for(i=0;i<4;i++) { next=now; next.x+=dx[i]; next.y+=dy[i]; if(wyx(next.x,next.y)) continue; next.time++; if(map[next.x][next.y]=='x') //在卫兵的地方要达到卫兵所以要多花费1秒。 next.time++; if(vis[next.x][next.y]>=next.time) {//最小时间, vis[next.x][next.y]=next.time; que.push(next); } } } return 0; } int main() { int i,j; while(~scanf("%d%d",&n,&m)) { for(i=0;i<n;i++) for(j=0;j<m;j++) { cin>>map[i][j]; if(map[i][j]=='r') { x1=i; y1=j; } else if(map[i][j]=='a') { x2=i; y2=j; } } memset(vis,1,sizeof(vis));//1表示未经过的状态。 int ans=0; ans=bfs(); if(ans) cout<<ans<<endl; else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; } return 0; } |
相关文章推荐
- HDU 1242 Rescue (bfs + priority_queue)
- ACM-BFS之Rescue——hdu1242
- ACM: 图论题 BFS + STL + priority…
- ACM学习历程—Hihocoder 1139 二分·二分答案(bfs)
- 逃离ACM迷宫(BFS)
- 【BFS+优先级队列】Rescue
- HDOJ_ACM_steps—— Higher Math
- CSU 1224: ACM小组的古怪象棋(BFS)
- ZOJ 1649 Rescue (BFS)
- ACM-BFS之非常可乐——hdu1495
- ACM--steps--2.2.3--汉诺塔VII
- ACM-BFS之Open the Lock——hdu1195(双向BFS)
- ACM--steps--3.1.5--
- hdu acm steps 1.3.5
- hdu acm steps 2.1.8
- hdu acm steps 2.2.6
- ACM--steps--3.2.4--Humble Numbers
- POJ_2312_BFS:priority_queue -- Battle City
- ACM--steps--4.1.3--Pie(二分)
- ACM--steps--4.1.7--Dome of Circus(三分)