0823

Done IOI Start at: 2026-8-23 19:00 2 hour(s) Host: 2
#include <bits/stdc++.h>
using namespace std;

int n, m, t;
char mp[15][15];
int stx, sty, edx, edy;
long long d[15][15];
int dx[] = {0, 0, 1, -1};

int dy[] = {-1, 1, 0, 0};

int check(int k) {
	memset(d, 0x3f, sizeof(d));
	queue<pair<int, int>> q;
	q.push({stx, sty});
	d[stx][sty] = 0;

	while (!q.empty()) {
		auto [x, y] = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int xx = x + dx[i], yy = y + dy[i];
			if (xx < 1 || xx > n || yy < 1 || yy > m)
				continue;
			int add = (mp[xx][yy] == '#') ? k : 1;
			if (d[xx][yy] > d[x][y] + add) {
				d[xx][yy] = d[x][y] + add;
				q.push({xx, yy});
			}
		}
	}
	return d[edx][edy] <= t;
}

int main() {
	freopen("level.in","r",stdin);
	freopen("level.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n >> m >> t;
	for (int i = 1; i <= n; i++) {
		string s;
		cin >> s;
		for (int j = 1; j <= m; j++) {
			mp[i][j] = s[j - 1];
			if (mp[i][j] == 'S') {
				stx = i;
				sty = j;
			} else if (mp[i][j] == 'G') {
				edx = i;
				edy = j;
			}
		}
	}

	int l = 1, r = 1e18;
	int ans = 0;
	while (l <= r) {
		int mid = l + (r - l) / 2;
		if (check(mid)) {
			ans = mid;
			l = mid + 1;
		} else {
			r = mid - 1;
		}
	}
	cout << ans << "\n";
	return 0;
}
Status
Done
Rule
IOI
Problem
4
Start at
2026-8-23 19:00
End at
2026-8-23 21:00
Duration
2 hour(s)
Host
Partic.
2