Submission #415659

#TimeUsernameProblemLanguageResultExecution timeMemory
415659duyanh1704Tracks in the Snow (BOI13_tracks)C++14
100 / 100
847 ms119044 KiB
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;

typedef pair<int, int> ii;
const int maxN = 4000;
const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};

int n, m;
char a[maxN + 5][maxN + 5];
int d[maxN + 5][maxN + 5];

bool isInside(int i, int j){
    return (i >= 1 && j >= 1 && i <= n && j <= m && a[i][j] != '.');
}

int bfs(){
    int ret = 0;
    deque<ii> q;
    q.push_back({1, 1});
    d[1][1] = 1;
    while (!q.empty()){
        ii t = q.front();
        q.pop_front();
        ret = max(d[t.x][t.y], ret);
        for (int k = 0; k < 4; ++k){
            int u = t.x + di[k];
            int v = t.y + dj[k];
            if (!isInside(u, v)) continue;
            if (!d[u][v]){
                if (a[u][v] == a[t.x][t.y]){
                    d[u][v] = d[t.x][t.y];
                    q.push_front({u, v});
                }
                else{
                    d[u][v] = d[t.x][t.y] + 1;
                    q.push_back({u, v});
                }
            }
        }
    }
    return ret;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0);

    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j) cin >> a[i][j];

    cout << bfs();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...