#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define endl '\n'
#define pl(var) " [" << #var << ": " << (var) << "] "
#define ll long long
void solve() {
int n, m; cin >> n >> m;
vector<string> arr(n);
for (int i = 0; i < n; i++) cin >> arr[i];
deque<pair<pair<int, int>, int>> dq;
int ans = 0;
dq.emplace_back(make_pair(0, 0), 0);
vector<vector<bool>> vis(n, vector<bool>(m, 0));
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
auto is_within_bounds = [&](int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
};
while (dq.size() != 0) {
auto [node, dist] = dq.front();
dq.pop_front();
if (vis[node.first][node.second]) continue;
vis[node.first][node.second] = 1;
ans = max(ans, dist);
int x = node.first;
int y = node.second;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (is_within_bounds(nx, ny)) {
if (arr[nx][ny] == '.') continue;
if (arr[nx][ny] == arr[x][y]) {
dq.emplace_front(make_pair(nx, ny), dist);
} else {
dq.emplace_back(make_pair(nx, ny), dist + 1);
}
}
}
}
cout << ans + 1 << endl;
}
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
1236 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
10 ms |
1364 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
6 ms |
596 KB |
Output is correct |
13 |
Correct |
3 ms |
536 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
14 ms |
1028 KB |
Output is correct |
16 |
Correct |
17 ms |
1108 KB |
Output is correct |
17 |
Correct |
10 ms |
852 KB |
Output is correct |
18 |
Correct |
9 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Correct |
48 ms |
3780 KB |
Output is correct |
3 |
Correct |
226 ms |
35904 KB |
Output is correct |
4 |
Correct |
50 ms |
8568 KB |
Output is correct |
5 |
Correct |
159 ms |
20052 KB |
Output is correct |
6 |
Correct |
944 ms |
86320 KB |
Output is correct |
7 |
Correct |
2 ms |
720 KB |
Output is correct |
8 |
Correct |
2 ms |
752 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
48 ms |
3740 KB |
Output is correct |
14 |
Correct |
28 ms |
2384 KB |
Output is correct |
15 |
Correct |
13 ms |
2520 KB |
Output is correct |
16 |
Correct |
25 ms |
1748 KB |
Output is correct |
17 |
Correct |
118 ms |
9344 KB |
Output is correct |
18 |
Correct |
55 ms |
9024 KB |
Output is correct |
19 |
Correct |
47 ms |
8496 KB |
Output is correct |
20 |
Correct |
55 ms |
7872 KB |
Output is correct |
21 |
Correct |
136 ms |
20852 KB |
Output is correct |
22 |
Correct |
158 ms |
20144 KB |
Output is correct |
23 |
Correct |
236 ms |
17548 KB |
Output is correct |
24 |
Correct |
122 ms |
20428 KB |
Output is correct |
25 |
Correct |
338 ms |
35788 KB |
Output is correct |
26 |
Correct |
1678 ms |
398124 KB |
Output is correct |
27 |
Correct |
824 ms |
180232 KB |
Output is correct |
28 |
Correct |
932 ms |
86592 KB |
Output is correct |
29 |
Correct |
890 ms |
82956 KB |
Output is correct |
30 |
Correct |
873 ms |
113936 KB |
Output is correct |
31 |
Correct |
737 ms |
25388 KB |
Output is correct |
32 |
Correct |
737 ms |
163408 KB |
Output is correct |