#include "bits/stdc++.h"
using namespace std;
const int MAXN = 4001;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int n, m;
char ar[MAXN][MAXN];
bool vis[MAXN][MAXN];
char cur;
queue<pair<int, int>> qcur;
queue<pair<int, int>> qnext;
void dfs(queue<pair<int, int>> q) {
while (!q.empty()) {
pair<int, int> v = q.front();
q.pop();
if (vis[v.first][v.second]) continue;
vis[v.first][v.second] = true;
for (int i = 0; i < 4; i++) {
pair<int, int> u = {v.first + dx[i], v.second + dy[i]};
if (u.first < 0 || u.second < 0 || u.first >= n || u.second >= m) continue;
if (ar[u.first][u.second] == '.') continue;
if (vis[u.first][u.second]) continue;
if (ar[u.first][u.second] == cur)
q.push(u);
else
qnext.push(u);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> ar[i][j];
int ans = 0;
qcur.push({0, 0});
cur = ar[0][0];
while (!qcur.empty()) {
queue<pair<int, int>> qtmp = qnext;
while (!qnext.empty()) qnext.pop();
cur = ar[qcur.front().first][qcur.front().second];
dfs(qcur);
qcur = qnext;
ans++;
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
4692 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
11 ms |
4984 KB |
Output is correct |
5 |
Correct |
4 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
4 ms |
2260 KB |
Output is correct |
11 |
Correct |
3 ms |
2144 KB |
Output is correct |
12 |
Correct |
8 ms |
2772 KB |
Output is correct |
13 |
Correct |
4 ms |
2644 KB |
Output is correct |
14 |
Correct |
4 ms |
2580 KB |
Output is correct |
15 |
Correct |
17 ms |
4644 KB |
Output is correct |
16 |
Correct |
20 ms |
4744 KB |
Output is correct |
17 |
Correct |
12 ms |
4308 KB |
Output is correct |
18 |
Correct |
10 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
30176 KB |
Output is correct |
2 |
Correct |
65 ms |
11732 KB |
Output is correct |
3 |
Correct |
421 ms |
47428 KB |
Output is correct |
4 |
Correct |
89 ms |
18772 KB |
Output is correct |
5 |
Correct |
700 ms |
32612 KB |
Output is correct |
6 |
Correct |
1089 ms |
94036 KB |
Output is correct |
7 |
Correct |
16 ms |
31592 KB |
Output is correct |
8 |
Correct |
15 ms |
30192 KB |
Output is correct |
9 |
Correct |
3 ms |
540 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
17 ms |
31032 KB |
Output is correct |
12 |
Correct |
3 ms |
1492 KB |
Output is correct |
13 |
Correct |
66 ms |
11740 KB |
Output is correct |
14 |
Correct |
37 ms |
8412 KB |
Output is correct |
15 |
Correct |
37 ms |
9068 KB |
Output is correct |
16 |
Correct |
37 ms |
4152 KB |
Output is correct |
17 |
Correct |
164 ms |
20188 KB |
Output is correct |
18 |
Correct |
154 ms |
19916 KB |
Output is correct |
19 |
Correct |
90 ms |
18696 KB |
Output is correct |
20 |
Correct |
99 ms |
17356 KB |
Output is correct |
21 |
Correct |
245 ms |
33812 KB |
Output is correct |
22 |
Correct |
650 ms |
32608 KB |
Output is correct |
23 |
Correct |
331 ms |
27728 KB |
Output is correct |
24 |
Correct |
329 ms |
33612 KB |
Output is correct |
25 |
Correct |
609 ms |
47272 KB |
Output is correct |
26 |
Correct |
525 ms |
39864 KB |
Output is correct |
27 |
Correct |
728 ms |
53992 KB |
Output is correct |
28 |
Correct |
1074 ms |
93936 KB |
Output is correct |
29 |
Correct |
1022 ms |
86752 KB |
Output is correct |
30 |
Correct |
934 ms |
84928 KB |
Output is correct |
31 |
Correct |
892 ms |
37928 KB |
Output is correct |
32 |
Correct |
678 ms |
53396 KB |
Output is correct |