算法题 深/广搜-05-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.
Input
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
Output
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.
Sample Input
4 4
Y.#@

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

.#…
@#.M
5 5
Y…@.
.#…
.#…
@…M.
#…#
Sample Output
66
88
66

思路:Y和M要去同一个@里,我们只需要对Y和M进行广搜找到最小步数,乘11即是答案了

#include<stdio.h>
#include<queue>
#include<set>
#include<string.h>
const int INF = 0x3f3f3f3f;
using namespace std;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1 };
int n,m;
char map[200][200];
int d[200][200];
int d2[200][200];
struct node {
	int x, y;
};
queue<node> que;
void bfs(node star) {
	queue<node> q;
	node  next,now;
	q.push(star);
	memset(d, INF, sizeof(d));
	d[star.x][star.y] = 0;
	while (!q.empty()) {
		now = q.front();
		q.pop();
		for (int k=0; k < 4; k++) {
			next.x = now.x + dx[k];
			next.y = now.y + dy[k];	
			if (next.x >= 0 && next.y >= 0 && next.x < n&&next.y < m&&map[next.x][next.y] != '#'&&d[next.x][next.y]==INF) {
				q.push(next);
				d[next.x][next.y] = d[now.x][now.y] + 1;
			}
		}
	}
}
void bfs1(node star) {
	queue<node> q1;
	node  next1, now1;
	q1.push(star);
	memset(d2, INF, sizeof(d2));
	d2[star.x][star.y] = 0;
	while (!q1.empty()) {
		now1 = q1.front();
		q1.pop();
		for (int k = 0; k < 4; k++) {
			next1.x = now1.x + dx[k];
			next1.y = now1.y + dy[k];
			if (next1.x >= 0 && next1.y >= 0 && next1.x < n&&next1.y < m&&map[next1.x][next1.y] != '#'&&d2[next1.x][next1.y] == INF) {
				q1.push(next1);
				d2[next1.x][next1.y] = d2[now1.x][now1.y] + 1;
			}
		}
	}
}
int main() {
	while (scanf("%d %d", &n, &m) != EOF) {
		int i, j;
		set<int> s;
		node M, Y;
		node KFC;
		int ans1, ans2;
		for (i=0; i < n; i++) {
			scanf("%s", map[i]);
			for (j = 0; j < m; j++) {
				if (map[i][j] == 'M') {
					M.x = i;
					M.y = j;
				}
				if (map[i][j] == 'Y') {
					Y.x = i;
					Y.y = j;
				}
				if (map[i][j] == '@') {
					node tmp;
					tmp.x = i; tmp.y = j;
					que.push(tmp);//因为@不止一个将所有@存在一个列表里
				}
			}
		}
		bfs(M);
		bfs1(Y);
		while(!que.empty()) {
			node temp;
			temp = que.front();
			que.pop();
			ans1 = d[temp.x][temp.y];//d数组存放了M到各个点的最优距离
			ans2 = d2[temp.x][temp.y];//d2数组存放了Y到各个点的最优距离
			s.insert(ans1 + ans2);//用set存就自动排好序了,不必在排序
		}
		printf("%d\n", (*s.begin())*11);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值