Submission #1255340

#TimeUsernameProblemLanguageResultExecution timeMemory
1255340MisterReaperTracks in the Snow (BOI13_tracks)C++20
100 / 100
596 ms61448 KiB
// File U.cpp created on 09.08.2025 at 00:46:23
#include <bits/stdc++.h>

using i64 = long long;

#ifdef DEBUG 
    #include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
    #define debug(...) void(23)
#endif

constexpr int max_N = int(4000) + 5;

int N, M;
char A[max_N][max_N];
bool vis[max_N][max_N];

constexpr int dx[] = {0, +1, 0, -1};
constexpr int dy[] = {+1, 0, -1, 0};

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

    std::cin >> N >> M;
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
            std::cin >> A[i][j];
        }
    }

    std::queue<std::pair<int, int>> que;
    vis[0][0] = true;
    que.emplace(0, 0);
    for (int t = 1; ; ++t) {
        std::vector<std::pair<int, int>> vec;
        while (!que.empty()) {
            auto[x, y] = que.front();
            que.pop();
            for (int d = 0; d < 4; ++d) {
                int nx = x + dx[d], ny = y + dy[d];
                if (0 <= nx && nx < N && 0 <= ny && ny < M && vis[nx][ny] == false && A[nx][ny] != '.') {
                    if (A[nx][ny] == A[x][y]) {
                        vis[nx][ny] = true;
                        que.emplace(nx, ny);
                    } else {
                        vis[nx][ny] = true;
                        vec.emplace_back(nx, ny);
                    }
                }
            }
        }
        if (vec.empty()) {
            std::cout << t << '\n';
            break;
        }
        for (auto[x, y] : vec) {
            que.emplace(x, y);
        }
    }

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