Submission #954233

#TimeUsernameProblemLanguageResultExecution timeMemory
954233tnknguyen_Tracks in the Snow (BOI13_tracks)C++14
100 / 100
650 ms252420 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n' 
#define pii pair<int, int>

const int MX = 4005;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int INF;

char a[MX][MX];
int d[MX][MX];
int n, m;

bool valid(int x, int y){
    return (x >= 1 && x <= n && y >= 1 && y <= m);
}

int bfs(int x, int y){
    memset(d, 63, sizeof d);
    INF = d[0][0];
    d[1][1] = 1;
    deque<pii> q;
    q.push_front({1, 1});
    int ans = 1;
    
    while(q.size()){
        int x, y;
        tie(x, y) = q.front();
        q.pop_front();

        ans = max(ans, d[x][y]);

        for(int i=0; i<4; ++i){
            int u = x + dx[i];
            int v = y + dy[i];
            int c = (a[x][y] != a[u][v]);
            if(valid(u, v) && a[u][v] != '.' && d[u][v] == INF){
                d[u][v] = d[x][y] + c;
                if(c == 0){
                    q.push_front({u, v});
                }
                else{
                    q.push_back({u, v});
                }
            }
        }
    }

    return ans;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

//    freopen("main.inp","r",stdin);
//    freopen("main.out","w",stdout);

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

    cout<<bfs(1, 1);

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...