#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
#include <iomanip>
using namespace std;
vector<vector<bool>> hasVisited;
vector<string> grid;
vector<pair<int, int>> pos = {{0, 1},
{-1, 0},
{0, -1},
{1, 0}};
int main() {
//freopen("milkorder.in", "r", stdin);
//freopen("milkorder.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
priority_queue<pair<int, pair<int, int>>> q;
grid.resize(n), hasVisited.resize(n);
for (int i = 0; i < n; i++) {
cin >> grid[i];
hasVisited[i].assign(m, false);
}
q.push({0, {0, 0}});
int myMax = 0;
while (!q.empty()) {
pair<int, int> loc = q.top().second;
int dist = -q.top().first;
q.pop();
if (hasVisited[loc.first][loc.second]) {
continue;
}
myMax = max(myMax, dist);
hasVisited[loc.first][loc.second] = true;
for (auto p: pos) {
int nx = loc.first + p.first;
int ny = loc.second + p.second;
if (nx >= 0 && ny >= 0 && nx < n && ny < m && grid[nx][ny] != '.') {
if (hasVisited[nx][ny]) {
myMax = max(myMax, dist + (grid[nx][ny] != grid[loc.first][loc.second]));
continue;
}
q.push(make_pair(-dist - (grid[nx][ny] != grid[loc.first][loc.second]),
make_pair(nx, ny)));
}
}
}
cout << myMax;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
912 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
49 ms |
1308 KB |
Output is correct |
5 |
Correct |
4 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
6 ms |
332 KB |
Output is correct |
11 |
Correct |
12 ms |
528 KB |
Output is correct |
12 |
Correct |
27 ms |
608 KB |
Output is correct |
13 |
Correct |
4 ms |
332 KB |
Output is correct |
14 |
Correct |
4 ms |
332 KB |
Output is correct |
15 |
Correct |
56 ms |
808 KB |
Output is correct |
16 |
Correct |
85 ms |
916 KB |
Output is correct |
17 |
Correct |
27 ms |
588 KB |
Output is correct |
18 |
Correct |
50 ms |
1416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
159 ms |
2252 KB |
Output is correct |
3 |
Correct |
439 ms |
20460 KB |
Output is correct |
4 |
Correct |
103 ms |
4940 KB |
Output is correct |
5 |
Correct |
132 ms |
11432 KB |
Output is correct |
6 |
Execution timed out |
2075 ms |
45412 KB |
Time limit exceeded |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
7 ms |
548 KB |
Output is correct |
10 |
Correct |
1 ms |
280 KB |
Output is correct |
11 |
Correct |
1 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
160 ms |
2252 KB |
Output is correct |
14 |
Correct |
90 ms |
1484 KB |
Output is correct |
15 |
Correct |
11 ms |
1484 KB |
Output is correct |
16 |
Correct |
92 ms |
1196 KB |
Output is correct |
17 |
Correct |
428 ms |
5444 KB |
Output is correct |
18 |
Correct |
45 ms |
5196 KB |
Output is correct |
19 |
Correct |
103 ms |
4940 KB |
Output is correct |
20 |
Correct |
113 ms |
4428 KB |
Output is correct |
21 |
Correct |
263 ms |
11724 KB |
Output is correct |
22 |
Correct |
133 ms |
11340 KB |
Output is correct |
23 |
Correct |
828 ms |
10100 KB |
Output is correct |
24 |
Correct |
93 ms |
11468 KB |
Output is correct |
25 |
Correct |
250 ms |
20444 KB |
Output is correct |
26 |
Execution timed out |
2094 ms |
15692 KB |
Time limit exceeded |
27 |
Execution timed out |
2084 ms |
23660 KB |
Time limit exceeded |
28 |
Execution timed out |
2090 ms |
45176 KB |
Time limit exceeded |
29 |
Execution timed out |
2087 ms |
45276 KB |
Time limit exceeded |
30 |
Execution timed out |
2074 ms |
44712 KB |
Time limit exceeded |
31 |
Execution timed out |
2081 ms |
14596 KB |
Time limit exceeded |
32 |
Execution timed out |
2081 ms |
22140 KB |
Time limit exceeded |