Submission #1176447

#TimeUsernameProblemLanguageResultExecution timeMemory
1176447lzz666Tracks in the Snow (BOI13_tracks)C++20
100 / 100
878 ms113584 KiB
#include <bits/stdc++.h>
using namespace std;

bool check(int x, int y, int n, int m, const vector<vector<char>>& snow) {
    return (x > -1 && x < n && y > -1 && y < m && snow[x][y] != '.');
}

int main() {
    int n, m, res = 1; cin >> n >> m;
    vector<vector<char>> snow(n, vector<char> (m));
	for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> snow[i][j];
    vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
    vector<vector<int>> depth(n, vector<int> (m));
	deque<pair<int, int>> q;
	q.push_back({0, 0});
	depth[0][0] = 1;

	while (q.size()) {
		pair<int, int> cur = q.front();
		q.pop_front();
		res = max(res, depth[cur.first][cur.second]);

		for (int i = 0; i < 4; i++) {
			int x = cur.first + dx[i], y = cur.second + dy[i];
			if (check(x, y, n, m, snow) && depth[x][y] == 0) {
				if (snow[x][y] == snow[cur.first][cur.second]) {
					depth[x][y] = depth[cur.first][cur.second];
					q.push_front({x, y});
				} else {
					depth[x][y] = depth[cur.first][cur.second] + 1;
					q.push_back({x, y});
				}
			}
		}
	}
	cout << res << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...