Submission #715643

#TimeUsernameProblemLanguageResultExecution timeMemory
715643fatemetmhrTracks in the Snow (BOI13_tracks)C++17
97.81 / 100
680 ms1048576 KiB
// ~ Be Name Khoda ~
 
#include <bits/stdc++.h>
//#pragma GCC optimize ("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,O3")
 
using namespace std;
 
typedef long long ll;
 
#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second
 
const int maxn  =  1e6   + 10;
const int maxn5 =  4e3   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   +  7;
const ll  inf   =  1e18;
 
int n, m, cnt = 0;
bool mark[maxn5][maxn5];
vector <pair<int, int>> nxt, tmp;
string s[maxn5];
 
void dfs(int x, int y){
    mark[x][y] = true;
    cnt++;
    if(cnt > maxn5 * maxn5 || nxt.size() > maxn5 * maxn5 * 4)
        exit(0);
    if(x && !mark[x - 1][y] && s[x - 1][y] != '.'){
        if(s[x - 1][y] == s[x][y])
            dfs(x - 1, y);
        else
            nxt.pb({x - 1, y});
    }
    if(x + 1 < n && !mark[x + 1][y] && s[x + 1][y] != '.'){
        if(s[x + 1][y] == s[x][y])
            dfs(x + 1, y);
        else
            nxt.pb({x + 1, y});
    }
    if(y && !mark[x][y - 1] && s[x][y - 1] != '.'){
        if(s[x][y - 1] == s[x][y])
            dfs(x, y - 1);
        else
            nxt.pb({x, y - 1});
    }
    if(y + 1 < m && !mark[x][y + 1] && s[x][y + 1] != '.'){
        if(s[x][y + 1] == s[x][y])
            dfs(x, y + 1);
        else
            nxt.pb({x, y + 1});
    }
}
 
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
 
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> s[i];
    dfs(0, 0);
    int ans = 1;
    while(nxt.size()){
        ans++;
        tmp.clear();
        for(auto u : nxt)
            tmp.pb(u);
        nxt.clear();
        for(auto u : tmp) if(!mark[u.fi][u.se])
            dfs(u.fi, u.se);
    }
    cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...