博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu Rescue 1242
阅读量:6530 次
发布时间:2019-06-24

本文共 4980 字,大约阅读时间需要 16 分钟。

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 11566    Accepted Submission(s): 4205

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
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
/*广搜检查了很久 最后 被困死的时候是 0,if(visit[i][j]
#include
#define HH 11111111char a[202][205];int map[4][2]={
{
1,0},{
0,1},{
0,-1},{-1,0}};int zhan[50000],len;int visit[202][202];int n,m;void bfs(int x,int y){ int i,x1,y1; zhan[++len]=x; zhan[++len]=y; visit[x][y]=0; while(len>0) { y=zhan[len--]; x=zhan[len--]; for(i=0;i<4;i++) { x1=x+map[i][0]; y1=y+map[i][1]; if(x1>=1&&x1<=n && y1>=1&&y1<=m) { if(visit[x1][y1]==0 && a[x1][y1]!='#') { if(a[x1][y1]=='.'||a[x1][y1]=='r') visit[x1][y1]=visit[x][y]+1; else if(a[x1][y1]=='x') visit[x1][y1]=visit[x][y]+2; zhan[++len]=x1; zhan[++len]=y1; } if(visit[x1][y1]>0 && a[x1][y1]!='#') { if((a[x1][y1]=='.'||a[x1][y1]=='r')&&visit[x1][y1]>visit[x][y]+1) { visit[x1][y1]=visit[x][y]+1; zhan[++len]=x1; zhan[++len]=y1; } if(a[x1][y1]=='x' && visit[x1][y1]>visit[x][y]+2) { visit[x1][y1]=visit[x][y]+2; zhan[++len]=x1; zhan[++len]=y1; } } } } }}int main(){ int i,j,num; while(scanf("%d%d",&n,&m)>0) { for(i=1;i<=n;i++) scanf("%s",a[i]+1); for(i=1;i<=n;i++) for(j=1;j<=m;j++) visit[i][j]=0; for(i=1;i<=n;i++) for(j=1;j<=m;j++) { if(a[i][j]=='a') { len=0; bfs(i,j); } } num=HH; for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(a[i][j]=='r') { if(visit[i][j]

单纯的广搜,在浙大oj超时.... 蒋神却过了,思想很厉害。

 

/*优先队列*/#include
#include
#include
#include
#include
#define HH 11111111using namespace std;char a[202][202];int visit[202][202];int n,m;int map[4][2]={
{
1,0},{
0,1},{-1,0},{
0,-1}};struct node{ friend bool operator< (node n1,node n2) { return n1.p>n2.p; } int p; int x; int y;};void bfs(int x,int y){ int i,x1,y1; priority_queue
b; while(!b.empty()) { b.pop(); } node tmp,tmp1; tmp.x=x; tmp.y=y; tmp.p=0; b.push(tmp); visit[x][y]=1; while(b.size()>0) { tmp=b.top(); b.pop(); for(i=0;i<4;i++) { x1=tmp.x+map[i][0]; y1=tmp.y+map[i][1]; if(x1>=1&&x1<=n && y1>=1&&y1<=m && visit[x1][y1]==0 && a[x1][y1]!='#') { if(a[x1][y1]=='x') visit[x1][y1]=tmp.p+2; else if(a[x1][y1]=='.' || a[x1][y1]=='r') visit[x1][y1]=tmp.p+1; tmp1=tmp; tmp.x=x1; tmp.y=y1; tmp.p=visit[x1][y1]; b.push(tmp); tmp=tmp1; if(a[x1][y1]=='r')return; } } }}int main(){ int i,j,num; while(scanf("%d%d",&n,&m)>0) { for(i=1;i<=n;i++) scanf("%s",a[i]+1); memset(visit,0,sizeof(visit)); for(i=1;i<=n;i++) for(j=1;j<=m;j++) { if(a[i][j]=='a') { bfs(i,j); } } num=HH; for(i=1;i<=n;i++) for(j=1;j<=m;j++) { if(a[i][j]=='r' && visit[i][j]!=0 && visit[i][j]

 

转载于:https://www.cnblogs.com/tom987690183/p/3193587.html

你可能感兴趣的文章
第四十天笔记
查看>>
4、动态代理
查看>>
Loj #6073.「2017 山东一轮集训 Day5」距离
查看>>
我的TCP/IP学习笔记
查看>>
shell--字符串的截取变量子串串
查看>>
Cas_个人理解
查看>>
UISearchController
查看>>
梦断代码阅读笔记02
查看>>
轮毂电机光电增量编码器的ABZ信号详解
查看>>
TextBox Template
查看>>
Linux MySQL 储存中文失败简单解决办法
查看>>
求最大值及其下标
查看>>
洛谷——P1330 封锁阳光大学
查看>>
css选择器
查看>>
zabbix-agent配置文件说明
查看>>
linux系统配置之bash shell的配置(centos)
查看>>
linux C 9*9
查看>>
hdu 1695: GCD 【莫比乌斯反演】
查看>>
python的string操作总结
查看>>
如何把word中的图片怎么导出来呢?
查看>>