#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;
deque<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_back({0, {0, 0}});
int myMax = 0;
while (!q.empty()) {
pair<int, int> loc = q.front().second;
int dist = -q.front().first;
q.pop_front();
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;
}
if (grid[nx][ny] != grid[loc.first][loc.second]) {
q.push_back(make_pair(-dist - 1, make_pair(nx, ny)));
} else {
q.push_front(make_pair(-dist, make_pair(nx, ny)));
}
}
}
}
cout << myMax;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
716 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
6 ms |
1100 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
5 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
11 ms |
720 KB |
Output is correct |
16 |
Correct |
14 ms |
832 KB |
Output is correct |
17 |
Correct |
9 ms |
664 KB |
Output is correct |
18 |
Correct |
7 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
716 KB |
Output is correct |
2 |
Correct |
37 ms |
2380 KB |
Output is correct |
3 |
Correct |
166 ms |
20340 KB |
Output is correct |
4 |
Correct |
37 ms |
4812 KB |
Output is correct |
5 |
Correct |
126 ms |
11340 KB |
Output is correct |
6 |
Correct |
660 ms |
57488 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
35 ms |
2252 KB |
Output is correct |
14 |
Correct |
21 ms |
1492 KB |
Output is correct |
15 |
Correct |
10 ms |
1484 KB |
Output is correct |
16 |
Correct |
20 ms |
1156 KB |
Output is correct |
17 |
Correct |
87 ms |
5324 KB |
Output is correct |
18 |
Correct |
41 ms |
5220 KB |
Output is correct |
19 |
Correct |
37 ms |
4812 KB |
Output is correct |
20 |
Correct |
40 ms |
4428 KB |
Output is correct |
21 |
Correct |
104 ms |
11724 KB |
Output is correct |
22 |
Correct |
125 ms |
11440 KB |
Output is correct |
23 |
Correct |
179 ms |
10052 KB |
Output is correct |
24 |
Correct |
90 ms |
11468 KB |
Output is correct |
25 |
Correct |
225 ms |
20428 KB |
Output is correct |
26 |
Incorrect |
573 ms |
163872 KB |
Output isn't correct |
27 |
Correct |
573 ms |
103728 KB |
Output is correct |
28 |
Correct |
663 ms |
57208 KB |
Output is correct |
29 |
Correct |
650 ms |
51532 KB |
Output is correct |
30 |
Correct |
626 ms |
72360 KB |
Output is correct |
31 |
Correct |
563 ms |
14256 KB |
Output is correct |
32 |
Correct |
555 ms |
91488 KB |
Output is correct |