Submission #1036260

#TimeUsernameProblemLanguageResultExecution timeMemory
1036260akmeena5726Tracks in the Snow (BOI13_tracks)C++17
100 / 100
532 ms222276 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int INF = LLONG_MAX;

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

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    // freopen("input.in", "r", stdin);
    // freopen("output.out", "w", stdout);

    int h, w;
    cin >> h >> w;

    grid.resize(h);
    for (string &x : grid) cin >> x;

    vector<vector<int>> dist(h, vector<int>(w, INF));

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

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

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

            if (nx >= 0 && nx < h && ny >= 0 && ny < w && grid[nx][ny] != '.' && dist[nx][ny] == INF) {
                if (grid[nx][ny] == grid[x][y]) {
                    dist[nx][ny] = dist[x][y];
                    q.push_front({nx, ny});
                } else {
                    dist[nx][ny] = dist[x][y] + 1;
                    q.push_back({nx, ny});   
                }
            }
        }
    }

    int maxi = -INF;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (dist[i][j] != INF) {
                maxi = max(maxi, dist[i][j]);
            }
        }
    }

    cout << maxi << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...