#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1005;
int N, M;
char ch[NMAX][NMAX];
bool Interior (pair<int,int> x) {
return 0 < x.first && x.first <= N && 0 < x.second && x.second <= M;
}
void Read () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
for (int i = 1; i <= N; ++ i )
for (int j = 1; j <= M; ++ j )
cin >> ch[i][j];
}
int viz[NMAX][NMAX];
int vf = 0;
vector <int> G[NMAX*NMAX];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void Lee (int L, int C, char car) {
++ vf;
viz[L][C] = vf;
queue <pair<int,int> > Q;
set <int> S;
Q.push({L, C});
while (!Q.empty()) {
pair<int,int> x = Q.front();
Q.pop();
for (int i = 0; i < 4; ++ i ) {
pair<int,int> vecin = make_pair(x.first + dx[i], x.second + dy[i]);
if (!Interior(vecin) || ch[vecin.first][vecin.second] == '*' || viz[vecin.first][vecin.second] == vf) continue;
if (viz[vecin.first][vecin.second] != 0) {
S.insert(viz[vecin.first][vecin.second]);
continue;
}
if (ch[vecin.first][vecin.second] == car) {
Q.push(vecin);
viz[vecin.first][vecin.second] = vf;
}
}
}
while (!S.empty()) {
int it = *S.begin();
S.erase(S.begin());
G[it].push_back(vf);
G[vf].push_back(it);
}
}
bool sel[NMAX*NMAX];
void Bfs (int Node) {
queue <pair<int,int> > Q;
Q.push({Node, 1});
for (int i = 1; i <= vf; ++ i )
sel[i] = 0;
sel[Node] = 1;
int Max = 0;
while (!Q.empty()) {
int x = Q.front().first;
int lung = Q.front().second;
Max = max(Max, lung);
Q.pop();
for (int i = 0; i < G[x].size(); ++ i ) {
int it = G[x][i];
if (sel[it]) continue;
Q.push({it, lung+1});
sel[it] = 1;
}
}
cout << Max << '\n';
}
void Solve () {
for (int i = 1; i <= N; ++ i ) {
for (int j = 1; j <= M; ++ j ) {
if (ch[i][j] == '*') continue;
if (viz[i][j] != 0) continue;
Lee(i, j, ch[i][j]);
}
}
Bfs(1);
}
int main () {
Read();
Solve();
return 0;
}
Compilation message
zoo.cpp: In function 'void Bfs(int)':
zoo.cpp:84:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for (int i = 0; i < G[x].size(); ++ i ) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24024 KB |
Output is correct |
2 |
Correct |
13 ms |
24020 KB |
Output is correct |
3 |
Correct |
13 ms |
24184 KB |
Output is correct |
4 |
Correct |
13 ms |
24296 KB |
Output is correct |
5 |
Correct |
17 ms |
24532 KB |
Output is correct |
6 |
Correct |
15 ms |
24572 KB |
Output is correct |
7 |
Correct |
18 ms |
24532 KB |
Output is correct |
8 |
Correct |
15 ms |
24572 KB |
Output is correct |
9 |
Correct |
17 ms |
24572 KB |
Output is correct |
10 |
Correct |
15 ms |
24576 KB |
Output is correct |
11 |
Correct |
14 ms |
24532 KB |
Output is correct |
12 |
Correct |
16 ms |
24624 KB |
Output is correct |
13 |
Correct |
14 ms |
24444 KB |
Output is correct |
14 |
Correct |
14 ms |
24624 KB |
Output is correct |
15 |
Correct |
14 ms |
24404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24024 KB |
Output is correct |
2 |
Correct |
13 ms |
24020 KB |
Output is correct |
3 |
Correct |
13 ms |
24184 KB |
Output is correct |
4 |
Correct |
13 ms |
24296 KB |
Output is correct |
5 |
Correct |
17 ms |
24532 KB |
Output is correct |
6 |
Correct |
15 ms |
24572 KB |
Output is correct |
7 |
Correct |
18 ms |
24532 KB |
Output is correct |
8 |
Correct |
15 ms |
24572 KB |
Output is correct |
9 |
Correct |
17 ms |
24572 KB |
Output is correct |
10 |
Correct |
15 ms |
24576 KB |
Output is correct |
11 |
Correct |
14 ms |
24532 KB |
Output is correct |
12 |
Correct |
16 ms |
24624 KB |
Output is correct |
13 |
Correct |
14 ms |
24444 KB |
Output is correct |
14 |
Correct |
14 ms |
24624 KB |
Output is correct |
15 |
Correct |
14 ms |
24404 KB |
Output is correct |
16 |
Correct |
41 ms |
29780 KB |
Output is correct |
17 |
Correct |
44 ms |
29828 KB |
Output is correct |
18 |
Correct |
33 ms |
29952 KB |
Output is correct |
19 |
Correct |
34 ms |
29848 KB |
Output is correct |
20 |
Correct |
32 ms |
29840 KB |
Output is correct |
21 |
Correct |
126 ms |
36872 KB |
Output is correct |
22 |
Correct |
153 ms |
36696 KB |
Output is correct |
23 |
Correct |
142 ms |
36856 KB |
Output is correct |
24 |
Correct |
144 ms |
37496 KB |
Output is correct |
25 |
Correct |
144 ms |
37328 KB |
Output is correct |
26 |
Correct |
143 ms |
37012 KB |
Output is correct |
27 |
Correct |
140 ms |
36928 KB |
Output is correct |
28 |
Correct |
132 ms |
36756 KB |
Output is correct |
29 |
Correct |
134 ms |
37380 KB |
Output is correct |
30 |
Correct |
160 ms |
37116 KB |
Output is correct |