#include <bits/stdc++.h>
using namespace std;
using ll = long long;
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<string> mat(n);
for (string& s : mat) {
cin >> s;
}
auto id = [m](int i, int j) { return i* m + j;};
vector<vector<pair<int,int>>> adj(n * m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (auto [di, dj] : {pair{0, -1}, {0, 1}, {-1, 0}, {1, 0}}) {
int ni = i + di;
int nj = j + dj;
if (clamp(ni, 0,n-1) == ni && clamp(nj, 0, m-1) == nj) {
if (mat[ni][nj] == '.' || mat[i][j] == '.') continue;
int c = mat[i][j] != mat[ni][nj];
adj[id(i, j)].push_back({id(ni, nj), c});
}
}
}
}
static constexpr int inf = 1e9;
vector<int> dist(n*m, inf), vis(n*m, 0);
queue<int> cur, next;
dist[0] = 0;
cur.push(0);
int res = 0;
while (cur.size()) {
int v = cur.front();
vis[v] = 1;
res = max(res, dist[v]);
for (auto [e, ec]: adj[v]) {
if (vis[e]) continue;
vis[e] = 1;
dist[e] = min(dist[e], dist[v] + ec);
if (ec) next.push(e);
else cur.push(e);
}
cur.pop();
if (cur.empty()) {
cur.swap(next);
next = {};
}
}
cout << res+1 << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
19276 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
33 ms |
12688 KB |
Output is correct |
5 |
Correct |
6 ms |
3796 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
7 ms |
3596 KB |
Output is correct |
11 |
Correct |
10 ms |
3540 KB |
Output is correct |
12 |
Correct |
19 ms |
7104 KB |
Output is correct |
13 |
Correct |
9 ms |
3796 KB |
Output is correct |
14 |
Correct |
6 ms |
3788 KB |
Output is correct |
15 |
Correct |
47 ms |
16876 KB |
Output is correct |
16 |
Correct |
51 ms |
19276 KB |
Output is correct |
17 |
Correct |
32 ms |
12540 KB |
Output is correct |
18 |
Correct |
35 ms |
12624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2504 KB |
Output is correct |
2 |
Correct |
184 ms |
79496 KB |
Output is correct |
3 |
Correct |
890 ms |
672728 KB |
Output is correct |
4 |
Correct |
271 ms |
142384 KB |
Output is correct |
5 |
Correct |
914 ms |
433196 KB |
Output is correct |
6 |
Runtime error |
1470 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Correct |
4 ms |
2260 KB |
Output is correct |
8 |
Correct |
4 ms |
2504 KB |
Output is correct |
9 |
Correct |
5 ms |
3508 KB |
Output is correct |
10 |
Correct |
2 ms |
1620 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
3 ms |
1364 KB |
Output is correct |
13 |
Correct |
181 ms |
79456 KB |
Output is correct |
14 |
Correct |
104 ms |
46320 KB |
Output is correct |
15 |
Correct |
97 ms |
41964 KB |
Output is correct |
16 |
Correct |
83 ms |
37116 KB |
Output is correct |
17 |
Correct |
459 ms |
203208 KB |
Output is correct |
18 |
Correct |
373 ms |
164772 KB |
Output is correct |
19 |
Correct |
273 ms |
142424 KB |
Output is correct |
20 |
Correct |
204 ms |
148028 KB |
Output is correct |
21 |
Correct |
540 ms |
393748 KB |
Output is correct |
22 |
Correct |
897 ms |
433408 KB |
Output is correct |
23 |
Correct |
910 ms |
392844 KB |
Output is correct |
24 |
Correct |
606 ms |
389212 KB |
Output is correct |
25 |
Correct |
1983 ms |
662220 KB |
Output is correct |
26 |
Execution timed out |
2097 ms |
973180 KB |
Time limit exceeded |
27 |
Runtime error |
1444 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Runtime error |
1441 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Runtime error |
1441 ms |
1048576 KB |
Execution killed with signal 9 |
30 |
Runtime error |
1462 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Execution timed out |
2092 ms |
811824 KB |
Time limit exceeded |
32 |
Runtime error |
1467 ms |
1048576 KB |
Execution killed with signal 9 |