广搜 — — ZOJ 1649 Rescue

Rescue

题目描述

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.)

输入

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.

输出

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." 

样例输入

7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

样例输出

13

可以参考拯救公主一题

从天使的朋友的位置开始进行广搜

运行时间 :15ms

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
int n,m,vis[201][201];
char Map[201][201];
const int c[4][2]={-1,0,1,0,0,1,0,-1};
struct node{
	int x,y;
	int step;
	friend bool operator < (node a,node b){
		return a.step > b.step;
	}
};

priority_queue<node> q;
int bfs(){
	node tmp;
	int px,py,kx,ky,res;
	while(!q.empty()){
		tmp=q.top();
		q.pop();
		px=tmp.x;
		py=tmp.y;
		res=tmp.step;
		for(int i=0;i<4;i++){
			kx=px+c[i][0];
			ky=py+c[i][1];
			if(!vis[kx][ky] && kx>=0 && kx<n && ky>=0 && ky<m && Map[kx][ky]!='#'){
				vis[kx][ky]=1;
				if(Map[kx][ky]=='a')	return res+1;
				if(Map[kx][ky]=='.'){
					tmp.step=res+1;
					tmp.x=kx;
					tmp.y=ky;
					q.push(tmp);
				}
				if(Map[kx][ky]=='x'){
					tmp.step=res+2;
					tmp.x=kx;
					tmp.y=ky;
					q.push(tmp);
				}
			}
		}
	}
	return -1;
}
int main()
{
	node tmp;
	while(~scanf("%d%d",&n,&m)){
		int sx,sy;
		memset(Map,0,sizeof(Map));
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++)	scanf("%s",Map[i]);
		while(!q.empty())	q.pop();
		for(int i=0;i<n ;i++){
			for(int j=0;j<m;j++){
				if(Map[i][j]=='r'){
					Map[i][j]='.';
					tmp.x=i;
					tmp.y=j;
					tmp.step=0;
					q.push(tmp);
					vis[i][j]=1;
				}
			}
		}
		int ans=bfs();
		if(ans!=-1)		printf("%d\n",ans);
		else         printf("Poor ANGEL has to stay in the prison all his life.\n");	
	}
	return 0;
}



找到天使的位置,从天使的位置开始搜索,若找到营救者,则结束;否则,向四个方向进行搜索,判断是否走过、是否能走,分情况进行步数的增减,若遇到 ‘ . ’ 或是 ‘ r ’ ,步数 + 1;若遇到 ‘ x ’,步数 + 2    

走过的路径保存到优先队列中去


运行时间  :0ms


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
int n,m,vis[201][201];
char Map[201][201];
const int c[4][2]={-1,0,1,0,0,1,0,-1};
struct node{
	int x,y;
	int step;
	friend bool operator < (node a,node b){
		return a.step > b.step;
	}
};
priority_queue<node> q;

int bfs(int x1,int y1){
	node tmp;
	tmp.x=x1;	tmp.y=y1;	tmp.step=0;
	q.push(tmp);
	int px,py,kx,ky,res;
	while(!q.empty()){
		tmp=q.top();
		q.pop();
		px=tmp.x;
		py=tmp.y;
		res=tmp.step;
		if(Map[px][py]=='r')	return res;
		
		for(int i=0;i<4;i++){
			tmp.x=px+c[i][0];
			tmp.y=py+c[i][1];
			tmp.step=0;
			if(!vis[tmp.x][tmp.y] && tmp.x>=0 && tmp.x<n && tmp.y>=0 && tmp.y<m){
				
				if(Map[tmp.x][tmp.y]=='.' || Map[tmp.x][tmp.y]=='r')
					tmp.step=res+1;
				
				else if(Map[tmp.x][tmp.y]=='x')
					tmp.step=res+2;
				if(tmp.step>0 && !vis[tmp.x][tmp.y]){
					vis[tmp.x][tmp.y]=1;
					q.push(tmp);
				}
			}
		}
	}
	return 0;
}
int main()
{
	while(~scanf("%d%d",&n,&m)){
		int sx,sy;
		memset(Map,0,sizeof(Map));
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++)	scanf("%s",Map[i]);
		for(int i=0;i<n ;i++){
			for(int j=0;j<m;j++){
				if(Map[i][j]=='a'){
					sx=i;
					sy=j;
					break;
				}
			}
		}
		vis[sx][sy]=1;
		int ans=bfs(sx,sy);
		if(ans)		printf("%d\n",ans);
		else         printf("Poor ANGEL has to stay in the prison all his life.\n");	
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黎轩栀海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值