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;
typedef pair<int, int> ii;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int n, m;
char grid[4002][4002];
bool vis[4002][4002];
void bfs(int si, int sj, char c)
{
queue<ii> q;
vis[si][sj] = true;
q.push(ii(si, sj));
while(!q.empty()){
int ui = q.front().first, uj = q.front().second;
q.pop();
for(int i = 0; i < 4; i++){
int vi = ui + dx[i], vj = uj + dy[i];
if(!vis[vi][vj]){
if(grid[vi][vj] == grid[ui][uj]){
vis[vi][vj] = true;
q.push(ii(vi, vj));
}
}
}
}
}
int main()
{
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
cin>>n>>m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++) cin>>grid[i][j];
}
int fox = 0, rab = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(grid[i][j] == 'F'){
if(!vis[i][j]){
fox++;
bfs(i, j, 'F');
}
}
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(grid[i][j] == 'R'){
if(!vis[i][j]){
rab++;
bfs(i, j, 'R');
}
}
}
}
cout<<min(rab, fox) + 1;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |