이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N=4e3+5;
int h, w, cnt, dx[4]={1, 0, 0, -1}, dy[4]={0, 1, -1, 0};
char mp[N][N], c;
queue<pair<int, int>> q, nq;
bool vs[N][N];
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
//#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
cin>>h>>w;
for (int i=1; i<=h; i++) for (int j=1; j<=w; j++) cin>>mp[i][j];
nq.push({1, 1});
vs[1][1]=1;
while (!nq.empty())
{
cnt++;
c=mp[nq.front().first][nq.front().second];
while (!nq.empty()) q.push(nq.front()), nq.pop();
while (!q.empty())
{
auto [x, y]=q.front();
q.pop();
for (int i=0; i<4; i++)
{
int nx=x+dx[i], ny=y+dy[i];
if (nx<1||nx>h||ny<1||ny>w||mp[nx][ny]=='.'||vs[nx][ny]) continue;
vs[nx][ny]=1;
if (mp[nx][ny]==c) q.push({nx, ny});
else nq.push({nx, ny});
}
}
}
cout<<cnt;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |