/*
Khangnh's code
"You can either experience the pain of discipline or the pain of regret.
The choice is yours."
*/
// - Only when necessary :d
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define fileopen(a, b) freopen(((string)a + ".inp").c_str(), "r", stdin); freopen(((string)b + ".out").c_str(), "w", stdout);
#define ll long long
// #define int long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define eb emplace_back
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f;
const double pi = acos(-1);
const double eps = 1e-9;
const int dx[] = {-1, 1, 0, 0},
dy[] = {0, 0, -1, 1};
int n, m;
char a[4005][4005];
int d[4005][4005];
int ans = 0;
bool inside(int x, int y) {
if (x < 1 || x > n || y < 1 || y > m) return false;
return true;
}
void bfs(pii s) {
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
d[i][j] = inf;
deque<pii> dq;
dq.pf(s);
d[s.fi][s.se] = 1;
while (!dq.empty()) {
int x = dq.front().fi, y = dq.front().se;
dq.pof();
ans = max(ans, d[x][y]);
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i], ny = y + dy[i];
if (!inside(nx, ny)) continue;
if (a[nx][ny] == '.') continue;
bool w = a[x][y] != a[nx][ny];
if (d[x][y] + w < d[nx][ny]) {
d[nx][ny] = d[x][y] + w;
(w ? dq.pb({nx, ny}) : dq.pf({nx, ny}));
}
}
}
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
cin >> a[i][j];
bfs({1, 1});
cout << ans;
}
signed main() {
#ifdef LOCAL
fileopen("input", "output");
auto start = clock();
#endif
#ifndef LOCAL
// fileopen("LAH", "LAH");
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int test = 1;
// cin >> test;
for (int tc = 1; tc <= test; ++tc) solve();
#ifdef LOCAL
auto end = clock();
cout << "\n\nExecution time : " << double(end - start)/CLOCKS_PER_SEC << "[s]";
#endif
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
5324 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 |
5068 KB |
Output is correct |
5 |
Correct |
4 ms |
3020 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 |
5 ms |
2636 KB |
Output is correct |
11 |
Correct |
3 ms |
2124 KB |
Output is correct |
12 |
Correct |
6 ms |
3016 KB |
Output is correct |
13 |
Correct |
5 ms |
3020 KB |
Output is correct |
14 |
Correct |
4 ms |
3024 KB |
Output is correct |
15 |
Correct |
13 ms |
5360 KB |
Output is correct |
16 |
Correct |
17 ms |
5348 KB |
Output is correct |
17 |
Correct |
14 ms |
5172 KB |
Output is correct |
18 |
Correct |
10 ms |
5148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
30924 KB |
Output is correct |
2 |
Correct |
57 ms |
17260 KB |
Output is correct |
3 |
Correct |
391 ms |
87600 KB |
Output is correct |
4 |
Correct |
115 ms |
32580 KB |
Output is correct |
5 |
Correct |
241 ms |
66272 KB |
Output is correct |
6 |
Correct |
814 ms |
100316 KB |
Output is correct |
7 |
Correct |
17 ms |
32400 KB |
Output is correct |
8 |
Correct |
14 ms |
30852 KB |
Output is correct |
9 |
Correct |
3 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
15 ms |
31712 KB |
Output is correct |
12 |
Correct |
2 ms |
1612 KB |
Output is correct |
13 |
Correct |
57 ms |
17192 KB |
Output is correct |
14 |
Correct |
37 ms |
11340 KB |
Output is correct |
15 |
Correct |
32 ms |
12384 KB |
Output is correct |
16 |
Correct |
27 ms |
6136 KB |
Output is correct |
17 |
Correct |
147 ms |
35012 KB |
Output is correct |
18 |
Correct |
123 ms |
34804 KB |
Output is correct |
19 |
Correct |
110 ms |
32616 KB |
Output is correct |
20 |
Correct |
91 ms |
29996 KB |
Output is correct |
21 |
Correct |
239 ms |
68676 KB |
Output is correct |
22 |
Correct |
281 ms |
66812 KB |
Output is correct |
23 |
Correct |
286 ms |
55748 KB |
Output is correct |
24 |
Correct |
280 ms |
68340 KB |
Output is correct |
25 |
Correct |
626 ms |
87552 KB |
Output is correct |
26 |
Correct |
526 ms |
126332 KB |
Output is correct |
27 |
Correct |
753 ms |
100860 KB |
Output is correct |
28 |
Correct |
788 ms |
96676 KB |
Output is correct |
29 |
Correct |
763 ms |
96936 KB |
Output is correct |
30 |
Correct |
724 ms |
96560 KB |
Output is correct |
31 |
Correct |
639 ms |
69404 KB |
Output is correct |
32 |
Correct |
634 ms |
99020 KB |
Output is correct |