Submission #700979

#TimeUsernameProblemLanguageResultExecution timeMemory
700979phiTracks in the Snow (BOI13_tracks)C++17
0 / 100
633 ms215988 KiB
#include <vector>
#include <utility>
#include <iostream>
#include <string>
#include <deque>
#include <cmath>

using namespace std;

vector<int> dx = { 1, 1, -1, -1 };
vector<int> dy = { 1, -1, 1, -1 };

int main() {
    int h, w;
    cin >> h >> w;

    vector<string> snow(h);
    for (int i = 0; i < h; i++) cin >> snow[i];

    vector<vector<int>> depth(h, vector<int>(w));

    auto inside = [&](int x, int y) {
        return 0 <= x && x < w && 0 <= y && y < h && snow[x][y] != '.';
    };

    int ans = 1;

    deque<pair<int, int>> visit;
    visit.push_back({ 0, 0 });
    depth[0][0] = 1;

    while (!visit.empty()) {
        auto [ x, y ] = visit.front();
        visit.pop_front();

        ans = max(ans, depth[x][y]);

        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (inside(nx, ny) && depth[nx][ny] == 0) {
                if (snow[x][y] == snow[nx][ny]) {
                    depth[nx][ny] = depth[x][y];
                    visit.push_front({ nx, ny });
                } else {
                    depth[nx][ny] = depth[x][y] + 1;
                    visit.push_back({ nx, ny });
                }
            }
        }
    }

    cout << ans - 1;

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