Submission #1300646

#TimeUsernameProblemLanguageResultExecution timeMemory
1300646louisTracks in the Snow (BOI13_tracks)C++17
100 / 100
714 ms120176 KiB
#include <bits/stdc++.h>
using namespace std;

vector<string> matrix;
vector<vector<int>> dir{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int inside(int x, int y, int h, int w) {
    return x >= 0 and x < h and y >= 0 and y < w and matrix[x][y] != '.';
}

int main() {
    int h, w;
    cin >> h >> w;
    matrix.resize(h);
    for(int i = 0; i < h; i++) {
        cin >> matrix[i];
    }

    deque<pair<int, int>> q;
    vector<vector<int>> cost(h, vector<int>(w, INT_MAX));
    q.push_front({0, 0});
    cost[0][0] = 1;
    int maxVal = 1;

    while(!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop_front();
        
        for(int i = 0; i < 4; i++) {
            int nx = x + dir[i][0];
            int ny = y + dir[i][1];
            if(inside(nx, ny, h, w) and cost[nx][ny] == INT_MAX) {
                if(matrix[nx][ny] == matrix[x][y]) {
                    q.push_front({nx, ny});
                    cost[nx][ny] = cost[x][y];
                }
                else {
                    q.push_back({nx, ny});
                    cost[nx][ny] = cost[x][y] + 1;
                }
                maxVal = max(maxVal, cost[nx][ny]);
            }
        }
    }

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