// I_Love_LPL 1y0m2d
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define endl '\n'
const int N = 4005;
long long mod = 1000007;
const int lim = 4e5 + 5;
const int lg = 20;
const int base = 311;
const long double eps = 1e-6;
ll nx[4] = {0, 0, 1, -1};
ll ny[4] = {1, -1, 0, 0};
char a[N][N];
ll dp[N][N];
struct viet{
ll x, y, v;
};
ll n, m;
bool check(ll x, ll y){
if (x < 1 || x > n || y < 1 || y > m || a[x][y] == '.') return false;
else return true;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("tests.inp", "r")){
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
cin >> n >> m;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> a[i][j];
dp[i][j] = 1e9;
}
}
deque<viet> q;
q.push_back({1, 1, 1});
dp[1][1] = 1;
ll ans = 1;
while (q.size()){
viet x = q.front();
ans = max(ans, x.v);
q.pop_front();
for (int i = 0; i < 4; i++){
ll nwx = x.x + nx[i];
ll nwy = x.y + ny[i];
if (!check(nwx, nwy)) continue;
ll cost = x.v + (a[nwx][nwy] != a[x.x][x.y]);
if (dp[nwx][nwy] > cost){
dp[nwx][nwy] = cost;
if (cost == x.v) q.push_front({nwx, nwy, cost});
else q.push_back({nwx, nwy, cost});
}
}
}
cout << ans;
}
/*
Ans:
Out:
*/
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
5196 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
10 ms |
5048 KB |
Output is correct |
5 |
Correct |
4 ms |
2892 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
4 ms |
2508 KB |
Output is correct |
11 |
Correct |
3 ms |
2124 KB |
Output is correct |
12 |
Correct |
7 ms |
3020 KB |
Output is correct |
13 |
Correct |
4 ms |
2892 KB |
Output is correct |
14 |
Correct |
4 ms |
2892 KB |
Output is correct |
15 |
Correct |
15 ms |
5196 KB |
Output is correct |
16 |
Correct |
17 ms |
5196 KB |
Output is correct |
17 |
Correct |
12 ms |
5060 KB |
Output is correct |
18 |
Correct |
10 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
30796 KB |
Output is correct |
2 |
Correct |
73 ms |
16252 KB |
Output is correct |
3 |
Correct |
447 ms |
78692 KB |
Output is correct |
4 |
Correct |
136 ms |
29932 KB |
Output is correct |
5 |
Correct |
305 ms |
59088 KB |
Output is correct |
6 |
Correct |
1036 ms |
98232 KB |
Output is correct |
7 |
Correct |
17 ms |
32204 KB |
Output is correct |
8 |
Correct |
17 ms |
30776 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
17 ms |
31576 KB |
Output is correct |
12 |
Correct |
2 ms |
1484 KB |
Output is correct |
13 |
Correct |
66 ms |
16288 KB |
Output is correct |
14 |
Correct |
38 ms |
11084 KB |
Output is correct |
15 |
Correct |
37 ms |
12148 KB |
Output is correct |
16 |
Correct |
29 ms |
5896 KB |
Output is correct |
17 |
Correct |
178 ms |
32132 KB |
Output is correct |
18 |
Correct |
149 ms |
32016 KB |
Output is correct |
19 |
Correct |
124 ms |
29900 KB |
Output is correct |
20 |
Correct |
101 ms |
27680 KB |
Output is correct |
21 |
Correct |
262 ms |
61068 KB |
Output is correct |
22 |
Correct |
299 ms |
59180 KB |
Output is correct |
23 |
Correct |
331 ms |
49352 KB |
Output is correct |
24 |
Correct |
300 ms |
60504 KB |
Output is correct |
25 |
Correct |
671 ms |
78692 KB |
Output is correct |
26 |
Correct |
1016 ms |
143144 KB |
Output is correct |
27 |
Correct |
940 ms |
105580 KB |
Output is correct |
28 |
Correct |
945 ms |
98292 KB |
Output is correct |
29 |
Correct |
967 ms |
95024 KB |
Output is correct |
30 |
Correct |
978 ms |
102696 KB |
Output is correct |
31 |
Correct |
709 ms |
63612 KB |
Output is correct |
32 |
Correct |
985 ms |
103656 KB |
Output is correct |