#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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
4180 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
9 ms |
4436 KB |
Output is correct |
5 |
Correct |
3 ms |
2608 KB |
Output is correct |
6 |
Correct |
0 ms |
468 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 |
3 ms |
2260 KB |
Output is correct |
11 |
Correct |
2 ms |
1888 KB |
Output is correct |
12 |
Correct |
4 ms |
2644 KB |
Output is correct |
13 |
Correct |
5 ms |
2516 KB |
Output is correct |
14 |
Correct |
4 ms |
2516 KB |
Output is correct |
15 |
Correct |
12 ms |
4296 KB |
Output is correct |
16 |
Correct |
14 ms |
4200 KB |
Output is correct |
17 |
Correct |
10 ms |
4052 KB |
Output is correct |
18 |
Correct |
9 ms |
4464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
30176 KB |
Output is correct |
2 |
Correct |
44 ms |
10148 KB |
Output is correct |
3 |
Correct |
294 ms |
31688 KB |
Output is correct |
4 |
Correct |
72 ms |
15028 KB |
Output is correct |
5 |
Correct |
243 ms |
23848 KB |
Output is correct |
6 |
Correct |
835 ms |
51864 KB |
Output is correct |
7 |
Correct |
24 ms |
31596 KB |
Output is correct |
8 |
Correct |
15 ms |
30240 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
15 ms |
31060 KB |
Output is correct |
12 |
Correct |
1 ms |
1492 KB |
Output is correct |
13 |
Correct |
45 ms |
10148 KB |
Output is correct |
14 |
Correct |
26 ms |
7480 KB |
Output is correct |
15 |
Correct |
28 ms |
8140 KB |
Output is correct |
16 |
Correct |
23 ms |
3440 KB |
Output is correct |
17 |
Correct |
165 ms |
16116 KB |
Output is correct |
18 |
Correct |
99 ms |
16000 KB |
Output is correct |
19 |
Correct |
100 ms |
15004 KB |
Output is correct |
20 |
Correct |
67 ms |
14076 KB |
Output is correct |
21 |
Correct |
160 ms |
24628 KB |
Output is correct |
22 |
Correct |
255 ms |
23724 KB |
Output is correct |
23 |
Correct |
256 ms |
19988 KB |
Output is correct |
24 |
Correct |
177 ms |
24744 KB |
Output is correct |
25 |
Correct |
425 ms |
31676 KB |
Output is correct |
26 |
Correct |
460 ms |
27784 KB |
Output is correct |
27 |
Correct |
530 ms |
34848 KB |
Output is correct |
28 |
Correct |
770 ms |
51784 KB |
Output is correct |
29 |
Correct |
811 ms |
48624 KB |
Output is correct |
30 |
Correct |
729 ms |
46128 KB |
Output is correct |
31 |
Correct |
621 ms |
26260 KB |
Output is correct |
32 |
Correct |
604 ms |
33172 KB |
Output is correct |