#include<bits/stdc++.h>
using namespace std;
//https://oj.uz/problem/view/BOI13_tracks
typedef long long Int;
const int N = 4005;
char board[N][N];
int dist[N][N];
using Cell = pair<int,int>;
#define x first
#define y second
#define all(v) (v).begin(), (v).end()
#define cst(T) const T&
template<class A, class B> bool umin(A& var, cst(B) val) {
return (val < var) ? (var = val, true) : false;
}
template<class A, class B> bool umax(A& var, cst(B) val) {
return (var < val) ? (var = val, true) : false;
}
const int INF = 1e9;
const int dx[] {-1,+1, 0, 0};
const int dy[] { 0, 0,-1,+1};
void solve()
{
int hei, wid;
cin >> hei >> wid;
for (int i = 1; i <= hei; i++)
for (int j = 1; j <= wid; j++) {
dist[i][j] = INF;
cin >> board[i][j];
}
deque<Cell> bfs;
bfs.push_front(Cell(1,1));
dist[1][1] = 1;
int ans = 0;
while (!bfs.empty()) {
auto u = bfs.front(); bfs.pop_front();
umax(ans, dist[u.x][u.y]);
for (int d = 0; d < 4; d++) {
Cell v(u.x + dx[d], u.y + dy[d]);
if (min(v.x,v.x) < 1 or v.x > hei or v.y > wid) continue;
if (board[v.x][v.y] == '.') continue;
int edge = (board[v.x][v.y] != board[u.x][u.y]);
if (umin(dist[v.x][v.y], dist[u.x][u.y] + edge))
edge ? bfs.push_back(v) : bfs.push_front(v);
}
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define task "WF"
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
solve();
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
5460 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
8 ms |
5204 KB |
Output is correct |
5 |
Correct |
4 ms |
3028 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
3 ms |
2516 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
5 ms |
3032 KB |
Output is correct |
13 |
Correct |
3 ms |
3028 KB |
Output is correct |
14 |
Correct |
4 ms |
3024 KB |
Output is correct |
15 |
Correct |
12 ms |
5552 KB |
Output is correct |
16 |
Correct |
13 ms |
5460 KB |
Output is correct |
17 |
Correct |
11 ms |
5220 KB |
Output is correct |
18 |
Correct |
8 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
30932 KB |
Output is correct |
2 |
Correct |
51 ms |
17828 KB |
Output is correct |
3 |
Correct |
332 ms |
86012 KB |
Output is correct |
4 |
Correct |
98 ms |
33600 KB |
Output is correct |
5 |
Correct |
219 ms |
67028 KB |
Output is correct |
6 |
Correct |
668 ms |
99788 KB |
Output is correct |
7 |
Correct |
15 ms |
32280 KB |
Output is correct |
8 |
Correct |
16 ms |
30932 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
4 ms |
468 KB |
Output is correct |
11 |
Correct |
14 ms |
31700 KB |
Output is correct |
12 |
Correct |
2 ms |
1616 KB |
Output is correct |
13 |
Correct |
52 ms |
17900 KB |
Output is correct |
14 |
Correct |
31 ms |
11852 KB |
Output is correct |
15 |
Correct |
27 ms |
13132 KB |
Output is correct |
16 |
Correct |
23 ms |
6620 KB |
Output is correct |
17 |
Correct |
152 ms |
36164 KB |
Output is correct |
18 |
Correct |
106 ms |
35740 KB |
Output is correct |
19 |
Correct |
101 ms |
33484 KB |
Output is correct |
20 |
Correct |
82 ms |
30956 KB |
Output is correct |
21 |
Correct |
223 ms |
70184 KB |
Output is correct |
22 |
Correct |
213 ms |
67860 KB |
Output is correct |
23 |
Correct |
237 ms |
56900 KB |
Output is correct |
24 |
Correct |
203 ms |
69368 KB |
Output is correct |
25 |
Correct |
503 ms |
88820 KB |
Output is correct |
26 |
Correct |
431 ms |
130888 KB |
Output is correct |
27 |
Correct |
563 ms |
107404 KB |
Output is correct |
28 |
Correct |
660 ms |
102996 KB |
Output is correct |
29 |
Correct |
624 ms |
102776 KB |
Output is correct |
30 |
Correct |
601 ms |
101712 KB |
Output is correct |
31 |
Correct |
480 ms |
73544 KB |
Output is correct |
32 |
Correct |
493 ms |
101860 KB |
Output is correct |