이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |