3-12 搜索练习

第一题

在这里插入图片描述
题目就是bfs搜索,但是有多个起点,我有两种解法。

解法一

普通bfs+枚举起点

#include<iostream>
#include<queue>
using namespace std;
int n,m;
const int maxn=205;
const int INF=0x3f3f3f3f;
char map[maxn][maxn];
int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
int step[maxn][maxn];
struct node{
	int x,y;
	int step=0;
}s,temp;
int flag,res=INF;
void bfs(int x1,int y1,int x2,int y2){
	fill(step[0],step[0]+maxn*maxn,INF);
	queue<node>q;
	s.x=x2,s.y=y2;
	q.push(s);
	while(!q.empty()){
		temp=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			node st=temp;
			st.x+=dir[i][0],st.y+=dir[i][1];
			if(st.x>=1&&st.x<=n&&st.y>=1&&st.y<=m&&map[st.x][st.y]!='#'){
				if(map[st.x][st.y]=='x')
				st.step=temp.step+2;
				else
				st.step=temp.step+1;
				if(st.step<step[st.x][st.y]){
					step[st.x][st.y]=st.step;
					if(st.x==x1&&st.y==y1)
					flag=1,res=st.step;
					q.push(st);
				}
			}
		}
	}
}
node ss[maxn*maxn];
int main(){
	ios::sync_with_stdio(false);
	int x1,y1,x2,y2;
	while(cin>>n>>m){
		int cnt=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin>>map[i][j];
				if(map[i][j]=='a')
				x1=i,y1=j;
				if(map[i][j]=='r')
				ss[cnt].x=i,ss[cnt++].y=j;
			}
		}
		flag=0;
		int ans=INF;
		for(int i=0;i<cnt;i++){
			bfs(x1,y1,ss[i].x,ss[i].y);
			ans=min(ans,res);
		}
		if(!flag){
			cout<<"Poor ANGEL has to stay in the prison all his life.\n";
		}else{
			cout<<res<<endl;
		}
	}
	
	return 0;
}

解法二

用一个优先队列维护所有起点的最短路径,只需要一次bfs即可。

#include<iostream>
#include<queue>
using namespace std;
int n,m;
const int maxn=205;
const int INF=0x3f3f3f3f;
char map[maxn][maxn];
int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
int step[maxn][maxn];
struct node{
	friend bool operator < (node a,node b){
		return a.step>b.step;
	}
	int x,y;
	int step=0;
}s,temp;
int flag,res=-1;
void bfs(int x1,int y1,int x2,int y2){
	fill(step[0],step[0]+maxn*maxn,INF);
	priority_queue<node>q;
	s.x=x2,s.y=y2;
	q.push(s);
	while(!q.empty()){
		temp=q.top();
		q.pop();
		for(int i=0;i<4;i++){
			node st=temp;
			st.x+=dir[i][0],st.y+=dir[i][1];
			if(st.x>=1&&st.x<=n&&st.y>=1&&st.y<=m&&map[st.x][st.y]!='#'){
				if(map[st.x][st.y]=='x')
				st.step=temp.step+2;
				else
				st.step=temp.step+1;
				if(st.step<step[st.x][st.y]){
					step[st.x][st.y]=st.step;
					if(st.x==x1&&st.y==y1)
					flag=1,res=st.step;
					q.push(st);
				}
			}
		}
	}
}
node ss[maxn*maxn];
int main(){
	ios::sync_with_stdio(false);
	int x1,y1,x2,y2;
	while(cin>>n>>m){
		int cnt=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin>>map[i][j];
				if(map[i][j]=='a')
				x1=i,y1=j;
				if(map[i][j]=='r')
				x2=i,y2=j;
			}
		}
		flag=0;
		bfs(x1,y1,x2,y2);
		if(!flag){
			cout<<"Poor ANGEL has to stay in the prison all his life.\n";
		}else{
			cout<<res<<endl;
		}
	}
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值