제출 #1029856

#제출 시각아이디문제언어결과실행 시간메모리
1029856VectorLiTracks in the Snow (BOI13_tracks)C++14
100 / 100
629 ms130896 KiB
#include <bits/stdc++.h>
#define long long long

using namespace std;

const int N = 4000;
const int B = numeric_limits<int>::max();
const int X[4] = {0, 0, 1, -1}, Y[4] = {1, -1, 0, 0};

int n, m;
string a[N];

deque<pair<int, int>> q;
int d[N][N];

bool valid(int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < m && a[x][y] != '.';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    q.push_back({0, 0});
    for (int i = 0; i < n; i++) {
        fill(d[i], d[i] + m, B);
    }
    d[0][0] = 0;

    int c = 0;
    while (not q.empty()) {
        int x0, y0;
        tie(x0, y0) = q.front();
        q.pop_front();
        c = max(c, d[x0][y0]);
        for (int i = 0; i < 4; i++) {
            int x1 = x0 + X[i], y1 = y0 + Y[i];
            if (valid(x1, y1) && d[x1][y1] > d[x0][y0] + (a[x0][y0] != a[x1][y1])) {
                d[x1][y1] = d[x0][y0] + (a[x0][y0] != a[x1][y1]);
                if (a[x0][y0] == a[x1][y1]) {
                    q.push_front({x1, y1});
                } else {
                    q.push_back({x1, y1});
                }
            }
        }
    }
    cout << c + 1 << "\n";

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...