This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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>>&q){
ans++;
queue<pair<int, int>>altr_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});
}
}
}
}
if(!altr_q.empty()){
bfs(s, n, m, altr_q);
}
}
int main(){
int h, w;
cin >> h >> w;
vector<string>s(h);
for(int i = 0; i < h; i++){
cin >> s[i];
}
queue<pair<int, int>>q;
q.push({0, 0});
vis[0][0] = true;
bfs(s, h, w, q);
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |