Submission #622971

#TimeUsernameProblemLanguageResultExecution timeMemory
622971Cubik65536Tracks in the Snow (BOI13_tracks)C++17
100 / 100
859 ms135396 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 4000;

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

int n, m;
int depth[MAX_N][MAX_N];
string snow[MAX_N];

bool inside(int x, int y) {
    return x > -1 && x < n && y > -1 && y < m && snow[x][y] != '.';
}

int main() {
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
        cin >> snow[i];
    }

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

    int result = 1;

    while (q.size()) {
        pair<int, int> cur = q.front();
        q.pop_front();
        result = max(result, depth[cur.first][cur.second]);

        for (int i = 0; i < 4; i++) {
            int x = cur.first + dx[i];
            int y = cur.second + dy[i];

            if (inside(x, y) && depth[x][y] == 0) {
                if (snow[x][y] == snow[cur.first][cur.second]) {
                    depth[x][y] = depth[cur.first][cur.second];
					q.push_front({x, y});
				} else {
					depth[x][y] = depth[cur.first][cur.second] + 1;
					q.push_back({x, y});
				}
            }
        }
    }

    cout << result << endl;

    return 0;

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