This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
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[505][505];
int d[505][505];
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |