#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
using namespace std;
const int INF = 10000000;
int n, m;
bool is_safe(int row, int col) {
return row >= 0 && row < n && col >= 0 && col < m;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
vector<vector<char>> grid(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
}
}
vector<vector<int>> dist(n, vector<int>(m, INF));
dist[0][0] = 1;
queue<pii> q;
q.push({0, 0});
int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
while (!q.empty()) {
int row = q.front().first, col = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int dest_x = row + dx[i], dest_y = col + dy[i];
if (is_safe(dest_x, dest_y) && grid[dest_x][dest_y] != '.') {
int cost = dist[row][col];
if (grid[dest_x][dest_y] != grid[row][col]) cost++;
if (dist[dest_x][dest_y] > cost) {
dist[dest_x][dest_y] = cost;
q.push({dest_x, dest_y});
}
}
}
}
int ans = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (dist[i][j] != INF) ans = max(ans, dist[i][j]);
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
1740 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
19 ms |
1264 KB |
Output is correct |
5 |
Correct |
10 ms |
836 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
5 ms |
588 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
37 ms |
876 KB |
Output is correct |
13 |
Correct |
10 ms |
836 KB |
Output is correct |
14 |
Correct |
12 ms |
836 KB |
Output is correct |
15 |
Correct |
81 ms |
1848 KB |
Output is correct |
16 |
Correct |
149 ms |
1848 KB |
Output is correct |
17 |
Correct |
97 ms |
1780 KB |
Output is correct |
18 |
Correct |
18 ms |
1268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
844 KB |
Output is correct |
2 |
Correct |
486 ms |
9672 KB |
Output is correct |
3 |
Execution timed out |
2086 ms |
94556 KB |
Time limit exceeded |
4 |
Correct |
1453 ms |
22624 KB |
Output is correct |
5 |
Correct |
225 ms |
53336 KB |
Output is correct |
6 |
Execution timed out |
2087 ms |
95036 KB |
Time limit exceeded |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
3 ms |
844 KB |
Output is correct |
9 |
Correct |
16 ms |
672 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
844 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
473 ms |
9628 KB |
Output is correct |
14 |
Correct |
234 ms |
5836 KB |
Output is correct |
15 |
Correct |
30 ms |
6188 KB |
Output is correct |
16 |
Correct |
447 ms |
4104 KB |
Output is correct |
17 |
Correct |
1746 ms |
24448 KB |
Output is correct |
18 |
Correct |
108 ms |
23880 KB |
Output is correct |
19 |
Correct |
1477 ms |
22620 KB |
Output is correct |
20 |
Execution timed out |
2084 ms |
20640 KB |
Time limit exceeded |
21 |
Execution timed out |
2064 ms |
55244 KB |
Time limit exceeded |
22 |
Correct |
228 ms |
53344 KB |
Output is correct |
23 |
Execution timed out |
2077 ms |
46528 KB |
Time limit exceeded |
24 |
Correct |
196 ms |
53872 KB |
Output is correct |
25 |
Correct |
571 ms |
94436 KB |
Output is correct |
26 |
Correct |
606 ms |
72504 KB |
Output is correct |
27 |
Correct |
887 ms |
94548 KB |
Output is correct |
28 |
Execution timed out |
2077 ms |
95104 KB |
Time limit exceeded |
29 |
Execution timed out |
2087 ms |
95112 KB |
Time limit exceeded |
30 |
Execution timed out |
2095 ms |
93068 KB |
Time limit exceeded |
31 |
Execution timed out |
2087 ms |
61468 KB |
Time limit exceeded |
32 |
Execution timed out |
2091 ms |
95208 KB |
Time limit exceeded |