#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
const int N = 4e3 + 5;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
queue<pair<int, int>> q1, q2;
char a[N][N];
bool vis[N][N];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int h, w, cnt = 0, ans = 0;
cin >> h >> w;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> a[i][j];
if (a[i][j] != '.') cnt++;
}
}
q1.push({1, 1});
vis[1][1] = 1;
while (cnt > 0) {
ans++;
while (!q1.empty()) {
int x = q1.front().fi, y = q1.front().se;
q1.pop();
cnt--;
for (int i = 0; i < 4; i++) {
int tx = x + dx[i], ty = y + dy[i];
if (tx < 1 or ty < 1 or tx > h or ty > w or vis[tx][ty] or a[tx][ty] == '.') continue;
else if (a[x][y] == a[tx][ty]) vis[tx][ty] = 1, q1.push({tx, ty});
else vis[tx][ty] = 1, q2.push({tx, ty});
}
}
q1 = q2;
while (!q2.empty()) q2.pop();
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
4436 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
7 ms |
4564 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
4 ms |
2348 KB |
Output is correct |
11 |
Correct |
2 ms |
2004 KB |
Output is correct |
12 |
Correct |
5 ms |
2644 KB |
Output is correct |
13 |
Correct |
3 ms |
2644 KB |
Output is correct |
14 |
Correct |
5 ms |
2644 KB |
Output is correct |
15 |
Correct |
14 ms |
4552 KB |
Output is correct |
16 |
Correct |
11 ms |
4436 KB |
Output is correct |
17 |
Correct |
9 ms |
4308 KB |
Output is correct |
18 |
Correct |
7 ms |
4564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
30292 KB |
Output is correct |
2 |
Correct |
59 ms |
11672 KB |
Output is correct |
3 |
Correct |
276 ms |
39680 KB |
Output is correct |
4 |
Correct |
85 ms |
18692 KB |
Output is correct |
5 |
Correct |
226 ms |
31880 KB |
Output is correct |
6 |
Correct |
798 ms |
59936 KB |
Output is correct |
7 |
Correct |
16 ms |
31700 KB |
Output is correct |
8 |
Correct |
15 ms |
30232 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
14 ms |
31132 KB |
Output is correct |
12 |
Correct |
2 ms |
1492 KB |
Output is correct |
13 |
Correct |
45 ms |
11676 KB |
Output is correct |
14 |
Correct |
26 ms |
8368 KB |
Output is correct |
15 |
Correct |
25 ms |
9132 KB |
Output is correct |
16 |
Correct |
21 ms |
4036 KB |
Output is correct |
17 |
Correct |
129 ms |
20128 KB |
Output is correct |
18 |
Correct |
138 ms |
19908 KB |
Output is correct |
19 |
Correct |
73 ms |
18768 KB |
Output is correct |
20 |
Correct |
76 ms |
17352 KB |
Output is correct |
21 |
Correct |
163 ms |
32696 KB |
Output is correct |
22 |
Correct |
235 ms |
31804 KB |
Output is correct |
23 |
Correct |
251 ms |
27544 KB |
Output is correct |
24 |
Correct |
190 ms |
32684 KB |
Output is correct |
25 |
Correct |
468 ms |
39732 KB |
Output is correct |
26 |
Correct |
478 ms |
35792 KB |
Output is correct |
27 |
Correct |
539 ms |
42980 KB |
Output is correct |
28 |
Correct |
971 ms |
59876 KB |
Output is correct |
29 |
Correct |
953 ms |
56688 KB |
Output is correct |
30 |
Correct |
646 ms |
54232 KB |
Output is correct |
31 |
Correct |
544 ms |
34320 KB |
Output is correct |
32 |
Correct |
578 ms |
41164 KB |
Output is correct |