#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int main() {
int h, w;
cin >> h >> w;
vector<vector<char>> grid(h, vector<char>(w));
int cnt = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> grid[i][j];
if (grid[i][j] != '.') cnt++;
}
}
deque<pair<int, int>> dq;
vector<vector<int>> dist(h, vector<int>(w, 0x3f3f3f3f));
// vector<vector<int>> vis(h, vector<int>(w));
dist[0][0] = 0;
dq.push_front({0, 0});
while (!dq.empty()) {
auto tp = dq.front();
dq.pop_front();
// cout << 61 - cnt << "\t" << tp.first << " " << tp.second << endl;
cnt--;
if (cnt == 0) {
break;
}
for (int i = 0; i < 4; i++) {
int nx = tp.first + dx[i];
int ny = tp.second + dy[i];
if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
if (grid[nx][ny] == '.') continue;
int cost = 1;
if (grid[nx][ny] == grid[tp.first][tp.second]) cost = 0;
// cout << vis[nx][ny] << " " << nx << " " << ny << endl;
if (dist[tp.first][tp.second] + cost < dist[nx][ny]) {
// vis[nx][ny] = true;
dist[nx][ny] = dist[tp.first][tp.second] + cost;
if (cost == 1) {
dq.push_back({nx, ny});
} else {
dq.push_front({nx, ny});
}
}
}
}
int ans = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (grid[i][j] != '.') ans = max(ans, dist[i][j]);
}
cout << endl;
}
cout << ans + 1 << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
1884 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
10 ms |
1372 KB |
Output is correct |
5 |
Correct |
4 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
6 ms |
860 KB |
Output is correct |
13 |
Correct |
4 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
860 KB |
Output is correct |
15 |
Correct |
15 ms |
2016 KB |
Output is correct |
16 |
Correct |
15 ms |
1884 KB |
Output is correct |
17 |
Correct |
14 ms |
1688 KB |
Output is correct |
18 |
Correct |
10 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
860 KB |
Output is correct |
2 |
Correct |
71 ms |
9664 KB |
Output is correct |
3 |
Correct |
581 ms |
94416 KB |
Output is correct |
4 |
Correct |
146 ms |
22440 KB |
Output is correct |
5 |
Correct |
369 ms |
53588 KB |
Output is correct |
6 |
Correct |
984 ms |
107480 KB |
Output is correct |
7 |
Correct |
9 ms |
860 KB |
Output is correct |
8 |
Correct |
6 ms |
860 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
8 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
70 ms |
9472 KB |
Output is correct |
14 |
Correct |
41 ms |
5716 KB |
Output is correct |
15 |
Correct |
39 ms |
6280 KB |
Output is correct |
16 |
Correct |
31 ms |
4184 KB |
Output is correct |
17 |
Correct |
192 ms |
24292 KB |
Output is correct |
18 |
Correct |
150 ms |
23888 KB |
Output is correct |
19 |
Correct |
170 ms |
22352 KB |
Output is correct |
20 |
Correct |
131 ms |
20724 KB |
Output is correct |
21 |
Correct |
348 ms |
55208 KB |
Output is correct |
22 |
Correct |
358 ms |
53464 KB |
Output is correct |
23 |
Correct |
371 ms |
45908 KB |
Output is correct |
24 |
Correct |
339 ms |
53936 KB |
Output is correct |
25 |
Correct |
656 ms |
94800 KB |
Output is correct |
26 |
Correct |
752 ms |
122716 KB |
Output is correct |
27 |
Correct |
987 ms |
114560 KB |
Output is correct |
28 |
Correct |
994 ms |
107368 KB |
Output is correct |
29 |
Correct |
996 ms |
106188 KB |
Output is correct |
30 |
Correct |
969 ms |
105828 KB |
Output is correct |
31 |
Correct |
680 ms |
61016 KB |
Output is correct |
32 |
Correct |
956 ms |
107360 KB |
Output is correct |