#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void IO(string name = "") {
cin.tie(0)->sync_with_stdio(0);
if (name.size()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
const int inf = 1e9+7;
vector<string> grid;
int H, W;
bool is_valid(int row, int col) {
if(row>=0 && row<H && col>=0 && col<W && grid[row][col]!='.') return true;
return false;
}
int main() {
// IO("pcb");
cin >> H >> W;
grid.resize(H);
for(int i = 0; i < H; i++) cin >> grid[i];
vector<vector<int>> dist(H, vector<int>(W, inf));
dist[0][0] = 0;
deque<tuple<int,int,int>> dq; // d, row, col
dq.push_back({0, 0, 0});
while(dq.size()) {
auto [d, row, col] = dq.front(); dq.pop_front();
if(d>dist[row][col]) continue;
pair<int,int> dirs[] = {{0,1},{0,-1},{1,0},{-1,0}};
for(auto[dirrow, dircol] : dirs) {
int nrow = row + dirrow;
int ncol = col + dircol;
if(is_valid(nrow, ncol)) {
if(grid[row][col]==grid[nrow][ncol]) { // 0
if(dist[row][col]+0<dist[nrow][ncol]) {
dist[nrow][ncol] = dist[row][col]+0;
dq.push_front({dist[nrow][ncol], nrow, ncol});
}
} else { // 1
if(dist[row][col]+1<dist[nrow][ncol]) {
dist[nrow][ncol] = dist[row][col]+1;
dq.push_back({dist[nrow][ncol], nrow, ncol});
}
}
}
}
}
int ans = 0;
for(int i = 0; i < H; i++) {
for(int j = 0; j < W; j++) {
if(dist[i][j]<inf) {
ans = max(ans, dist[i][j]);
}
}
}
cout << ans+1;
}
Compilation message
tracks.cpp: In function 'void IO(std::string)':
tracks.cpp:9:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:10:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
2004 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
10 ms |
1620 KB |
Output is correct |
5 |
Correct |
4 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
4 ms |
724 KB |
Output is correct |
11 |
Correct |
3 ms |
616 KB |
Output is correct |
12 |
Correct |
10 ms |
916 KB |
Output is correct |
13 |
Correct |
4 ms |
852 KB |
Output is correct |
14 |
Correct |
4 ms |
852 KB |
Output is correct |
15 |
Correct |
16 ms |
2072 KB |
Output is correct |
16 |
Correct |
20 ms |
2068 KB |
Output is correct |
17 |
Correct |
13 ms |
1876 KB |
Output is correct |
18 |
Correct |
10 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Correct |
71 ms |
10312 KB |
Output is correct |
3 |
Correct |
510 ms |
108920 KB |
Output is correct |
4 |
Correct |
129 ms |
25900 KB |
Output is correct |
5 |
Correct |
307 ms |
55840 KB |
Output is correct |
6 |
Correct |
993 ms |
128440 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
3 ms |
692 KB |
Output is correct |
10 |
Correct |
2 ms |
488 KB |
Output is correct |
11 |
Correct |
3 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
72 ms |
10280 KB |
Output is correct |
14 |
Correct |
41 ms |
6476 KB |
Output is correct |
15 |
Correct |
35 ms |
7116 KB |
Output is correct |
16 |
Correct |
36 ms |
4152 KB |
Output is correct |
17 |
Correct |
184 ms |
27960 KB |
Output is correct |
18 |
Correct |
138 ms |
27592 KB |
Output is correct |
19 |
Correct |
130 ms |
25776 KB |
Output is correct |
20 |
Correct |
114 ms |
23756 KB |
Output is correct |
21 |
Correct |
303 ms |
57708 KB |
Output is correct |
22 |
Correct |
305 ms |
55776 KB |
Output is correct |
23 |
Correct |
358 ms |
47804 KB |
Output is correct |
24 |
Correct |
282 ms |
56684 KB |
Output is correct |
25 |
Correct |
668 ms |
108952 KB |
Output is correct |
26 |
Correct |
847 ms |
147836 KB |
Output is correct |
27 |
Correct |
888 ms |
136024 KB |
Output is correct |
28 |
Correct |
1022 ms |
128412 KB |
Output is correct |
29 |
Correct |
994 ms |
125140 KB |
Output is correct |
30 |
Correct |
963 ms |
132384 KB |
Output is correct |
31 |
Correct |
768 ms |
63424 KB |
Output is correct |
32 |
Correct |
881 ms |
134248 KB |
Output is correct |