ZOJ 649 Rescue(优先队列+bfs)
2024-08-25 12:52:36
Rescue |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 521 Accepted Submission(s): 217 |
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 Input
7 8 |
Sample Output
13 |
题意:要求天使的朋友在最短的时间内救出天使,a代表天使,r代表天使的朋友,.代表路,#代表墙,x代表小兵,
每走一步需要一个时间,遇到小兵杀了他要一个时间,移到小兵的位置有要一个时间;
输出最短的时间,
如果救不出来就输出“Poor ANGEL has to stay in the prison all his life.”
/*
题解:利用优先队列+bfs
这题一直错,原因竟然是。。。读入有多组数据,我只读了一组
*/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int f[][];
char mp[][];
int n,m,sx,sy;
struct node
{
int x,y;
node(int a,int b){x=a; y=b;}
};
int dr[][]={{,},{,},{-,},{,-} };
struct cmp
{
bool operator()(node a,node b)
{
return f[a.x][a.y]>f[b.x][b.y];
}
};
int bfs()
{
priority_queue<node,vector<node>,cmp> Q;
memset(f,,sizeof(f));
Q.push(node(sx,sy));
f[sx][sy]=;
while(!Q.empty())
{
node p=Q.top();
Q.pop();
for(int i=;i<;i++)
{
int xx=p.x+dr[i][];
int yy=p.y+dr[i][];
if (xx>=n || xx< || yy>=m || yy<) continue;
if (mp[xx][yy]=='#' || f[xx][yy]>) continue;
f[xx][yy]=f[p.x][p.y]+;
if (mp[xx][yy]=='x') f[xx][yy]++;
Q.push(node(xx,yy));
if(mp[xx][yy]=='r') return f[xx][yy]-;
}
}
return -;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<n;i++)
{
scanf("%s",&mp[i]);
for(int j=;j<m;j++)
if (mp[i][j]=='a') sx=i,sy=j;
}
int ans=bfs();
if (ans==-) printf("Poor ANGEL has to stay in the prison all his life.\n");
else printf("%d\n",ans);
} return ;
}
最新文章
- Python开发【十一章】:RabbitMQ队列
- js const
- Chrome清除dns缓存
- Linux - Screen
- MySQL分表自增ID解决方案
- margin小结
- 为CDH 5.7集群添加Kerberos身份验证及Sentry权限控制
- ADF_Desktop Integration系列1_ADF桌面集成入门之设定Development Environment
- MySql远程连接无法打开解决办法
- 【coursera笔记】Machine Learning(Week6)
- Sysstat性能监控工具包中20个实用命令
- Swift - 正则表达式的使用(附用户名、邮箱、URL等常用格式验证)
- mybatis批量修改
- 云计算之路-阿里云上:节点 CPU 波动引发 docker swarm 集群故障
- Sql2012数据库还原
- 不应滥用named let
- 转自: linux svn命令行无法拉取中文名称的文件
- JAVA发送http get/post请求,调用http接口、方法
- Spark Gradient-boosted trees (GBTs)梯度提升树
- VS 2005 处理条件