제출 #931371

#제출 시각아이디문제언어결과실행 시간메모리
931371sofija6Tracks in the Snow (BOI13_tracks)C++14
91.25 / 100
2025 ms1048576 KiB
#include <bits/stdc++.h>
#define ll int
#define MAXN 4001
using namespace std;
char c[MAXN][MAXN];
ll a[MAXN][MAXN],cur=1,dist[MAXN*MAXN];
bool V[MAXN][MAXN];
set<pair<ll,ll> > s;
vector<ll> G[MAXN*MAXN];
void DFS(ll i,ll j)
{
    a[i][j]=cur;
    V[i][j]=true;
    for (ll x=-1;x<2;x++)
    {
        for (ll y=-1;y<2;y++)
        {
            if (abs(x)+abs(y)==1)
            {
                if (c[i+x][j+y]==c[i][j] && !V[i+x][j+y])
                    DFS(i+x,j+y);
                else if (c[i+x][j+y]!=c[i][j] && V[i+x][j+y] && !s.count({a[i+x][j+y],cur}))
                {
                    G[a[i+x][j+y]].push_back(cur);
                    G[cur].push_back(a[i+x][j+y]);
                    s.insert({a[i+x][j+y],cur});
                }
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll h,w;
    cin >> h >> w;
    for (ll i=1;i<=h;i++)
    {
        for (ll j=1;j<=w;j++)
            cin >> c[i][j];
    }
    for (ll i=1;i<=h;i++)
    {
        for (ll j=1;j<=w;j++)
        {
            if (!V[i][j] && c[i][j]!='.')
            {
                DFS(i,j);
                cur++;
            }
        }
    }
    queue<ll> q;
    q.push(1);
    dist[1]=1;
    ll ans=0;
    while (!q.empty())
    {
        ll i=q.front();
        q.pop();
        ans=max(ans,dist[i]);
        for (ll next : G[i])
        {
            if (!dist[next])
            {
                dist[next]=dist[i]+1;
                q.push(next);
            }
        }
    }
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...