#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
// void setIO(string filename) {
// freopen((filename + ".in").c_str(), "r", stdin);
// freopen((filename + ".out").c_str(), "w", stdout);
// }
int n, m;
vector<vector<char>> snow;
vector<vector<int>> dis;
int ans;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool check(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m && snow[x][y] != '.');
}
void bfs(pair<int, int> st) {
dis.assign(n, vector<int>(m, 1e9));
deque<pair<int, int>> dq;
dis[st.first][st.second] = 0;
dq.push_back({st.first, st.second});
while (!dq.empty()) {
auto node = dq.front(); dq.pop_front();
int i = node.first, j = node.second;
ans = max(ans, dis[i][j]);
for (int k = 0; k < 4; k++) {
int x = i + dx[k], y = j + dy[k];
if (check(x, y) && dis[x][y] == 1e9) {
if (snow[x][y] == snow[i][j]) {
// 0 edge
dis[x][y] = dis[i][j];
dq.push_front({x, y});
} else {
// 1 edge
dis[x][y] = dis[i][j] + 1;
dq.push_back({x, y});
}
}
}
}
}
void solution() {
cin >> n >> m;
snow.resize(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> snow[i][j];
}
}
bfs({0, 0});
cout << ans + 1 << endl;
}
int main() {
// setIO("");
fast;
solution();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
1620 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
5 ms |
1416 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
4 ms |
852 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
9 ms |
1620 KB |
Output is correct |
16 |
Correct |
10 ms |
1608 KB |
Output is correct |
17 |
Correct |
7 ms |
1620 KB |
Output is correct |
18 |
Correct |
5 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Correct |
38 ms |
8180 KB |
Output is correct |
3 |
Correct |
261 ms |
79036 KB |
Output is correct |
4 |
Correct |
73 ms |
18896 KB |
Output is correct |
5 |
Correct |
168 ms |
44620 KB |
Output is correct |
6 |
Correct |
597 ms |
93500 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
520 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
37 ms |
8160 KB |
Output is correct |
14 |
Correct |
22 ms |
4820 KB |
Output is correct |
15 |
Correct |
17 ms |
5412 KB |
Output is correct |
16 |
Correct |
18 ms |
3524 KB |
Output is correct |
17 |
Correct |
104 ms |
20456 KB |
Output is correct |
18 |
Correct |
79 ms |
20040 KB |
Output is correct |
19 |
Correct |
68 ms |
18884 KB |
Output is correct |
20 |
Correct |
60 ms |
17356 KB |
Output is correct |
21 |
Correct |
155 ms |
46152 KB |
Output is correct |
22 |
Correct |
170 ms |
44684 KB |
Output is correct |
23 |
Correct |
206 ms |
38480 KB |
Output is correct |
24 |
Correct |
152 ms |
45124 KB |
Output is correct |
25 |
Correct |
443 ms |
78964 KB |
Output is correct |
26 |
Correct |
574 ms |
110808 KB |
Output is correct |
27 |
Correct |
538 ms |
104620 KB |
Output is correct |
28 |
Correct |
600 ms |
93480 KB |
Output is correct |
29 |
Correct |
593 ms |
90688 KB |
Output is correct |
30 |
Correct |
565 ms |
96052 KB |
Output is correct |
31 |
Correct |
417 ms |
51232 KB |
Output is correct |
32 |
Correct |
577 ms |
103080 KB |
Output is correct |