#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() {
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].resize(m);
for (int j = 0; j < m; j++) {
hasVisited[i][j] = false;
}
}
q.push({0, {0, 0}});
int myMax = 0;
while (!q.empty()) {
pair<int,int> loc = q.top().second;
int dist = -q.top().first;
myMax = max(myMax, dist);
q.pop();
if (hasVisited[loc.first][loc.second]) {
continue;
}
hasVisited[loc.first][loc.second] = true;
for (auto p: pos) {
int dx = p.first;
int dy = p.second;
//for (int dy = -1; dy <= 1; dy++) {
if (abs(dx) + abs(dy) == 1 && dx + loc.first >= 0 && dy + loc.second >= 0 && dx + loc.first < n && dy + loc.second < m
&& grid[loc.first + dx][loc.second + dy] != '.') {
int new_dist = -dist - (grid[dx + loc.first][dy + loc.second] != grid[loc.first][loc.second]);
q.push(make_pair(new_dist, make_pair(loc.first + dx, loc.second + dy)));
}
//}
}
}
cout << myMax;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
159 ms |
1164 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
92 ms |
1384 KB |
Output is correct |
5 |
Correct |
8 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 |
3 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
17 ms |
452 KB |
Output is correct |
11 |
Correct |
28 ms |
604 KB |
Output is correct |
12 |
Correct |
56 ms |
716 KB |
Output is correct |
13 |
Correct |
7 ms |
332 KB |
Output is correct |
14 |
Correct |
7 ms |
332 KB |
Output is correct |
15 |
Correct |
104 ms |
900 KB |
Output is correct |
16 |
Correct |
157 ms |
1164 KB |
Output is correct |
17 |
Correct |
49 ms |
716 KB |
Output is correct |
18 |
Correct |
93 ms |
1528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
716 KB |
Output is correct |
2 |
Correct |
319 ms |
2380 KB |
Output is correct |
3 |
Correct |
958 ms |
20672 KB |
Output is correct |
4 |
Correct |
247 ms |
5068 KB |
Output is correct |
5 |
Correct |
265 ms |
11600 KB |
Output is correct |
6 |
Execution timed out |
2075 ms |
45220 KB |
Time limit exceeded |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
13 ms |
472 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
362 ms |
2408 KB |
Output is correct |
14 |
Correct |
185 ms |
1592 KB |
Output is correct |
15 |
Correct |
23 ms |
1608 KB |
Output is correct |
16 |
Correct |
174 ms |
1276 KB |
Output is correct |
17 |
Correct |
817 ms |
5536 KB |
Output is correct |
18 |
Correct |
89 ms |
5304 KB |
Output is correct |
19 |
Correct |
225 ms |
5068 KB |
Output is correct |
20 |
Correct |
228 ms |
4616 KB |
Output is correct |
21 |
Correct |
548 ms |
11896 KB |
Output is correct |
22 |
Correct |
298 ms |
11616 KB |
Output is correct |
23 |
Correct |
1704 ms |
10176 KB |
Output is correct |
24 |
Correct |
215 ms |
11572 KB |
Output is correct |
25 |
Correct |
476 ms |
20612 KB |
Output is correct |
26 |
Execution timed out |
2093 ms |
15900 KB |
Time limit exceeded |
27 |
Execution timed out |
2063 ms |
22080 KB |
Time limit exceeded |
28 |
Execution timed out |
2101 ms |
45104 KB |
Time limit exceeded |
29 |
Execution timed out |
2088 ms |
45356 KB |
Time limit exceeded |
30 |
Execution timed out |
2097 ms |
44720 KB |
Time limit exceeded |
31 |
Execution timed out |
2093 ms |
16164 KB |
Time limit exceeded |
32 |
Execution timed out |
2078 ms |
22200 KB |
Time limit exceeded |