Submission #847753

#TimeUsernameProblemLanguageResultExecution timeMemory
847753AdishTracks in the Snow (BOI13_tracks)C++14
100 / 100
1420 ms249132 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 40001;
int ans = 0;
vector<vector<bool>> vis(N, vector<bool>(N, false));
void bfs(vector<string>&s, int n, int m){
    queue<pair<int, int>>altr_q, q;
    altr_q.push({0, 0});
    vis[0][0] = true;
    while(!altr_q.empty()){
        ans++;
        q = altr_q;
        queue<pair<int, int>>q_;
        altr_q = q_;
        while(!q.empty()){
            pair<int, int>tp = q.front();
            q.pop();
            vector<int> dx = {-1, 0, 1, 0};
            vector<int> dy = {0, 1, 0, -1};
            for(int i = 0; i < 4; i++){
                int x = tp.first + dx[i];
                int y = tp.second + dy[i];
                if(x >= 0 and x < n and y >=0 and y < m){
                    if(s[x][y] == '.' or vis[x][y]){
                        continue;
                    }
                    vis[x][y] = true;
                    if(s[x][y] == s[tp.first][tp.second]){
                        q.push({x, y});
                    }
                    else{
                        altr_q.push({x, y});
                    }
                }
            }
        }
    }
}
int main(){
    int h, w;
    cin >> h >> w;
    vector<string>s(h);
    for(int i = 0; i < h; i++){
        cin >> s[i];
    }
    bfs(s, h, w);
    cout << ans << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...