//0 1 1 0 1
//0 1 0 0 1
//1 0 0 1 1
//0 1 1 0 1
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx2")
using namespace std;
#define F first
#define S second
#define pb push_back
#define sze size()
#define all(x) x.begin() , x.end()
#define wall__ cout << "--------------------------------------\n";
#define kids int mid = (tl + tr) >> 1, cl = v << 1, cr = v << 1 | 1
#define file_io freopen("input.cpp", "r", stdin); freopen("output.cpp", "w", stdout);
typedef long long ll;
typedef long double dl;
typedef pair < short int , short int > pii;
typedef pair < int , ll > pil;
typedef pair < ll , int > pli;
typedef pair < ll , ll > pll;
typedef pair < int , pii > piii;
typedef pair < ll, pll > plll;
const ll N = 4000 + 3;
const ll M = N * N;
const ll mod = 1e9 + 7;
const ll inf = 2e16;
const ll rinf = -2e16;
const ll INF = 1e9 + 10;
const ll rINF = -1e9 - 10;
const ll lg = 32;
int n, m, h[N][N], ans;
char c[N][N];
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) c[i][0] = '.';
for (int i = 1; i <= m; i++) c[0][i] = '.';
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> c[i][j];
h[i][j] = INF;
}
deque < pii > q; q.pb({1, 1}); h[1][1] = 0;
while (q.sze) {
pii v = q.back();
q.pop_back();
short int x = v.F, y = v.S; ans = max(h[x][y], ans);
for (short int i = -1; i <= 1; i++) {
if (c[x + i][y] == c[x][y]) {
if (h[x][y] < h[x + i][y]) {
h[x + i][y] = h[x][y];
q.push_back({x + i, y});
}
} else if (c[x + i][y] != '.') {
if (h[x][y] + 1 < h[x + i][y]) {
h[x + i][y] = h[x][y] + 1;
q.push_front({x + i, y});
}
}
if (c[x][y + i] == c[x][y]) {
if (h[x][y] < h[x][y + i]) {
h[x][y + i] = h[x][y];
q.push_back({x, y + i});
}
} else if (c[x][y + i] != '.') {
if (h[x][y] + 1 < h[x][y + i]) {
h[x][y + i] = h[x][y] + 1;
q.push_front({x, y + i});
}
}
}
}
cout << ++ans << '\n';
return 0;
}
/*
5 12
RRRRRRRR....
F...F..RRRRR
R...F..FR..R
RR..FF.FFFFR
FRRRRFFFFFRR
*/
//shrek will AC this;
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
5280 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
8 ms |
5060 KB |
Output is correct |
5 |
Correct |
5 ms |
2900 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 |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
8 ms |
3028 KB |
Output is correct |
13 |
Correct |
4 ms |
2908 KB |
Output is correct |
14 |
Correct |
5 ms |
3028 KB |
Output is correct |
15 |
Correct |
13 ms |
5460 KB |
Output is correct |
16 |
Correct |
15 ms |
5276 KB |
Output is correct |
17 |
Correct |
12 ms |
5184 KB |
Output is correct |
18 |
Correct |
9 ms |
5056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
30804 KB |
Output is correct |
2 |
Correct |
54 ms |
17864 KB |
Output is correct |
3 |
Correct |
335 ms |
94380 KB |
Output is correct |
4 |
Correct |
93 ms |
33536 KB |
Output is correct |
5 |
Correct |
216 ms |
67880 KB |
Output is correct |
6 |
Correct |
709 ms |
101304 KB |
Output is correct |
7 |
Correct |
16 ms |
32212 KB |
Output is correct |
8 |
Correct |
14 ms |
30892 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
480 KB |
Output is correct |
11 |
Correct |
14 ms |
31700 KB |
Output is correct |
12 |
Correct |
2 ms |
1492 KB |
Output is correct |
13 |
Correct |
54 ms |
17904 KB |
Output is correct |
14 |
Correct |
36 ms |
12108 KB |
Output is correct |
15 |
Correct |
29 ms |
13092 KB |
Output is correct |
16 |
Correct |
27 ms |
6604 KB |
Output is correct |
17 |
Correct |
138 ms |
36160 KB |
Output is correct |
18 |
Correct |
115 ms |
35636 KB |
Output is correct |
19 |
Correct |
99 ms |
33580 KB |
Output is correct |
20 |
Correct |
86 ms |
30964 KB |
Output is correct |
21 |
Correct |
214 ms |
70172 KB |
Output is correct |
22 |
Correct |
234 ms |
67904 KB |
Output is correct |
23 |
Correct |
258 ms |
56784 KB |
Output is correct |
24 |
Correct |
201 ms |
69572 KB |
Output is correct |
25 |
Correct |
523 ms |
94312 KB |
Output is correct |
26 |
Correct |
401 ms |
105872 KB |
Output is correct |
27 |
Correct |
563 ms |
108692 KB |
Output is correct |
28 |
Correct |
729 ms |
101172 KB |
Output is correct |
29 |
Correct |
699 ms |
101748 KB |
Output is correct |
30 |
Correct |
671 ms |
102796 KB |
Output is correct |
31 |
Correct |
602 ms |
73552 KB |
Output is correct |
32 |
Correct |
493 ms |
103352 KB |
Output is correct |