#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 4000 + 7;
string s[N];
bool vis[N][N];
int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};
int n, m, ret = 0;
void bfs(queue<pair<int, int>> &q1, queue<pair<int, int>> &q2, int d) {
if (q1.empty()) return;
ret = d;
while (!q1.empty()) {
pair<int, int> x = q1.front();
q1.pop();
for (int dir = 0; dir < 4; dir++) {
if (x.first + dx[dir] < 0 || x.first + dx[dir] >= n || x.second + dy[dir] < 0 || x.second + dy[dir] >= m) {
continue;
}
if (vis[x.first + dx[dir]][x.second + dy[dir]] == true || s[x.first + dx[dir]][x.second + dy[dir]] == '.') {
continue;
}
vis[x.first + dx[dir]][x.second + dy[dir]] = 1;
if (s[x.first + dx[dir]][x.second + dy[dir]] != s[x.first][x.second]) {
q2.push({x.first + dx[dir], x.second + dy[dir]});
} else {
q1.push({x.first + dx[dir], x.second + dy[dir]});
}
}
}
bfs(q2, q1, d + 1);
}
int main() {
#ifdef ONPC
freopen("input.txt", "r", stdin);
#endif // ONPC
#ifndef ONPC
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif // ONPC
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
queue<pair<int, int>> q1, q2;
q1.push({0, 0});
vis[0][0] = 1;
bfs(q1, q2, 1);
cout << ret << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2908 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
5 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
1712 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
844 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
1624 KB |
Output is correct |
11 |
Correct |
2 ms |
1368 KB |
Output is correct |
12 |
Correct |
4 ms |
1884 KB |
Output is correct |
13 |
Correct |
2 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
10 ms |
2908 KB |
Output is correct |
16 |
Correct |
9 ms |
2852 KB |
Output is correct |
17 |
Correct |
7 ms |
2732 KB |
Output is correct |
18 |
Correct |
5 ms |
2904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15444 KB |
Output is correct |
2 |
Correct |
32 ms |
8664 KB |
Output is correct |
3 |
Correct |
150 ms |
49492 KB |
Output is correct |
4 |
Correct |
38 ms |
15560 KB |
Output is correct |
5 |
Correct |
109 ms |
30844 KB |
Output is correct |
6 |
Correct |
604 ms |
59596 KB |
Output is correct |
7 |
Correct |
7 ms |
16216 KB |
Output is correct |
8 |
Correct |
9 ms |
15708 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
7 ms |
15932 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
31 ms |
8480 KB |
Output is correct |
14 |
Correct |
20 ms |
5988 KB |
Output is correct |
15 |
Correct |
11 ms |
6492 KB |
Output is correct |
16 |
Correct |
16 ms |
3420 KB |
Output is correct |
17 |
Correct |
80 ms |
16728 KB |
Output is correct |
18 |
Correct |
43 ms |
16544 KB |
Output is correct |
19 |
Correct |
37 ms |
15440 KB |
Output is correct |
20 |
Correct |
36 ms |
14416 KB |
Output is correct |
21 |
Correct |
90 ms |
31824 KB |
Output is correct |
22 |
Correct |
113 ms |
30788 KB |
Output is correct |
23 |
Correct |
168 ms |
26344 KB |
Output is correct |
24 |
Correct |
89 ms |
31312 KB |
Output is correct |
25 |
Correct |
239 ms |
49588 KB |
Output is correct |
26 |
Correct |
305 ms |
39528 KB |
Output is correct |
27 |
Correct |
427 ms |
51348 KB |
Output is correct |
28 |
Correct |
650 ms |
59668 KB |
Output is correct |
29 |
Correct |
577 ms |
58064 KB |
Output is correct |
30 |
Correct |
491 ms |
56372 KB |
Output is correct |
31 |
Correct |
441 ms |
34712 KB |
Output is correct |
32 |
Correct |
339 ms |
50076 KB |
Output is correct |