/* IN THE NAME OF GOD */
/* |\/| /-\ |\| | |\/| /-\ */
#include "bits/stdc++.h"
using namespace std;
#define sz(x) (int)x.size()
#define endl '\n'
#define pb push_back
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define mr make_pair
//#define int long long
#define pii pair<int, int>
typedef long double ld;
typedef long long ll;
mt19937 rng (chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 4e3 + 10;
const int MOD = 1e9 + 7;
const int inf = 1e9 + 1;
int dis[N][N], dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0};
char c[N][N];
int32_t main(){
ios_base:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> c[i][j];
dis[i][j] = inf;
}
}
deque<pii> q;
q.pb(mr(1, 1));
dis[1][1] = 1;
int ans = 0;
while(!q.empty()){
pii v = q.front();
ans = max(ans, dis[v.F][v.S]);
q.pop_front();
for(int i = 0; i < 4; i++){
if(v.F + dx[i] < 0 || v.F + dx[i] > n || v.S + dy[i] <= 0 || v.S + dy[i] > m)
continue;
if(c[v.F + dx[i]][v.S + dy[i]] == '.')
continue;
bool f = (c[v.F][v.S] == c[v.F + dx[i]][v.S + dy[i]]);
if(f){
if(dis[v.F][v.S] < dis[v.F + dx[i]][v.S + dy[i]]){
dis[v.F + dx[i]][v.S + dy[i]] = dis[v.F][v.S];
q.push_front(mr(v.F + dx[i], v.S + dy[i]));
}
}
else{
if(dis[v.F][v.S] + 1 < dis[v.F + dx[i]][v.S + dy[i]]){
dis[v.F + dx[i]][v.S + dy[i]] = dis[v.F][v.S] + 1;
q.pb(mr(v.F + dx[i], v.S + dy[i]));
}
}
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
5204 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
9 ms |
5000 KB |
Output is correct |
5 |
Correct |
4 ms |
2844 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 |
5 ms |
2516 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
7 ms |
2900 KB |
Output is correct |
13 |
Correct |
3 ms |
2872 KB |
Output is correct |
14 |
Correct |
4 ms |
2856 KB |
Output is correct |
15 |
Correct |
14 ms |
5204 KB |
Output is correct |
16 |
Correct |
17 ms |
5228 KB |
Output is correct |
17 |
Correct |
12 ms |
5076 KB |
Output is correct |
18 |
Correct |
9 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
30804 KB |
Output is correct |
2 |
Correct |
81 ms |
16332 KB |
Output is correct |
3 |
Correct |
384 ms |
78760 KB |
Output is correct |
4 |
Correct |
98 ms |
29948 KB |
Output is correct |
5 |
Correct |
262 ms |
59140 KB |
Output is correct |
6 |
Correct |
804 ms |
91724 KB |
Output is correct |
7 |
Correct |
14 ms |
32212 KB |
Output is correct |
8 |
Correct |
17 ms |
30804 KB |
Output is correct |
9 |
Correct |
3 ms |
580 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
14 ms |
31684 KB |
Output is correct |
12 |
Correct |
1 ms |
1492 KB |
Output is correct |
13 |
Correct |
56 ms |
16312 KB |
Output is correct |
14 |
Correct |
36 ms |
11092 KB |
Output is correct |
15 |
Correct |
40 ms |
12056 KB |
Output is correct |
16 |
Correct |
30 ms |
5952 KB |
Output is correct |
17 |
Correct |
167 ms |
32200 KB |
Output is correct |
18 |
Correct |
110 ms |
31764 KB |
Output is correct |
19 |
Correct |
98 ms |
29848 KB |
Output is correct |
20 |
Correct |
97 ms |
27688 KB |
Output is correct |
21 |
Correct |
230 ms |
61184 KB |
Output is correct |
22 |
Correct |
214 ms |
59176 KB |
Output is correct |
23 |
Correct |
277 ms |
49356 KB |
Output is correct |
24 |
Correct |
204 ms |
60584 KB |
Output is correct |
25 |
Correct |
516 ms |
78792 KB |
Output is correct |
26 |
Correct |
698 ms |
119328 KB |
Output is correct |
27 |
Correct |
711 ms |
98408 KB |
Output is correct |
28 |
Correct |
870 ms |
91808 KB |
Output is correct |
29 |
Correct |
790 ms |
90604 KB |
Output is correct |
30 |
Correct |
755 ms |
90496 KB |
Output is correct |
31 |
Correct |
792 ms |
63556 KB |
Output is correct |
32 |
Correct |
785 ms |
91640 KB |
Output is correct |