Submission #689084

#TimeUsernameProblemLanguageResultExecution timeMemory
689084Anonymous_GuysTracks in the Snow (BOI13_tracks)C++17
100 / 100
938 ms125852 KiB
#include <bits/stdc++.h>


using namespace std;

#define pb push_back
#define ii pair<int, int>
const int mxN = 5e3;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int n, m;
string s[mxN];
int ans[mxN][mxN];

bool check(int x, int y) {
    return (x >=0 && x < n && y >= 0 && y < m && s[x][y] != '.');
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> s[i];

    deque<ii> dq;
    dq.pb({0, 0});
    ans[0][0] = 1;
    int kq =0 ;
    while(dq.size()) {
        ii it = dq.front();
        dq.pop_front();
        kq = max(kq, ans[it.first][it.second]);
        for (int i = 0; i < 4; ++i) {
            int x = it.first + dx[i];
            int y = it.second + dy[i];
            if (check(x, y) && ans[x][y] == 0) {
                if (s[x][y] == s[it.first][it.second]) {
                    ans[x][y] = ans[it.first][it.second];
                    dq.push_front({x, y});
                } else {
                    ans[x][y] = ans[it.first][it.second] + 1;
                    dq.push_back({x, y});
                }
            }

        }
    }
    cout << kq;
    return 0;
}

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