Submission #1285447

#TimeUsernameProblemLanguageResultExecution timeMemory
1285447abhi_atgTracks in the Snow (BOI13_tracks)C++20
100 / 100
913 ms225960 KiB
// Jai Shree Ram
#include<bits/stdc++.h>
using namespace std;
/* Abhi-Atg */
#define array int n;cin >> n;vector<long long> v(n);f(i,0,n)cin >> v[i];
#define out(_) for (auto &it :_){cout << it << " " ;}cout<<endl;
#define f(i,l,r) for(int i=l;i<r;i++)
#define ff(i,r,l) for(int i=r;i>=l;i--)
#define pb push_back
#define mod 1000000007
#define INDIA 998244353
#define ll long long 
#define all(v) v.begin(),v.end()
#define dbg cout << "Bharat\n"
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define minus cout << "-1\n"

void solve(){
    int n,m;cin >> n >> m;
    vector<string> v(n);
    for(int i=0;i<n;i++){
        cin >> v[i];
    }
    vector<vector<int>> vis(n,vector<int>(m,0));
    vector<pair<int,int>> q;
    vector<int> x={0,0,1,-1};
    vector<int> y={1,-1,0,0};
    q.push_back({0,0});
    vis[0][0]=1;
    int ans=0;
    while(q.size()){
        vector<pair<int,int>> p;
        for(int i=0;i<q.size();i++){
            for(int j=0;j<4;j++){
                int a=q[i].first+x[j];
                int b=q[i].second+y[j];
                if(a>=0 && a<n && b>=0 && b<m && v[a][b]!='.' && vis[a][b]==0){
                    if(v[a][b]==v[q[i].first][q[i].second]){
                        q.push_back({a,b});
                        vis[a][b]=1;
                    }
                    else{
                        p.push_back({a,b});
                        vis[a][b]=1;
                    }
                }
            }
        }
        q=p;
        ans++;
    }
    cout<<ans<<"\n";
}


int main(){

    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    // #ifndef ONLINE_JUDGE
    //     freopen("input.txt", "r", stdin);
    //     freopen("output.txt", "w", stdout);
    // #endif

    // freopen("input.in", "r", stdin);
    // freopen("output.out", "w", stdout);

    int t=1;
    // cin >> t;
    for(int _=1;_<=t;_++){
        // cout<<"Case #"<<_<<": ";
        solve();
    }

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