Submission #767704

#TimeUsernameProblemLanguageResultExecution timeMemory
767704lukehsiaoTracks in the Snow (BOI13_tracks)C++14
100 / 100
486 ms130692 KiB
#include <bits/stdc++.h>
using namespace std;

const int mxN = 4000, INF = 2e9;

int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};

int H, W, depth[mxN][mxN];
string grid[mxN];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> H >> W;
	for (int i=0; i<H; ++i)
		cin >> grid[i];
	
	deque<pair<int, int>> dq;
	dq.push_front({0, 0});
	depth[0][0] = 1;
	
	int ans = 0;
	while (!dq.empty()) {
		int r = dq.front().first;
		int c = dq.front().second;
		dq.pop_front();

		ans = max(ans, depth[r][c]);

		for (int i=0; i<4; ++i) {
			int nr = r + dx[i], nc = c + dy[i];
			if (nr >= 0 && nr < H && nc >= 0 && nc < W &&
					grid[nr][nc] != '.' && depth[nr][nc] == 0) {
				if (grid[r][c] == grid[nr][nc]) {
					depth[nr][nc] = depth[r][c];
					dq.push_front({nr, nc});
				} else {
					depth[nr][nc] = depth[r][c] + 1;
					dq.push_back({nr, nc});
				}
			}
		}
	}

	cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...