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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using tiii = tuple<int, int, int>;
using str = string;
using ld = long double;
#define FOR(i,a,b) for(int i = (a);i < (b);i ++)
#define ROF(i,a,b) for(int i = (a);i >= (b);i --)
#define For(i,b) for(int i = 0;i < (b);i ++)
#define Rof(i,b) for(int i = (b);i >= 0;i --)
#define trav(a, b) for(auto a : (b))
#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define lb(x, y) lower_bound(all(x), y)
#define ub(x, y) upper_bound(all(x), y)
#define beg(x) (x).begin()
#define en(x) (x).end()
#define pb push_back
#define eb emplace_back
#define ins insert
#define mk make_pair
#define mt make_tuple
#define f first
#define s second
const int N = 4000;
int n, m;
int level[N][N];
char a[N][N];
deque<tiii> q;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
void bfs_01() {
q.pb({0, 0, 0});
level[0][0] = 0;
while(sz(q)) {
auto x = q.front();
int d, i, j;tie(d, i, j) = x;
q.pop_front();
if(d != level[i][j]) continue;
FOR(k,0,4) {
int nx = i + dx[k], ny = j + dy[k];
if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if(a[nx][ny] == '.') continue;
if(level[nx][ny] > level[i][j] + (a[i][j] != a[nx][ny])) {
level[nx][ny] = level[i][j] + (a[i][j] != a[nx][ny]);
if(a[i][j] != a[nx][ny]) {
q.pb({level[nx][ny], nx, ny});
} else {
q.push_front({level[nx][ny], nx, ny});
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
For(i,n) {
For(j,m) {
cin >> a[i][j];
level[i][j] = INT_MAX;
}
}
bfs_01();
int ans = 0;
For(i,n) {
For(j,m) {
if(a[i][j] != '.') {
ans = max(ans, level[i][j]);
}
}
}
cout << ans + 1 << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |