#include <iostream>
#include <deque>
#include <vector>
#include <limits>
using namespace std;
int maxY, maxX;
//bool visited[4000][4000] = {false};
int distances[4000][4000];
vector<string> grid;
pair<int, int> add(pair<int, int> a, pair<int, int> b){
return make_pair(a.first+b.first,a.second+b.second);
}
bool valid(pair<int, int> a){
if (a.first<0||a.first>=maxX||a.second<0||a.second>=maxY){
return false;
}
return grid[a.second][a.first]!='.';
}
int getEdge(pair<int, int> a, pair<int, int> b){
return grid[a.second][a.first] == grid[b.second][b.first] ? 0 : 1;
}
string s;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> maxY >> maxX;
for (int i = 0; i < maxY; i++){
cin >> s;
grid.push_back(s);
for (int j = 0; j < maxX; j++){
distances[i][j] = numeric_limits<int>::max()/2;
}
}
//visited[0][0] = false;
deque<pair<int, int>> currents;
int best = 0;
distances[0][0] = 0;
currents.emplace_back(0, 0);
while (currents.size()>0){
pair<int, int> current = currents.front();
currents.pop_front();
best = max(distances[current.second][current.first], best);
pair<int, int> nextOne;
nextOne = add(current, make_pair(1,0));
if (valid(nextOne)){
int newDistance = distances[current.second][current.first] + getEdge(current, nextOne);
if (newDistance < distances[nextOne.second][nextOne.first]){
distances[nextOne.second][nextOne.first] = newDistance;
if (getEdge(current, nextOne)==0){
currents.push_front(nextOne);
}
else{
currents.push_back(nextOne);
}
}
}
nextOne = add(current, make_pair(-1,0));
if (valid(nextOne)){
int newDistance = distances[current.second][current.first] + getEdge(current, nextOne);
if (newDistance < distances[nextOne.second][nextOne.first]){
distances[nextOne.second][nextOne.first] = newDistance;
if (getEdge(current, nextOne)==0){
currents.push_front(nextOne);
}
else{
currents.push_back(nextOne);
}
}
}nextOne = add(current, make_pair(0,1));
if (valid(nextOne)){
int newDistance = distances[current.second][current.first] + getEdge(current, nextOne);
if (newDistance < distances[nextOne.second][nextOne.first]){
distances[nextOne.second][nextOne.first] = newDistance;
if (getEdge(current, nextOne)==0){
currents.push_front(nextOne);
}
else{
currents.push_back(nextOne);
}
}
}nextOne = add(current, make_pair(0,-1));
if (valid(nextOne)){
int newDistance = distances[current.second][current.first] + getEdge(current, nextOne);
if (newDistance < distances[nextOne.second][nextOne.first]){
distances[nextOne.second][nextOne.first] = newDistance;
if (getEdge(current, nextOne)==0){
currents.push_front(nextOne);
}
else{
currents.push_back(nextOne);
}
}
}
}
cout << best + 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9048 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2556 KB |
Output is correct |
4 |
Correct |
5 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
3 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4700 KB |
Output is correct |
12 |
Correct |
4 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
7 ms |
9028 KB |
Output is correct |
16 |
Correct |
8 ms |
8988 KB |
Output is correct |
17 |
Correct |
6 ms |
9008 KB |
Output is correct |
18 |
Correct |
5 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
62296 KB |
Output is correct |
2 |
Correct |
26 ms |
22620 KB |
Output is correct |
3 |
Correct |
134 ms |
78768 KB |
Output is correct |
4 |
Correct |
41 ms |
34908 KB |
Output is correct |
5 |
Correct |
87 ms |
56656 KB |
Output is correct |
6 |
Correct |
558 ms |
92112 KB |
Output is correct |
7 |
Correct |
10 ms |
62492 KB |
Output is correct |
8 |
Correct |
10 ms |
62104 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
10 ms |
62300 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
28 ms |
22620 KB |
Output is correct |
14 |
Correct |
16 ms |
15708 KB |
Output is correct |
15 |
Correct |
10 ms |
17756 KB |
Output is correct |
16 |
Correct |
12 ms |
7256 KB |
Output is correct |
17 |
Correct |
67 ms |
37464 KB |
Output is correct |
18 |
Correct |
46 ms |
37212 KB |
Output is correct |
19 |
Correct |
42 ms |
35108 KB |
Output is correct |
20 |
Correct |
35 ms |
32600 KB |
Output is correct |
21 |
Correct |
87 ms |
58972 KB |
Output is correct |
22 |
Correct |
90 ms |
56656 KB |
Output is correct |
23 |
Correct |
129 ms |
47292 KB |
Output is correct |
24 |
Correct |
84 ms |
58916 KB |
Output is correct |
25 |
Correct |
246 ms |
78924 KB |
Output is correct |
26 |
Correct |
401 ms |
118376 KB |
Output is correct |
27 |
Correct |
464 ms |
97016 KB |
Output is correct |
28 |
Correct |
553 ms |
92428 KB |
Output is correct |
29 |
Correct |
559 ms |
89956 KB |
Output is correct |
30 |
Correct |
542 ms |
94996 KB |
Output is correct |
31 |
Correct |
312 ms |
62780 KB |
Output is correct |
32 |
Correct |
409 ms |
95584 KB |
Output is correct |