Submission #1154365

#TimeUsernameProblemLanguageResultExecution timeMemory
1154365newbie__1Tracks in the Snow (BOI13_tracks)C++20
100 / 100
1884 ms1042876 KiB
#include<bits/stdc++.h>
#define ll int
#define F first
#define S second
#define pb push_back
#define mpr make_pair

const ll N=5e5 + 10 , mod=1e9 + 7;

using namespace std;

ll n,a,m;
ll t[N];
string ch[4100];

ll c=0;
ll com[4100][4100];
bool vis[4100][4100];
ll x,y;

void ff(ll i,ll j){
    if(i<0 || i>=n || j<0 || j>=m || ch[x][y]!=ch[i][j] || vis[i][j])
        return;
    //cout<<i<<" "<<j<<" "<<x<<" "<<y<<"\n";
    vis[i][j]=1;
    com[i][j]=c;
    ff(i-1,j);
    ff(i+1,j);
    ff(i,j+1);
    ff(i,j-1);
    return ;
}





int main(){
  //  ios_base::sync_with_stdio (0); cin.tie(0),cout.tie(0);
//    freopen("odometer.in","r",stdin);
//    freopen("odometer.out","w",stdout);



    ll T=1;
  //  cin>>T;
    while(T--){
        cin>>n>>m;
        for(ll i=0;i<n;i++){
            cin>>ch[i];
        }
        for(ll i=0;i<n;i++){
            for(ll j=0;j<m;j++){
                if(!vis[i][j]){
                        c++;
                        x=i;y=j;
                        ff(i,j);
                }
                //cout<<com[i][j];
            }
           // cout<<"\n";
        }
       // cout<<"jsldkj";
        vector<vector<ll>>adj(c+1);
        for(ll i=0;i<n;i++){
            for(ll j=0;j<m;j++){
                if(ch[i][j]!='.') {
                    if(i+1<n){
                        if(ch[i][j]!=ch[i+1][j] && ch[i+1][j]!='.'){
                            adj[com[i][j]].pb(com[i+1][j]);
                            adj[com[i+1][j]].pb(com[i][j]);
                        }
                    }
                    if(j+1<m){
                        if(ch[i][j]!=ch[i][j+1] && ch[i][j+1]!='.'){
                            adj[com[i][j]].pb(com[i][j+1]);
                            adj[com[i][j+1]].pb(com[i][j]);
                        }
                    }
                }
            }
        }
        vector<ll>d(c+1,1e9);
        d[1]=0;
        queue<ll>q;
        q.push(1);
        ll res=0;
        while(!q.empty()){
            ll node=q.front();
            q.pop();
            for(ll x:adj[node]){
                if(d[x]>d[node]+1){
                    d[x]=d[node]+1;
                    q.push(x);
                    res=max(res,d[x]);
                }
            }
        }

        cout<<res+1<<"\n";

    }




    return 0;
}

/*

1 5 6 5

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