Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 31144 Accepted Submission(s): 10926
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.)
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.
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 Input
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
Sample Output
13
/**************************************************************
Problem: Rescue
Language: C/C++
method: 优先队列+bfs
date:2017 7 18
create by Songdan_Lee
****************************************************************/
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int dir[4][2]={-1,0,1,0,0,-1,0,1};
char map[205][205];
int vis[205][205];
int n,m;
typedef struct Node{
int x;
int y;
int step;
bool operator < (const Node &a)
const {
return step>a.step;//最小值优先
}
}ss;
typedef struct Node1{
int x;int y;
}position;
position start,end1;
void bfs(int x,int y,int total){
priority_queue<ss>q;
ss hd,hd1;
memset(vis,0,sizeof(vis));
vis[x][y]=1;
hd.x=x;
hd.y=y;
hd.step=total;
q.push(hd);
while(!q.empty())
{
hd1=q.top();
q.pop();
if(map[hd1.x][hd1.y]=='a')
{
printf("%dn",hd1.step+1);
return ;
}
for(int i=0;i<4;i++)
{
hd.x=hd1.x+dir[i][0];
hd.y=hd1.y+dir[i][1];
if(hd.x<0||hd.y<0||hd.x>=m||hd.y>=n||vis[hd.x][hd.y]||map[hd.x][hd.y]=='#')
continue;
if(map[hd.x][hd.y]=='.')
hd.step=hd1.step+1;
if(map[hd.x][hd.y]=='x')
hd.step+hd.step+2;
vis[hd.x][hd.y]=1;
q.push(hd);
}
}
printf("Poor ANGEL has to stay in the prison all his life.n");
}
int main(){
while(scanf("%d%d",&m,&n)!=EOF){
if(m==0&&n==0)
break;
for(int i=0;i<m;i++)
scanf("%s",map[i]);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(map[i][j]=='a')
{
end1.x=i;
end1.y=j;
}
if(map[i][j]=='r'){
start.x=i;
start.y=j;
}
}
bfs(start.x,start.y,0);
}
}
最后
以上就是超帅蛋挞最近收集整理的关于Rescue(优先队列+bfs) Rescue的全部内容,更多相关Rescue(优先队列+bfs)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复