Find a way (BFS recording steps)

Title connection: the Find A Way

topic:

jxf hj recently and fell in love with the game pokemon go. Before training home, they tried to grab a rare ztw to save save character (because marmot refresh location recently came close to)
but because ztw too strong, his foot Shredded tree line and can easily KMP the two beat them, but they punch combo AC robot fail dfs order to build two trees can be persistent segment tree can be ztw to play a coma, and can be captured.
But because this is money paid by the time the game, they need to capture as soon as possible to ztw (ie the time they are two to ztw and require a minimum), so they found you to help them solve this problem. Provisions every step it takes 11 minutes.

Input
Input plurality of sets (using! = EOF)
a first line of each of the two integers n, m (2 <= n , m <= 200)
the next n rows, each row including m character
'Y' represents jxf location of
'M' represents the position hj where
'' indicates the place by the
'#' indicates the teaching building that is not taking place
'@' means (more than just rare ztw presence map) rare ztw refreshing place
(Friendly reminder: Y and M are the two places is not to go !!!)

Output
For each sample output refresh woodchuck they reach the minimum sum of the time points.
Each set of samples to ensure that there is a groundhog refresh point, they can reach two

Sample Input
4 4
Y.#@

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

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

Sample Output
66
88
66
The English version was made to see the problem, to be honest I do not understand the Chinese Y and M tips I can not take what's the use if your code encounters a need to determine the Y and M can not take issue, you can also put your code Share it.

Problem-solving ideas:

Let two people once BFS run chart, go to each location (x, y) recorded the shortest number of steps to everyone here, and then added, so you can get the whole map for each of up to two minimum at the number of steps and finally found a position which @ * 11 output the minimum number of steps to

AC Code:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
char a[300][300]; int m, n; //存图
bool vis[300][300]; //标记是否到过这里
int res[300][300]; //储存到达i j处的步数
int fx[][2] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
struct node {
	int x, y, step;
};
queue<node> q;
void bfs()
{
	memset(vis, 0, sizeof(vis)); //因为是两个人分别跑, 所以每次进函数都要清空
	node temp = q.front(); vis[temp.x][temp.y] = 1;
	while (!q.empty()) {
		int t = q.size();
		while (t--) {
			node op = q.front(); q.pop();
			for (int i = 0; i < 4; i++) {
				int dx = op.x + fx[i][0];
				int dy = op.y + fx[i][1];
				int cou = op.step + 1;
				if (dx < 1 || dy < 1 || dx > m || dy > n || vis[dx][dy] || a[dx][dy] == '#') continue;
				vis[dx][dy] = 1; q.push({ dx, dy, cou }); res[dx][dy] += cou; //因为是两个人跑, 所以要用+=
			}
		}
	}
}
int main(void)
{
	while (cin >> m >> n) {
		memset(res, 0, sizeof(res)); memset(a, 0, sizeof(a)); int ans = 0x7fffffff;
		int yx, yy, mx, my;
		for (int i = 1; i <= m; i++) {
			scanf("%s", a[i] + 1);
			for (int j = 1; j <= n; j++) {
				if (a[i][j] == 'Y') { yx = i, yy = j; }
				if (a[i][j] == 'M') { mx = i, my = j; }
			}
		}

		q.push({ yx, yy, 0 }); bfs(); //分别跑两个人的BFS
		q.push({ mx, my, 0 }); bfs();

		for (int i = 1; i <= m; i++) {
			for (int j = 1; j <= n; j++) {
				if (a[i][j] == '@' && res[i][j]) ans = min(res[i][j], ans); //要保证能跑到这个@
			}
		}
		cout << ans * 11 << endl;
	}
	return 0;
}

About only one path vis array record: If you feel a little loose, and I think can not guarantee to Y M can also be a place to, then I give my thoughts below.
Because the topic guarantee a solution, that is, there must be a shortest path @, then that can reach the Y and M @ at the Y, and so should be the starting point M on the map is connected, if such a term, then Y on the map whenever you can to M You will be able to, and vice versa.

END

Published 20 original articles · won praise 0 · Views 498

Guess you like

Origin blog.csdn.net/weixin_45799835/article/details/104300373