Submission #575910

#TimeUsernameProblemLanguageResultExecution timeMemory
575910acatmeowmeowMecho (IOI09_mecho)C++11
27 / 100
878 ms13768 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long 

const int N = 8e2 + 5;
int n, s;
char grid[N][N];
pair<int, int> init, home;
vector<pair<int, int>> hive;

vector<int> dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
int dmecho[N][N], dbee[N][N];
bool vmecho[N][N], vbee[N][N];
queue<pair<int, int>> qmecho, qbee;

bool bee_inbound(int x, int y) {
	return x >= 1 && x <= n && y >= 1 && y <= n && !vbee[x][y] && grid[x][y] == 'G';
}

bool mecho_inbound(int x, int y) {
	return x >= 1 && x <= n && y >= 1 && y <= n && !vmecho[x][y] && (grid[x][y] == 'G' || grid[x][y] == 'D');
}

bool valid(int m) {
	// initialize
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			dmecho[i][j] = dbee[i][j] = 1e18;
			vmecho[i][j] = vbee[i][j] = false;
		}
	}
	// mecho eats and bee move
	for (auto&u : hive) {
		int x = u.first, y = u.second;
		dbee[x][y] = 0, qbee.push({x, y});
	}
	for (int i = 1; i <= n*n; i++) {
		queue<pair<int, int>> nex;
		while (qbee.size()) {
			int x = qbee.front().first, y = qbee.front().second; qbee.pop();
			for (int k = 0; k < 4; k++) {
				int tox = x + dx[k], toy = y + dy[k];
				if (!bee_inbound(tox, toy)) continue;
				if (dbee[tox][toy] <= max(1ll, i - m + 1)) continue;
				dbee[tox][toy] = max(1ll, i - m + 1), vbee[tox][toy] = true, nex.push({tox, toy});
			}
		}
		swap(nex, qbee);
	}
	// debug
	/*for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cout << (dbee[i][j] == 1e18 ? -1 : dbee[i][j]) << ' ';
		}
		cout << '\n';
	}*/
	// debug
	int x = init.first, y = init.second;
	dmecho[x][y] = 0, vmecho[x][y] = true, qmecho.push({x, y});
	for (int i = 1; i <= (n*n + s - 1)/s; i++) {
		for (int j = 1; j <= s; j++) {
			queue<pair<int, int>> nex;
			while (qmecho.size()) {
				int x = qmecho.front().first, y = qmecho.front().second; qmecho.pop();
				if (dmecho[x][y] >= dbee[x][y]) continue;
				for (int k = 0; k < 4; k++) {
					int tox = x + dx[k], toy = y + dy[k];
					if (!mecho_inbound(tox, toy)) continue;
					if (dmecho[tox][toy] <= i) continue;
					dmecho[tox][toy] = i, vmecho[tox][toy] = true, nex.push({tox, toy});
				}
			}
			swap(nex, qmecho);
		}
	}
	x = home.first, y = home.second;
	return dmecho[x][y] < dbee[x][y];
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n >> s;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> grid[i][j];
			if (grid[i][j] == 'M') init = {i, j};
			else if (grid[i][j] == 'D') home = {i, j};
			else if(grid[i][j] == 'H') hive.push_back({i, j});
		}
	}
	int l = 0, r = 1000, ans = 0;
	while (l <= r) {
		int m = (l + r)/2;
		if (valid(m)) ans = m, l = m + 1;
		else r = m - 1;
	}
	cout << ans << '\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...