Submission #27487

#TimeUsernameProblemLanguageResultExecution timeMemory
27487TAMREFTracks in the Snow (BOI13_tracks)C++11
100 / 100
1999 ms595580 KiB
#include <bits/stdc++.h>
using namespace std;
const int mx=4002, ms=4000*4000+1;
vector<int> G[ms];
int rep[ms];
char F[4000][mx];
int ans=0, rt=1, d, n, m, g=1;
void bfs(){
    memset(rep,0,sizeof(rep));
    queue<int> node,dep;
    node.push(rt);
    dep.push(1);
    for(;!node.empty();){
        int now=node.front(), dnow=dep.front();
        node.pop(); dep.pop();
        if(rep[now]) continue;
        rep[now]=1; d=max(d,dnow);
        for(int u : G[now]){
            if(!rep[u]){
                node.push(u);
                dep.push(dnow+1);
            }
        }
    }
}
void input(){
    scanf("%d %d\n",&n,&m);
    for(int i=0;i<n;i++) fgets(F[i],mx,stdin);
}
void gennode(){
    for(int now=0;now<n*m;++now){
        if(rep[now]||F[now/m][now%m]=='.') continue;
        queue<int> Q;
        Q.push(now); rep[now]=g;
        char cvar=F[now/m][now%m];
        for(int p;!Q.empty();){
            p=Q.front(); Q.pop();
            rep[p]=g;
            if(p%m<m-1){
                int r=p+1;
                char var=F[r/m][r%m];
                if(var==cvar && !rep[r]){
                    rep[r]=g;
                    Q.push(r);
                }
                else if(var!='.'){
                    if(rep[r] && var!=cvar && (G[rep[r]].empty() || G[rep[r]].back()!=g)){
                        G[g].push_back(rep[r]);
                        G[rep[r]].push_back(g);
                    }
                }
            }
            if(p%m>0){
                int r=p-1;
                char var=F[r/m][r%m];
                if(var==cvar && !rep[r]){
                    rep[r]=g;
                    Q.push(r);
                }
                else if(var!='.'){
                    if(rep[r] && var!=cvar && (G[rep[r]].empty() || G[rep[r]].back()!=g)){
                        G[g].push_back(rep[r]);
                        G[rep[r]].push_back(g);
                    }
                }
            }
            if(p/m<n-1){
                int r=p+m;
                char var=F[r/m][r%m];
                if(var==cvar && !rep[r]){
                    rep[r]=g;
                    Q.push(r);
                }
                else if(var!='.'){
                    if(rep[r] && var!=cvar && (G[rep[r]].empty() || G[rep[r]].back()!=g)){
                        G[g].push_back(rep[r]);
                        G[rep[r]].push_back(g);
                    }
                }
            }
            if(p/m>0){
                int r=p-m;
                char var=F[r/m][r%m];
                if(var==cvar && !rep[r]){
                    rep[r]=g;
                    Q.push(r);
                }
                else if(var!='.'){
                    if(rep[r] && var!=cvar && (G[rep[r]].empty() || G[rep[r]].back()!=g)){
                        G[g].push_back(rep[r]);
                        G[rep[r]].push_back(g);
                    }
                }
            }
        }
        ++g;
    }
}
int main(){
    input();
    gennode();
/*
    printf("%d\n",g);
    for(int i=1;i<g;i++,puts("")) for(int x : G[i]) printf("%d ",x);
*/
    bfs();
    printf("%d\n",d);
    return 0;
}

Compilation message (stderr)

tracks.cpp: In function 'void input()':
tracks.cpp:27:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d\n",&n,&m);
                           ^
tracks.cpp:28:46: warning: ignoring return value of 'char* fgets(char*, int, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=0;i<n;i++) fgets(F[i],mx,stdin);
                                              ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...