#include<bits/stdc++.h>
using namespace std;
#define int long long
int n, m, ans = 0;
bool visited[1001][1001];
char arr[1001][1001];
vector<pair<int, int>> cur;
vector<pair<int, int>> nextcur;
//dfs 4 hướng liên thông
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
void dfs(int x, int y)
{
visited[x][y] = true;
for (int i = 0; i < 4; i++){
int vx = x + dx[i];
int vy = y + dy[i];
if (vx <= n && vy <= m && min(vx, vy) > 0&& arr[vx][vy] != '*' && !visited[vx][vy]){
if (arr[vx][vy] == arr[x][y]) dfs(vx, vy);
else nextcur.push_back({vx, vy});
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> arr[i][j];
nextcur.emplace_back(1, 1);
while (!nextcur.empty()){
ans++;
cur = nextcur;
nextcur.clear();
//dfs cho tới khi hết tìm được dấu chân khác liên thông
for (pair<int, int> x : cur) dfs(x.first, x.second);
cur.clear();
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |