This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <string.h>
#include <functional>
#define task ""
#define size() size() * 1ll
#define all(x) (x).begin(), (x).end()
#define allr(x, sz) (x) + 1, (x) + 1 + sz
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define MASK(x) (1LL << (x))
#define BIT(x,i) (((x) >> (i)) & 1)
#define numbit(x) __builtin_popcountll(x)
using namespace std;
const int N = 1e6 + 1;
const int M = 4e3 + 1;
const long long mod = 1e9 + 7;
const long long oo = 1e18 + 7;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef long long ll;
template<class t>
bool mini(t &x,t y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
template<class t>
bool maxi(t &x,t y) {
if (x < y) {
x = y;
return 1;
}
return 0;
}
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int n, m;
char a[M][M];
int d[M][M];
void solve(int test = -1) {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
deque<pii> dq;
dq.pb(pii(1, 1));
d[1][1] = 1;
int res = 0;
while (dq.size()) {
pii u = dq.front();
dq.pop_front();
maxi(res, d[u.fi][u.se]);
for (int i = 0; i < 4; i++) {
pii v = pii(u.fi + dx[i], u.se + dy[i]);
if (v.fi < 1 || v.se < 1 || v.fi > n || v.se > m || a[v.fi][v.se] == '.' || d[v.fi][v.se] != 0) continue;
if (a[u.fi][u.se] == a[v.fi][v.se]) {
d[v.fi][v.se] = d[u.fi][u.se];
dq.push_front(v);
} else {
d[v.fi][v.se] = d[u.fi][u.se] + 1;
dq.pb(v);
}
}
}
cout << res;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
int T = 1;
// cin >> T;
for (int i = 1; i <= T; i++) {
solve(i);
}
return 0;
}
/*
*/
Compilation message (stderr)
tracks.cpp: In function 'void solve(int)':
tracks.cpp:8:23: warning: '*' in boolean context, suggest '&&' instead [-Wint-in-bool-context]
8 | #define size() size() * 1ll
| ^
tracks.cpp:62:15: note: in expansion of macro 'size'
62 | while (dq.size()) {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |