// Zende bad Shoma nasime faghat !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const ll Mod = 1000000007LL;
const int N = 4e3 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;
int n, m, dis[N][N];
str A[N];
pii adj[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
deque<pii> dq;
inline bool val(int x, int y){
return (0 <= x) && (x < n) && (0 <= y) && (y < m) && (A[x][y] != '.');
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++)
cin >> A[i];
if(A[0][0] == '.') return cout << "0\n", 0;
memset(dis, 31, sizeof dis);
dis[0][0] = 1;
dq.pb({0, 0});
int x, y, nx, ny;
int ans = 1;
while(!dq.empty()){
x = dq.front().F; y = dq.front().S;
dq.pop_front();
//cerr << "# " << x << ' ' << y << '\n';
for(auto &[X, Y] : adj){
nx = x + X; ny = y + Y;
//cerr << nx << ' ' << ny << '\n';
//if(val(nx, ny)) cerr << nx << ' ' << ny << ' ' << A[nx][ny] << '\n';
if(val(nx, ny) && dis[nx][ny] > dis[x][y] + (A[x][y] == A[nx][ny] ? 0 : 1)){
if(A[x][y] == A[nx][ny]){
dis[nx][ny] = dis[x][y];
dq.push_front({nx, ny});
} else {
dis[nx][ny] = dis[x][y] + 1;
dq.push_back({nx, ny});
ans = dis[nx][ny];
}
}
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
63864 KB |
Output is correct |
2 |
Correct |
34 ms |
63352 KB |
Output is correct |
3 |
Correct |
33 ms |
63352 KB |
Output is correct |
4 |
Correct |
39 ms |
63872 KB |
Output is correct |
5 |
Correct |
34 ms |
63612 KB |
Output is correct |
6 |
Correct |
33 ms |
63352 KB |
Output is correct |
7 |
Correct |
38 ms |
63352 KB |
Output is correct |
8 |
Correct |
38 ms |
63356 KB |
Output is correct |
9 |
Correct |
38 ms |
63352 KB |
Output is correct |
10 |
Correct |
35 ms |
63480 KB |
Output is correct |
11 |
Correct |
35 ms |
63480 KB |
Output is correct |
12 |
Correct |
37 ms |
63608 KB |
Output is correct |
13 |
Correct |
40 ms |
63608 KB |
Output is correct |
14 |
Correct |
44 ms |
63608 KB |
Output is correct |
15 |
Correct |
47 ms |
63864 KB |
Output is correct |
16 |
Correct |
47 ms |
64120 KB |
Output is correct |
17 |
Correct |
45 ms |
63864 KB |
Output is correct |
18 |
Correct |
37 ms |
63864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
63480 KB |
Output is correct |
2 |
Correct |
68 ms |
66680 KB |
Output is correct |
3 |
Correct |
193 ms |
96760 KB |
Output is correct |
4 |
Correct |
80 ms |
71032 KB |
Output is correct |
5 |
Correct |
165 ms |
81912 KB |
Output is correct |
6 |
Correct |
598 ms |
110084 KB |
Output is correct |
7 |
Correct |
35 ms |
63480 KB |
Output is correct |
8 |
Correct |
35 ms |
63488 KB |
Output is correct |
9 |
Correct |
36 ms |
63480 KB |
Output is correct |
10 |
Correct |
38 ms |
63488 KB |
Output is correct |
11 |
Correct |
35 ms |
63480 KB |
Output is correct |
12 |
Correct |
39 ms |
63480 KB |
Output is correct |
13 |
Correct |
72 ms |
66552 KB |
Output is correct |
14 |
Correct |
57 ms |
65272 KB |
Output is correct |
15 |
Correct |
56 ms |
65400 KB |
Output is correct |
16 |
Correct |
49 ms |
64764 KB |
Output is correct |
17 |
Correct |
126 ms |
71672 KB |
Output is correct |
18 |
Correct |
96 ms |
71444 KB |
Output is correct |
19 |
Correct |
85 ms |
71032 KB |
Output is correct |
20 |
Correct |
75 ms |
70392 KB |
Output is correct |
21 |
Correct |
136 ms |
82712 KB |
Output is correct |
22 |
Correct |
166 ms |
81912 KB |
Output is correct |
23 |
Correct |
198 ms |
79352 KB |
Output is correct |
24 |
Correct |
133 ms |
82040 KB |
Output is correct |
25 |
Correct |
384 ms |
96888 KB |
Output is correct |
26 |
Correct |
604 ms |
139016 KB |
Output is correct |
27 |
Correct |
497 ms |
115152 KB |
Output is correct |
28 |
Correct |
555 ms |
110068 KB |
Output is correct |
29 |
Correct |
564 ms |
107808 KB |
Output is correct |
30 |
Correct |
547 ms |
113524 KB |
Output is correct |
31 |
Correct |
518 ms |
85112 KB |
Output is correct |
32 |
Correct |
491 ms |
113620 KB |
Output is correct |