Submission #474827

#TimeUsernameProblemLanguageResultExecution timeMemory
474827dqkTracks in the Snow (BOI13_tracks)C++17
97.81 / 100
1629 ms1048580 KiB
#include <bits/stdc++.h>

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int n, m;
    std::cin >> n >> m;
    std::vector<std::string> g(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> g[i];
    }
    std::vector<std::vector<int>> comp(n, std::vector<int>(m, -1));
    std::vector<std::vector<int>> adj;
    int num = 0;
    std::function<void(int, int, char)> dfs=[&](int x, int y, char c) {
        if (x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '.')
            return;
        if (comp[x][y] != -1 && comp[x][y] != num) {
            adj[comp[x][y]].push_back(num);
            adj[num].push_back(comp[x][y]);
        }
        if (comp[x][y] != -1 || g[x][y] != c)
            return;

        comp[x][y] = num;
        dfs(x + 1, y, c);
        dfs(x - 1, y, c);
        dfs(x, y + 1, c);
        dfs(x, y - 1, c);
    };
    for (int x = 0; x < n; ++x) {
        for (int y = 0; y < m; ++y) {
            if (comp[x][y] != -1 || g[x][y] == '.')
                continue;
            adj.push_back({});
            dfs(x, y, g[x][y]);
            num++;
        }
    }

    std::vector<int> d(num, 0);
    std::queue<int> q;
    q.push(0);
    d[0] = 1;
    int ans = 1;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int v : adj[u]) {
            if (d[v] == 0) {
                q.push(v);
                d[v] = d[u] + 1;
                ans = std::max(ans, d[v]);
            }
        }
    }
    std::cout << ans << "\n";
    return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...