-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFlood Fill alphabet match.cpp
More file actions
58 lines (46 loc) · 932 Bytes
/
Flood Fill alphabet match.cpp
File metadata and controls
58 lines (46 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;
const int N = 100;
int a[N][N], vis[N][N];
int n, m;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void flood_fill(int x, int y, int col) {
vis[x][y] = 1;
for (int i = 0; i < 4; i++) {
int xx = x + dx[i];
int yy = y + dy[i];
if (xx >= 0 && xx < n && yy >= 0 && yy < m && a[x][y] == a[xx][yy] && !vis[xx][yy]) {
flood_fill(xx, yy, col);
}
}
a[x][y] = col;
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char ch;
cin >> ch;
a[i][j] = ch - 'A' + 1;
}
}
int col = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!vis[i][j]) {
col++;
flood_fill(i, j, col);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << a[i][j] << " ";
} cout << '\n';
}
return 0;
}