[BFS] hdu2612 [ACM][2018北理校赛]朋友,面基吗 【补】

11 篇文章 0 订阅

Description

终于到了北京了,这是糖糖最后一次参加蓝桥杯,由于上一次鸽了LZH,这一次决定和lzh找一个肯德基面基,但是长途劳累的他们并不想走很远的路,于是他们就想找一个总路程最小的肯德基见面。现在给你一个北京的地图,抽象成一个n*m的矩阵,他们只能向相邻的点移动并花费11分钟。求他们见面需要的最短总时间

Input

有多组数据每一组数据第一行是两个整数n,m,(2<=n,m<=200)下面接着n行,每一行有m个字符“Y”表示糖糖的位置“M”表示lzh的位置“#”表示不可穿越的物体,可以看作一堵墙“.”表示可以到达的点“@”表示肯德基

Output

对应每一组数据输出一行他们见面需要的最短总时间

Sample Input 1 

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

Sample Output 1

66 
88

思路: 两次BFS , 但是不能拿肯德基去找 两个人 肯定会TLE , 因为肯德基可以有多个, 先在输入时记录 肯德基位置 

拿两个二维数组 记录 到 这个坑德基的距离。 最后在 加起来 遍历 一遍取最小就行了

AC代码:

//hdu2612
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define mem(a, b) memset(a, b, sizeof(a))

using namespace std;

int n, m, cnty[220][220], cntm[220][220];
bool vis[220][220];
char g[220][220];

struct p {
	int x, y, cnt;
	p(int a = -1, int b = -1, int c = 0) : x(a), y(b), cnt(c) {}
} kfc[220];

int dxdy[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

bool in_g(p a) {//判断是否在图中
	return a.x >= 0 && a.y >= 0 && a.x < n && a.y < m;
}

void bfs1(p s) {
	mem(vis, 0);
	queue<p> q;
	s.cnt = 0;
	q.push(s);
	while (!q.empty()) {
		p now = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			p tmp = {now.x + dxdy[i][0], now.y + dxdy[i][1], now.cnt};
			if (in_g(tmp) && g[tmp.x][tmp.y] != '#' &&  !vis[tmp.x][tmp.y]) {
				tmp.cnt++;
				vis[tmp.x][tmp.y] = 1;
				if (g[tmp.x][tmp.y] == '@') {
					cnty[tmp.x][tmp.y] = tmp.cnt;
				} else
					q.push(tmp);
			}
		}
	}
}

void bfs2(p s) {
	mem(vis, 0);
	queue<p> q;
	s.cnt = 0;
	q.push(s);
	while (!q.empty()) {
		p now = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			p tmp = {now.x + dxdy[i][0], now.y + dxdy[i][1], now.cnt};
			if (in_g(tmp) && g[tmp.x][tmp.y] != '#' &&  !vis[tmp.x][tmp.y]) {
				tmp.cnt++;
				vis[tmp.x][tmp.y] = 1;
				if (g[tmp.x][tmp.y] == '@') {
					cntm[tmp.x][tmp.y] = tmp.cnt;
				}
				q.push(tmp);
			}
		}
	}
}

int main() {
	while (~scanf("%d %d", &n, &m)) {
		getchar();
		int cnt = 0;
		p Y(-1, -1), M(-1, -1);
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				scanf("%c", &g[i][j]);
				if (g[i][j] == 'Y') {
					Y = {i, j};
				} else if (g[i][j] == 'M') {
					M = {i, j};
				} else if (g[i][j] == '@') {
					kfc[cnt++] = {i, j};
				}
			}
			getchar();
		}
//		puts("---------------------");
//		printf("Y(%d,%d), M(%d,%d)\nKFC\n", Y.x, Y.y, M.x, M.y);
//		for (int i = 0; i < cnt; i++)
//			printf("(%d,%d)\n", kfc[i].x, kfc[i].y);
//		puts("---------------------");
		int ans = INF;
		mem(cnty, INF);
		mem(cntm, INF);
		bfs1(Y);
		bfs2(M);
		for (int i = 0; i < cnt; i++) {
			ans = min(ans, cnty[kfc[i].x][kfc[i].y] + cntm[kfc[i].x][kfc[i].y]);
		}
//		for (int i = 0; i < cnt; i++) {
//			int tmp = cnty[kfc[i].x][kfc[i].y];
//			if (tmp >= INF)
//				printf("-1 ");
//			else
//				printf("%d ", tmp);
//			tmp = cntm[kfc[i].x][kfc[i].y];
//			if (tmp >= INF)
//				printf("-1 ");
//			else
//				printf("%d ", tmp);
//			cout << endl;
//		}
//		puts("\n****************************");
		if (ans >= INF) {
			puts("-1");
		} else {
			printf("%d\n", ans * 11);
		}
	}
	return 0;
}
/*

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

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

4 4
..#@
....
.#..
@#.M

4 4
Y.#@
....
.#..
@#..

5 5
Y..@.
.#...
.#...
@..M.
#...#

3 3
Y@@
@@@
@@M

 */


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值