제출 #1001006

#제출 시각아이디문제언어결과실행 시간메모리
1001006vjudge1Tracks in the Snow (BOI13_tracks)C++17
100 / 100
422 ms118800 KiB
#include <bits/stdc++.h>
using i64 = long long;

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

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

    int h, w;
    std::cin >> h >> w;
    
    std::vector<std::string> g(h);
    for (auto &s : g) {
        std::cin >> s;
    }

    std::vector dis(h, std::vector<int>(w, -1));
    dis[0][0] = 0;

    std::deque<std::tuple<int, int>> q;
    q.push_back({0, 0});

    int ans = 0;
    while (q.size()) {
        auto [x, y] = q.front();
        q.pop_front();

        ans = std::max(ans, dis[x][y]);
        
        for (int i = 0; i < 4; i++) {
            int tx = x + dx[i];
            int ty = y + dy[i];
            if (tx < 0 || tx >= h || ty < 0 || ty >= w || g[tx][ty] == '.' || dis[tx][ty] != -1) {
                continue;
            }
            
            if (g[x][y] == g[tx][ty]) {
                dis[tx][ty] = dis[x][y];
                q.push_front({tx, ty});
            } else {
                dis[tx][ty] = dis[x][y] + 1;
                q.push_back({tx, ty});
            }
        }
    }
    std::cout << 1 + ans << "\n";

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