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>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define bug(x) cerr << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const int N = 4005;
int n, m, dx[]={-1, 0, 0, 1}, dy[]={0, -1, 1, 0};
char a[N][N];
bool vis[N][N];
queue<pll> Q, Q2;
inline bool chk(int x, int y)
{ return x >= 1 && x <= n && y >= 1 && y <= m; }
int main(void)
{ IO
int i, ans = 0;
cin >> n >> m;
for (i=1; i <= n; ++i)
for (int j=1; j <= m; ++j) cin >> a[i][j];
if (a[1][1] != a[n][m]) assert(0);
Q.push({1, 1}), vis[1][1] = 1;
while (1)
{
++ans;
while (!Q.empty())
{
int x = Q.front().F, y = Q.front().S; Q.pop();
for (int d=0; d < 4; ++d)
{
int xx = x + dx[d], yy = y + dy[d];
if (!chk(xx, yy) || vis[xx][yy] || a[xx][yy] == '.') continue;
vis[xx][yy] = 1;
if (a[xx][yy] != a[x][y]) Q2.push({xx, yy});
else Q.push({xx, yy});
}
}
if (Q2.empty()) break;
while (!Q2.empty())
Q.push(Q2.front()), Q2.pop();
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |