首页 技术 正文
技术 2022年11月24日
0 收藏 460 点赞 3,550 浏览 2202 个字

Rescue

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 60   Accepted Submission(s) : 22

Problem DescriptionAngel 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.)
 InputFirst 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. OutputFor 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     
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define N 210
using namespace std;
char a[N][N];
int b[N][N];
int n,m,ax,ay;
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
struct zz
{
int x,y,ans;
friend bool operator<(zz x,zz y)
{
return x.ans>y.ans;/*按照时间从小到大排序*/
}
};
void bfs(int x,int y)
{
memset(b,0,sizeof(b));
priority_queue<zz>q;
zz f1,f2;
f1.x=x;f1.y=y;
f1.ans=0;/*第一部一定要让ans=0*/
q.push(f1);
b[x][y]=1;
while(!q.empty())
{
f1=q.top() ;
q.pop();
if(a[f1.x][f1.y]=='r')
{
printf("%d\n",f1.ans);
return ;
}
for(int i=0;i<4;i++)
{
f2.x=f1.x+dx[i];
f2.y=f1.y+dy[i];
if(f2.x>0&&f2.x<=n&&f2.y>0&&f2.y<=m&&!b[f2.x][f2.y]&&a[f2.x][f2.y]!='#')
{/*没有越界不是墙壁没有被用过的点执行下一步操作*/
b[f2.x][f2.y]=1;
if(a[f2.x][f2.y]=='x')
f2.ans=f1.ans+2;/*遇到x时间加1*/
else
f2.ans=f1.ans+1;
q.push(f2);
}
}
}
printf("Poor ANGEL has to stay in the prison all his life.\n");/*当遍历一遍之后还未找到人r*/
}
int main(){
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%s",a[i]+1);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(a[i][j]=='a')
{
ax=i;ay=j;/*找到a的坐标*/
}
}
}
bfs(ax,ay);
}
return 0;
}

微信扫一扫

支付宝扫一扫

本文网址:https://www.zhankr.net/141968.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:877 阅读:6,217
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:806 阅读:4,049
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:566 阅读:4,851
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:732 阅读:4,786
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:5,874
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:3,567
助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:8:00-16:00

客服电话

400-888-8888

客服邮箱

ceotheme@ceo.com

扫描二维码

关注微信公众号

扫描二维码

手机访问本站