Submission #938055

#TimeUsernameProblemLanguageResultExecution timeMemory
938055TAhmed33Tracks in the Snow (BOI13_tracks)C++98
100 / 100
591 ms122464 KiB
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
const int MAXN = 4000;
int n, m;
char a[MAXN][MAXN];
bool check (int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < m && a[x][y] != '.';
}
int dist[MAXN][MAXN];
deque <pair <int, int>> cur;
signed main () {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j];
 	cur.push_back({0, 0}); 
 	for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) dist[i][j] = 1e9;
 	dist[0][0] = 1;
	int ans = 1;
	while (!cur.empty()) {
		auto k = cur.front(); cur.pop_front();
		ans = max(ans, dist[k.first][k.second]);
		for (int i = 0; i < 4; i++) {
			int x = k.first + dx[i], y = k.second + dy[i];
			if (!check(x, y)) continue;
			if (a[x][y] == a[k.first][k.second]) {
				if (dist[k.first][k.second] < dist[x][y]) {
					dist[x][y] = dist[k.first][k.second];
					cur.push_front({x, y});
				}
			} else {
				if (dist[k.first][k.second] + 1 < dist[x][y]) {
					dist[x][y] = dist[k.first][k.second] + 1;
					cur.push_back({x, y});
				}
			}
		}
	}
	cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...