#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma warning(disable : 6031)
#define USACO freopen("exercise.in", "r", stdin); freopen("exercise.out", "w+", stdout)
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
using namespace std;
ll binpow(ll a, ll x, ll m) {
ll ans = 1, md = a % m;
while (x != 0) {
if (x % 2 == 1) ans = (ans * md) % m;
md = (md * md) % m;
x >>= 1;
}
return ans;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) {
return a * b / gcd(a, b);
}
bool ok(int r, int c, int n, int m) {
return r >= 0 && r < n && c >= 0 && c < m;
}
/*
*/
char a[4000][4000];
int d[4000][4000];
void solve() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cin >> a[i][j];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) d[i][j] = INT32_MAX;
}
d[0][0] = 1;
deque<pair<int, int>> s;
s.push_front({ 0, 0 });
while (!s.empty()) {
int r = s.front().first, c = s.front().second;
s.pop_front();
if (ok(r - 1, c, n, m) && a[r - 1][c] != '.' && d[r][c] + (a[r][c] != a[r - 1][c]) < d[r - 1][c]) {
d[r - 1][c] = d[r][c] + (a[r][c] != a[r - 1][c]);
if (a[r][c] != a[r - 1][c]) s.push_back({ r - 1, c });
else s.push_front({ r - 1, c });
}
if (ok(r + 1, c, n, m) && a[r + 1][c] != '.' && d[r][c] + (a[r][c] != a[r + 1][c]) < d[r + 1][c]) {
d[r + 1][c] = d[r][c] + (a[r][c] != a[r + 1][c]);
if (a[r][c] != a[r + 1][c]) s.push_back({ r + 1, c });
else s.push_front({ r + 1, c });
}
if (ok(r, c - 1, n, m) && a[r][c - 1] != '.' && d[r][c] + (a[r][c] != a[r][c - 1]) < d[r][c - 1]) {
d[r][c - 1] = d[r][c] + (a[r][c] != a[r][c - 1]);
if (a[r][c] != a[r][c - 1]) s.push_back({ r, c - 1 });
else s.push_front({ r, c - 1 });
}
if (ok(r, c + 1, n, m) && a[r][c + 1] != '.' && d[r][c] + (a[r][c] != a[r][c + 1]) < d[r][c + 1]) {
d[r][c + 1] = d[r][c] + (a[r][c] != a[r][c + 1]);
if (a[r][c] != a[r][c + 1]) s.push_back({ r, c + 1 });
else s.push_front({ r, c + 1 });
}
}
int mx = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] != '.') mx = max(mx, d[i][j]);
}
}
cout << mx << "\n";
}
int main() {
//USACO;
fastIO;
solve();
}
Compilation message
tracks.cpp:2: warning: ignoring '#pragma warning ' [-Wunknown-pragmas]
2 | #pragma warning(disable : 4996)
|
tracks.cpp:3: warning: ignoring '#pragma warning ' [-Wunknown-pragmas]
3 | #pragma warning(disable : 6031)
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
5204 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
8 ms |
5156 KB |
Output is correct |
5 |
Correct |
4 ms |
3024 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 |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
3 ms |
2520 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
6 ms |
3024 KB |
Output is correct |
13 |
Correct |
5 ms |
3032 KB |
Output is correct |
14 |
Correct |
4 ms |
3024 KB |
Output is correct |
15 |
Correct |
13 ms |
5384 KB |
Output is correct |
16 |
Correct |
14 ms |
5336 KB |
Output is correct |
17 |
Correct |
11 ms |
5228 KB |
Output is correct |
18 |
Correct |
10 ms |
5128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
30676 KB |
Output is correct |
2 |
Correct |
58 ms |
16612 KB |
Output is correct |
3 |
Correct |
337 ms |
79168 KB |
Output is correct |
4 |
Correct |
95 ms |
30216 KB |
Output is correct |
5 |
Correct |
241 ms |
59500 KB |
Output is correct |
6 |
Correct |
695 ms |
92572 KB |
Output is correct |
7 |
Correct |
15 ms |
32108 KB |
Output is correct |
8 |
Correct |
15 ms |
30672 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
13 ms |
31460 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
51 ms |
16632 KB |
Output is correct |
14 |
Correct |
29 ms |
11340 KB |
Output is correct |
15 |
Correct |
29 ms |
12368 KB |
Output is correct |
16 |
Correct |
23 ms |
6236 KB |
Output is correct |
17 |
Correct |
135 ms |
32520 KB |
Output is correct |
18 |
Correct |
116 ms |
31964 KB |
Output is correct |
19 |
Correct |
100 ms |
30252 KB |
Output is correct |
20 |
Correct |
90 ms |
28108 KB |
Output is correct |
21 |
Correct |
215 ms |
61628 KB |
Output is correct |
22 |
Correct |
227 ms |
59400 KB |
Output is correct |
23 |
Correct |
246 ms |
49796 KB |
Output is correct |
24 |
Correct |
204 ms |
61028 KB |
Output is correct |
25 |
Correct |
579 ms |
79220 KB |
Output is correct |
26 |
Correct |
503 ms |
119392 KB |
Output is correct |
27 |
Correct |
592 ms |
96816 KB |
Output is correct |
28 |
Correct |
720 ms |
92560 KB |
Output is correct |
29 |
Correct |
695 ms |
92704 KB |
Output is correct |
30 |
Correct |
662 ms |
92220 KB |
Output is correct |
31 |
Correct |
501 ms |
63972 KB |
Output is correct |
32 |
Correct |
547 ms |
94700 KB |
Output is correct |