제출 #1161424

#제출 시각아이디문제언어결과실행 시간메모리
1161424uakkesTracks in the Snow (BOI13_tracks)C++20
100 / 100
574 ms119152 KiB
//
// All truth are easy to understand once they are discovered.
// The point is to discover them
//
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 123;
int was[4001][4001];

void solve(){
    int n, m; cin >> n >> m;
    string g[n];
    for (int i = 0; i < n; i++) {
        cin >> g[i];
    }

    vector<int> dx = {0, 0, 1, -1};
    vector<int> dy = {1, -1, 0, 0};
    deque<pair<int, int>> dq;
    was[0][0] = 1;
    dq.push_front({0, 0});

    int ans = 0;

    while (!dq.empty()) {
        auto [x, y] = dq.front();
        dq.pop_front();
        ans = max(ans, was[x][y]);

        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];

            if (nx >= 0 && nx < n && ny >= 0 && ny < m && g[nx][ny] != '.' && was[nx][ny] == 0) {
                if (g[nx][ny] == g[x][y]) {
                    was[nx][ny] = was[x][y];
                    dq.push_front({nx, ny});
                } else {
                    was[nx][ny] = was[x][y] + 1;
                    dq.push_back({nx, ny});
                }
            }
        }
    }

    cout << ans;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int tt = 1;
//    cin >> tt;

    while (tt--) {
        solve();
        cout << '\n';
    }

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