Find a way (双bfs)

Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.

题目大意:

终于在杭州度过了一年的学习,一飞回到家乡宁波。离开宁波一年了,亦芬菲有很多人要见面。特别是一个好朋友梅尔斯基。

一奋飞的家在乡下,梅尔斯基的家在市中心。于是一奋飞和奔驰安排在肯德基见面。在宁波有很多肯德基,他们想选择一个让总时间最小的。

现在给你一张宁波地图,奔驰和易芬飞都可以上下左右移动到相邻的路上,只要11分钟。

输入

输入包含多个测试用例。

每个测试用例包括,前两个整数n,m (2<=n,m<=200)。

接下来n行,每一行包含m个字符。

'Y’表示yifenfei的初始位置。

'M’表示Merceki的初始位置。

'#'表示不可通行;

'.'表示道路。

'@表示’KCF

#include<bits/stdc++.h>
using namespace std;
int b,c,v[210][210],ans[210][210],dx[4]={-1,0,1,0},dy[4]={0,1,0,-1},f[210][210];
char a[210][210];
struct node{
	int x;
	int y;
};
void bfs(int x,int y){
	memset(v,0,sizeof v);
	memset(f,0,sizeof f);
	queue<node> q;
	q.push({x,y});
	v[x][y]=0;
	f[x][y]=1;
	while(!q.empty()){
		node t=q.front();
		q.pop();
		if(a[t.x][t.y]=='@'){
		ans[t.x][t.y]+=v[t.x][t.y];
				}
		for(int i=0;i<4;i++){
			int tx=dx[i]+t.x;
			int ty=dy[i]+t.y;
			if(tx>=1&&tx<=b&&ty>=1&&ty<=c&&a[tx][ty]!='#'&&f[tx][ty]==0){
				f[tx][ty]=1;
				q.push({tx,ty});
				v[tx][ty]=v[t.x][t.y]+11;
			} 
		}
	}
}
int main(){
	while(cin>>b>>c){
		memset(ans,0,sizeof ans);
		int sx1,sy1,sx2,sy2;
		for(int i=1;i<=b;i++)
		for(int j=1;j<=c;j++){
		cin>>a[i][j];
		if(a[i][j]=='Y'){
			sx1=i;
			sy1=j;
		}
		if(a[i][j]=='M'){
			sx2=i;
			sy2=j;
		}
		}
		bfs(sx1,sy1);
		bfs(sx2,sy2);
		int minn=40005;
		for(int i=1;i<=b;i++)
		for(int j=1;j<=c;j++){
			if(a[i][j]=='@'&&ans[i][j]!=0){
			minn=min(minn,ans[i][j]);	
			}
		}
		printf("%d\n",minn);
	}
	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、付费专栏及课程。

余额充值