제출 #517770

#제출 시각아이디문제언어결과실행 시간메모리
517770JomnoiTracks in the Snow (BOI13_tracks)C++17
100 / 100
523 ms126476 KiB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;

const int N = 4e3 + 10;
const int INF = 1e9 + 7;
const int r[] = {-1, 0, 1, 0};
const int c[] = {0, -1, 0, 1};

char a[N][N];
int dist[N][N];

int main() {
	cin.tie(0)->sync_with_stdio(0);
    int h, w;
    cin >> h >> w;
    for(int i = 1; i <= h; i++) {
        cin >> (a[i] + 1);
    }

	int ans = 0;
	deque <pair <int, int>> dq;
	dq.emplace_back(1, 1);
	dist[1][1] = 1;
	while(!dq.empty()) {
		auto [xi, yi] = dq.front();
		dq.pop_front();

		ans = max(ans, dist[xi][yi]);
		for(int d = 0; d < 4; d++) {
			int xj = xi + r[d], yj = yi + c[d];
			if(xj < 1 or xj > h or yj < 1 or yj > w or a[xj][yj] == '.') {
				continue;
			}

			int w = (a[xi][yi] != a[xj][yj]);
			if(dist[xj][yj] == 0) {
				dist[xj][yj] = dist[xi][yi] + w;
				if(w == 1) {
					dq.emplace_back(xj, yj);
				}
				else {
					dq.emplace_front(xj, yj);
				}
			}
		}
	}
	cout << ans;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...