BFS- Find a way

BFS-Find a way
题目描述
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.
输入
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
输出
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
样例输入
4 4
Y.#@

.#…
@…M
4 4
Y.#@

.#…
@#.M
5 5
Y…@.
.#…
.#…
@…M.
#…#
样例输出
66
88
66

【题意】
Y和M要去一个相同的@点喝咖啡约会,求,最短时间。
【思路】
Y和M各使用一次广搜,搜全图,适当剪枝,记录一下到每一个@点的时间,

#include <iostream>
#include <queue>
#include <cstdio>
#include <memory.h>
#include <algorithm>
#include <cstring>

using namespace std;

struct nod1 {
	int x,y;
	int sum;
};

int fx[] = {-1,0,1,0};  //方向数组,分别表示两人可以移动的四个方向
int fy[] = {0,1,0,-1};
int n,m;
int ax,bx,ay,by;  //记录两人的初始位置
nod1 s[240],ch;
char A[240][240];  //存储地图
bool vis[240][240]; //创建二元数组记录是否被访问过,1为被访问
int s2[2][240][240];  //存储两人到不同餐厅的距离
int po[240];   //存储两人到同一餐厅的距离

bool cmp (int a,int b) {
	return a < b;
}
bool check(int  y,int x) {
//判断该点是否可以走
	if(y < 1||y > n||x < 1||x > m||A[y][x] == '#'||vis[y][x])
		return 0;
	return 1;
}

int BFS(int x,int y,int k ) {
	//把起点地址位置入队
	queue<nod1> q;
	ch.x = x; ch.y = y; ch.sum = 0;
	q.push(ch);
	vis[y][x] = 1;

	while ( q.size() ) {
		for(int i = 0;i < 4;i++) {
			ch.x = q.front().x + fx[i];
			ch.y = q.front().y + fy[i];
			ch.sum = q.front().sum + 1;
			if ( check ( ch.y , ch.x ) ) {   
				if(A[ch.y][ch.x] == '@')  // 如果是找到餐厅到餐厅的步数存到餐厅坐标对应的数组下标所指向的位置 
					s2[k][ch.y][ch.x] = ch.sum;
				q.push(ch);  //把满足条件的位置元素入队
				vis[ch.y][ch.x] = 1; //标签其已经被走过

			}
		}
		q.pop(); //把队首元素出队
	}
}

int main() {

	while (scanf("%d%d",&n,&m)!=EOF) {

		for (int i = 1;i <= n;i++) {
			for(int j = 1;j <= m;j++) {
				scanf (" %c",&A[i][j]);
				if(A[i][j] == 'Y') {
					ax = j; ay = i;	
				}
				if(A[i][j] == 'M') {
					bx = j; by = i;
				}
			}
		}

		memset(vis,0,sizeof(vis));//初始化标记数组,使其没被访问过
		BFS(ax,ay,0);//从第一个人开始搜索餐厅
		memset(vis,0,sizeof(vis)); //初始化数组,使其为没被访问过
		BFS(bx,by,1);//从第二个人开始搜索餐厅
		
		//把两人到同一餐厅的距离相加
		int t = 1;
		for(int j = 1;j <= n;j++)
			for(int i = 1; i <= m;i++) 
				if(s2[0][j][i]&&s2[1][j][i]) //相应位置不能为0,为0不加
					po[t++] = s2[0][j][i] + s2[1][j][i];
		//从小到大排序
		sort(po+1,po+t,cmp);
		//输出两人距离和最小的路径所花费的时间
		printf("%d\n",11*po[1]);

		memset(po,0,sizeof(po));
		memset(s2,0,sizeof(s2));
	}
	return 0;	
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值