#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int inf = 1e9;
const int rc[]{0,0,1,-1};
const int cc[]{1,-1,0,0};
const int N = 4004;
char a[N][N];
int vis[N][N],used[N][N];
int ans = 0, n, m, id = 0;
void dfs(int x,int y, char c) {
vis[x][y] = id;
for (int i = 0; i < 4;i++) {
int nx = x + rc[i],ny = y + cc[i];
if (nx >=0 && nx < n && ny >= 0 && ny < m && vis[nx][ny] == 0 && a[nx][ny] == c) {
dfs(nx,ny,c);
}
}
}
void dfs2(int x,int y, char c) {
vis[x][y] = 1;
for (int i = 0; i < 4;i++) {
int nx = x + rc[i],ny = y + cc[i];
if (nx >=0 && nx < n && ny >= 0 && ny < m && !used[nx][ny] && (a[nx][ny] == c)) {
dfs2(nx,ny,c);
}
}
}
void solve() {
cin >> n >> m;
int ok = 0, ok1 = 0;
for (int i = 0; i < n;i++) {
for (int j = 0; j < m;j++) {
cin >> a[i][j];
if (a[i][j] == 'R') ok = 1;
if (a[i][j] == 'F') ok1 = 1;
vis[i][j]= 0,used[i][j] = 0;
}
}
char c = a[0][0];
for (int i = 0; i < n;i++) {
for (int j = 0; j < m;j++) {
if (c != a[i][j]) continue;
if (vis[i][j] == 0) {
id++;
dfs(i,j,c);
ans++;
}
}
}
for (int i = 0; i < n;i++) {
for (int j = 0; j < m;j++) {
if (c == a[i][j]) continue;
if (vis[i][j] == 0) {
id++;
dfs(i,j,a[i][j]);
ans++;
}
}
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |