//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
char matrix[1005][1005];
int vis[1005 * 1005];
bool viss[1005][1005];
set <int> adj[1005 * 1005];
int comp[1005][1005];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
#define ONLINE_JUDGE
void solve() {
memset(vis, -1, sizeof(vis));
int r, c;
cin >> r >> c;
for(int i = 1; i <= r; i++) {
for(int j = 1; j <= c; j++) {
cin >> matrix[i][j];
}
}
auto in = [&](int x, int y) -> bool {
return 1 <= x && x <= r && 1 <= y && y <= c;
};
function <void(int, int, int)> dfs = [&](int x, int y, int c) -> void {
if(viss[x][y]) {
return;
}
viss[x][y] = true;
comp[x][y] = c;
for(int i = 0; i < 4; i++) {
int _x = x + dx[i], _y = y + dy[i];
if(in(_x, _y) && matrix[_x][_y] == matrix[x][y]) {
dfs(_x, _y, c);
}
}
};
int cnt = 0;
for(int i = 1; i <= r; i++) {
for(int j = 1; j <= c; j++) {
if(!viss[i][j] && matrix[i][j] != '*') {
dfs(i, j, cnt);
cnt++;
}
}
}
for(int i = 1; i <= r; i++) {
for(int j = 1; j <= c; j++) {
if(i +1 <= r && matrix[i][j] != matrix[i +1][j] && matrix[i][j] != '*' && matrix[i +1][j] != '*') {
adj[comp[i][j]].emplace(comp[i +1][j]);
}
if(j +1 <= c && matrix[i][j] != matrix[i][j +1] && matrix[i][j] != '*' && matrix[i][j +1] != '*') {
adj[comp[i][j]].emplace(comp[i][j +1]);
}
if(i -1 >= 1 && matrix[i][j] != matrix[i -1][j] && matrix[i][j] != '*' && matrix[i -1][j] != '*') {
adj[comp[i][j]].emplace(comp[i -1][j]);
}
if(j -1 >= 1 && matrix[i][j] != matrix[i][j -1] && matrix[i][j] != '*' && matrix[i][j -1] != '*') {
adj[comp[i][j]].emplace(comp[i][j -1]);
}
}
}
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <>> q;
q.emplace(1, 0);
while(!q.empty()) {
auto [d, x] = q.top();
q.pop();
if(vis[x] != -1) {
continue;
}
cerr << x << " " << d << "\n";
vis[x] = d;
for(const int child : adj[x]) {
if(vis[child] == -1) {
q.emplace(d +1, child);
}
}
}
cout << *max_element(vis, vis + 1005 * 1005);
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
54616 KB |
Output is correct |
2 |
Correct |
12 ms |
54620 KB |
Output is correct |
3 |
Correct |
12 ms |
54492 KB |
Output is correct |
4 |
Correct |
12 ms |
56668 KB |
Output is correct |
5 |
Correct |
13 ms |
56924 KB |
Output is correct |
6 |
Correct |
13 ms |
56808 KB |
Output is correct |
7 |
Correct |
15 ms |
56920 KB |
Output is correct |
8 |
Correct |
20 ms |
56924 KB |
Output is correct |
9 |
Correct |
20 ms |
57088 KB |
Output is correct |
10 |
Correct |
22 ms |
57160 KB |
Output is correct |
11 |
Correct |
20 ms |
57032 KB |
Output is correct |
12 |
Correct |
23 ms |
56924 KB |
Output is correct |
13 |
Correct |
21 ms |
56924 KB |
Output is correct |
14 |
Correct |
21 ms |
56920 KB |
Output is correct |
15 |
Correct |
20 ms |
56924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
54616 KB |
Output is correct |
2 |
Correct |
12 ms |
54620 KB |
Output is correct |
3 |
Correct |
12 ms |
54492 KB |
Output is correct |
4 |
Correct |
12 ms |
56668 KB |
Output is correct |
5 |
Correct |
13 ms |
56924 KB |
Output is correct |
6 |
Correct |
13 ms |
56808 KB |
Output is correct |
7 |
Correct |
15 ms |
56920 KB |
Output is correct |
8 |
Correct |
20 ms |
56924 KB |
Output is correct |
9 |
Correct |
20 ms |
57088 KB |
Output is correct |
10 |
Correct |
22 ms |
57160 KB |
Output is correct |
11 |
Correct |
20 ms |
57032 KB |
Output is correct |
12 |
Correct |
23 ms |
56924 KB |
Output is correct |
13 |
Correct |
21 ms |
56924 KB |
Output is correct |
14 |
Correct |
21 ms |
56920 KB |
Output is correct |
15 |
Correct |
20 ms |
56924 KB |
Output is correct |
16 |
Correct |
38 ms |
59484 KB |
Output is correct |
17 |
Correct |
47 ms |
59560 KB |
Output is correct |
18 |
Correct |
38 ms |
59476 KB |
Output is correct |
19 |
Correct |
46 ms |
59740 KB |
Output is correct |
20 |
Correct |
43 ms |
59720 KB |
Output is correct |
21 |
Correct |
1102 ms |
85800 KB |
Output is correct |
22 |
Correct |
1110 ms |
85740 KB |
Output is correct |
23 |
Correct |
1124 ms |
86152 KB |
Output is correct |
24 |
Correct |
1139 ms |
87368 KB |
Output is correct |
25 |
Correct |
1110 ms |
86544 KB |
Output is correct |
26 |
Correct |
1110 ms |
86528 KB |
Output is correct |
27 |
Correct |
1092 ms |
85744 KB |
Output is correct |
28 |
Correct |
1132 ms |
85448 KB |
Output is correct |
29 |
Correct |
1116 ms |
87140 KB |
Output is correct |
30 |
Correct |
1154 ms |
86608 KB |
Output is correct |