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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define debug(x) cerr << '[' << (#x) << "] = " << x << '\n';
template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
void PlayGround() {
int r, c; cin >> r >> c;
vector<string>g(r);
for(string& row : g) cin >> row;
vector<vector<bool>>vis(r, vector<bool>(c, 0));
int ans = 0, ans2 = 0;
auto valid = [&] (int i, int j) {
return i>-1 && i<r && j>-1 && j<c && vis[i][j]==false;
};
auto BFS = [&] (int x, int y) {
int dx[] {1, -1, 0, 0};
int dy[] {0, 0, 1, -1};
queue<pair<int,int>>q;
q.push(make_pair(x, y));
while(!q.empty()) {
int i = q.front().first, j = q.front().second;
q.pop();
if(vis[i][j]) continue;
vis[i][j] = true;
for(int k=0; k<4; ++k) {
int newi = i + dx[k], newj = j + dy[k];
if(valid(newi, newj) && g[newi][newj]==g[i][j])
q.push(make_pair(newi, newj));
}
}
};
char C = g[0][0];
bool other = false;
for(int i=0; i<r; ++i) {
for(int j=0; j<c; ++j) if(!vis[i][j] && g[i][j]==C) {
BFS(i, j);
++ans;
} else other |= g[i][j]!=C;
}
ans += other;
cout << ans << '\n';
#ifndef ONLINE_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// #endif
PlayGround();
return 0;
}
Compilation message (stderr)
zoo.cpp: In function 'void PlayGround()':
zoo.cpp:18:18: warning: unused variable 'ans2' [-Wunused-variable]
18 | int ans = 0, ans2 = 0;
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |