#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<bool>> visited(H, vector<bool>(W));
vector<vector<int>> dist(H, vector<int>(W, inf));
dist[0][0] = 0;
deque<pair<int,int>> dq;
dq.push_back({0, 0});
while(dq.size()) {
auto [row, col] = dq.front(); dq.pop_front();
if(visited[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) && !visited[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({nrow, ncol});
}
} else { // 1
if(dist[row][col]+0<dist[nrow][ncol]) {
dist[nrow][ncol] = dist[row][col]+1;
dq.push_back({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 |
27 ms |
2252 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
15 ms |
1588 KB |
Output is correct |
5 |
Correct |
4 ms |
820 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
784 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
9 ms |
980 KB |
Output is correct |
13 |
Correct |
4 ms |
796 KB |
Output is correct |
14 |
Correct |
5 ms |
820 KB |
Output is correct |
15 |
Correct |
26 ms |
2132 KB |
Output is correct |
16 |
Correct |
25 ms |
2144 KB |
Output is correct |
17 |
Correct |
17 ms |
2016 KB |
Output is correct |
18 |
Correct |
12 ms |
1672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
980 KB |
Output is correct |
2 |
Correct |
82 ms |
10572 KB |
Output is correct |
3 |
Correct |
556 ms |
111160 KB |
Output is correct |
4 |
Correct |
142 ms |
26460 KB |
Output is correct |
5 |
Correct |
309 ms |
57104 KB |
Output is correct |
6 |
Correct |
1210 ms |
132392 KB |
Output is correct |
7 |
Correct |
3 ms |
1108 KB |
Output is correct |
8 |
Correct |
3 ms |
1076 KB |
Output is correct |
9 |
Correct |
4 ms |
688 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
1080 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
82 ms |
10672 KB |
Output is correct |
14 |
Correct |
52 ms |
6604 KB |
Output is correct |
15 |
Correct |
36 ms |
7356 KB |
Output is correct |
16 |
Correct |
42 ms |
4336 KB |
Output is correct |
17 |
Correct |
235 ms |
28616 KB |
Output is correct |
18 |
Correct |
147 ms |
28108 KB |
Output is correct |
19 |
Correct |
135 ms |
26528 KB |
Output is correct |
20 |
Correct |
134 ms |
24268 KB |
Output is correct |
21 |
Correct |
316 ms |
59040 KB |
Output is correct |
22 |
Correct |
317 ms |
57144 KB |
Output is correct |
23 |
Correct |
428 ms |
48972 KB |
Output is correct |
24 |
Correct |
303 ms |
58056 KB |
Output is correct |
25 |
Correct |
680 ms |
111156 KB |
Output is correct |
26 |
Correct |
911 ms |
125472 KB |
Output is correct |
27 |
Correct |
952 ms |
129504 KB |
Output is correct |
28 |
Correct |
1132 ms |
132508 KB |
Output is correct |
29 |
Correct |
1177 ms |
128240 KB |
Output is correct |
30 |
Correct |
1099 ms |
131556 KB |
Output is correct |
31 |
Correct |
1102 ms |
64976 KB |
Output is correct |
32 |
Correct |
1059 ms |
129268 KB |
Output is correct |