제출 #344801

#제출 시각아이디문제언어결과실행 시간메모리
344801JovanK26Tracks in the Snow (BOI13_tracks)C++14
100 / 100
1084 ms978416 KiB
#include <bits/stdc++.h>

using namespace std;
int n,m;
string mat[4000];
int addc[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
vector< array<int,2> > pt[2];
int rez=0;
bool check(int i,int j)
{
    if(i<0 || i>=n || j<0 || j>=m || mat[i][j]=='.')return 0;
    return 1;
}
void dfs(int x,int y)
{
    char cur=mat[x][y];
    mat[x][y]='.';
    for(int i=0;i<4;i++)
    {
        int c1=x+addc[i][0];
        int c2=y+addc[i][1];
        if(!check(c1,c2))
        {
            continue;
        }
        if(mat[c1][c2]==cur)
        {
            dfs(c1,c2);
        }
        else
        {
            pt[(rez&1)^1].push_back({c1,c2});
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for(int i=0;i<n;i++)
    {
        cin >> mat[i];
    }
    pt[rez&1].push_back({0,0});
    while(pt[rez&1].size())
    {
        for(array<int,2> p : pt[rez&1])
        {
            if(check(p[0],p[1]))
            {
                dfs(p[0],p[1]);
            }
        }
        pt[rez&1].clear();
        rez++;
    }
    cout << rez;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...