#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " " << x << endl
#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
typedef long long llint;
const int MAXN = 1010;
const int dr[] = {1, 0, -1, 0};
const int ds[] = {0, 1, 0, -1};
char grid[MAXN][MAXN];
int R, S, comp_cnt;
int comp[MAXN][MAXN];
set<pair<int, int>> E;
vector<vector<int>> v;
inline bool inside(int r, int s) {
return r >= 0 && r < R && s >= 0 && s < S;
}
inline bool check(int r, int s) {
return r >= 0 && r < R && s >= 0 && s < S && comp[r][s] == -1;
}
void bfs(int r, int s, int curr_comp) {
queue<pair<int, int>> Q;
comp[r][s] = curr_comp;
Q.push({r, s});
while (!Q.empty()) {
auto curr = Q.front(); Q.pop();
for (int i = 0; i < 4; ++i) {
int rr = curr.first + dr[i], ss = curr.second + ds[i];
if (check(rr, ss) && grid[rr][ss] == grid[r][s]) {
comp[rr][ss] = comp[r][s];
Q.push({rr, ss});
}
}
}
}
int main(void) {
scanf("%d%d", &R, &S);
for (int i = 0; i < R; ++i)
scanf("%s", grid[i]);
memset(comp, -1, sizeof comp);
for (int i = 0; i < R; ++i)
for (int j = 0; j < S; ++j)
if (comp[i][j] == -1 && grid[i][j] != '*')
bfs(i, j, comp_cnt++);
v.resize(comp_cnt);
for (int i = 0; i < R; ++i) {
for (int j = 0; j < S; ++j) {
if (comp[i][j] == -1) continue;
for (int k = 0; k < 4; ++k) {
int ii = i + dr[k], jj = j + ds[k];
if (!inside(ii, jj) || comp[ii][jj] == -1 || comp[ii][jj] == comp[i][j])
continue;
if (E.find({comp[i][j], comp[ii][jj]}) == E.end()) {
E.insert({comp[i][j], comp[ii][jj]});
v[comp[i][j]].push_back(comp[ii][jj]);
}
}
}
}
vector<bool> bio(comp_cnt, false);
queue<pair<int, int>> Q;
bio[0] = true;
Q.push({0, 1});
int sol = 0;
while (!Q.empty()) {
auto curr = Q.front(); Q.pop();
sol = max(sol, curr.second);
for (int nxt : v[curr.first]) {
if (bio[nxt]) continue;
bio[nxt] = true;
Q.push({nxt, curr.second + 1});
}
}
printf("%d\n", sol);
return 0;
}
Compilation message
zoo.cpp: In function 'int main()':
zoo.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &R, &S);
~~~~~^~~~~~~~~~~~~~~~
zoo.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", grid[i]);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4344 KB |
Output is correct |
2 |
Correct |
9 ms |
4472 KB |
Output is correct |
3 |
Correct |
5 ms |
4348 KB |
Output is correct |
4 |
Correct |
6 ms |
4344 KB |
Output is correct |
5 |
Correct |
5 ms |
4472 KB |
Output is correct |
6 |
Correct |
6 ms |
4472 KB |
Output is correct |
7 |
Correct |
6 ms |
4448 KB |
Output is correct |
8 |
Correct |
8 ms |
4728 KB |
Output is correct |
9 |
Correct |
8 ms |
4728 KB |
Output is correct |
10 |
Correct |
8 ms |
4728 KB |
Output is correct |
11 |
Correct |
8 ms |
4600 KB |
Output is correct |
12 |
Correct |
9 ms |
4856 KB |
Output is correct |
13 |
Correct |
8 ms |
4600 KB |
Output is correct |
14 |
Correct |
8 ms |
4728 KB |
Output is correct |
15 |
Correct |
8 ms |
4728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4344 KB |
Output is correct |
2 |
Correct |
9 ms |
4472 KB |
Output is correct |
3 |
Correct |
5 ms |
4348 KB |
Output is correct |
4 |
Correct |
6 ms |
4344 KB |
Output is correct |
5 |
Correct |
5 ms |
4472 KB |
Output is correct |
6 |
Correct |
6 ms |
4472 KB |
Output is correct |
7 |
Correct |
6 ms |
4448 KB |
Output is correct |
8 |
Correct |
8 ms |
4728 KB |
Output is correct |
9 |
Correct |
8 ms |
4728 KB |
Output is correct |
10 |
Correct |
8 ms |
4728 KB |
Output is correct |
11 |
Correct |
8 ms |
4600 KB |
Output is correct |
12 |
Correct |
9 ms |
4856 KB |
Output is correct |
13 |
Correct |
8 ms |
4600 KB |
Output is correct |
14 |
Correct |
8 ms |
4728 KB |
Output is correct |
15 |
Correct |
8 ms |
4728 KB |
Output is correct |
16 |
Correct |
27 ms |
5624 KB |
Output is correct |
17 |
Correct |
25 ms |
5756 KB |
Output is correct |
18 |
Correct |
29 ms |
5596 KB |
Output is correct |
19 |
Correct |
28 ms |
5724 KB |
Output is correct |
20 |
Correct |
23 ms |
5624 KB |
Output is correct |
21 |
Correct |
398 ms |
42252 KB |
Output is correct |
22 |
Correct |
387 ms |
41976 KB |
Output is correct |
23 |
Correct |
390 ms |
42428 KB |
Output is correct |
24 |
Correct |
411 ms |
44152 KB |
Output is correct |
25 |
Correct |
412 ms |
43256 KB |
Output is correct |
26 |
Correct |
394 ms |
42792 KB |
Output is correct |
27 |
Correct |
435 ms |
41976 KB |
Output is correct |
28 |
Correct |
381 ms |
41848 KB |
Output is correct |
29 |
Correct |
416 ms |
43768 KB |
Output is correct |
30 |
Correct |
415 ms |
43444 KB |
Output is correct |