이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// IN THE NAME OF GOD
#include<bits/stdc++.h>
#define endl '\n'
#define file_reading freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define flush cout.flush();
using namespace std;
typedef long long ll;
typedef long double dll;
typedef unsigned long long ull;
const int N = 4e3+5;
int dat[N][N], mark[N][N], ans, having, must;
queue<pair<int,int>> q,qq;
void bfs(int n, int m){
q.push({n,m});
int x,y;
int tim = 0;
while(having != must){
ans++;
tim++;
if (tim&1){
while(q.size()){
x = q.front().first;
y = q.front().second;
q.pop();
if (mark[x][y]) continue;
mark[x][y] = 1;
having++;
if (x > 1 && dat[x][y] == dat[x-1][y] && !mark[x-1][y]) q.push({x-1,y});
if (x < n && dat[x][y] == dat[x+1][y] && !mark[x+1][y]) q.push({x+1,y});
if (y > 1 && dat[x][y] == dat[x][y-1] && !mark[x][y-1]) q.push({x,y-1});
if (y < m && dat[x][y] == dat[x][y+1] && !mark[x][y+1]) q.push({x,y+1});
if (x > 1 && dat[x][y] != dat[x-1][y] && !mark[x-1][y] && dat[x][y] + dat[x-1][y] == 3) qq.push({x-1,y});
if (x < n && dat[x][y] != dat[x+1][y] && !mark[x+1][y] && dat[x][y] + dat[x+1][y] == 3) qq.push({x+1,y});
if (y > 1 && dat[x][y] != dat[x][y-1] && !mark[x][y-1] && dat[x][y] + dat[x][y-1] == 3) qq.push({x,y-1});
if (y < m && dat[x][y] != dat[x][y+1] && !mark[x][y+1] && dat[x][y] + dat[x][y+1] == 3) qq.push({x,y+1});
}
}
else{
while(qq.size()){
x = qq.front().first;
y = qq.front().second;
qq.pop();
if (mark[x][y]) continue;
mark[x][y] = 1;
having++;
if (x > 1 && dat[x][y] == dat[x-1][y] && !mark[x-1][y]) qq.push({x-1,y});
if (x < n && dat[x][y] == dat[x+1][y] && !mark[x+1][y]) qq.push({x+1,y});
if (y > 1 && dat[x][y] == dat[x][y-1] && !mark[x][y-1]) qq.push({x,y-1});
if (y < m && dat[x][y] == dat[x][y+1] && !mark[x][y+1]) qq.push({x,y+1});
if (x > 1 && dat[x][y] != dat[x-1][y] && !mark[x-1][y] && dat[x][y] + dat[x-1][y] == 3) q.push({x-1,y});
if (x < n && dat[x][y] != dat[x+1][y] && !mark[x+1][y] && dat[x][y] + dat[x+1][y] == 3) q.push({x+1,y});
if (y > 1 && dat[x][y] != dat[x][y-1] && !mark[x][y-1] && dat[x][y] + dat[x][y-1] == 3) q.push({x,y-1});
if (y < m && dat[x][y] != dat[x][y+1] && !mark[x][y+1] && dat[x][y] + dat[x][y+1] == 3) q.push({x,y+1});
}
}
}
}
void solve(){
int n,m; cin >> n >> m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
char g; cin >> g;
if (g == '.') dat[i][j] = 0;
else if (g == 'R') dat[i][j] = 1, must++;
else dat[i][j] = 2, must++;
}
}
bfs(n,m);
cout << ans << endl;
}
int32_t main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t; t=1; //cin >> t;
while(t--){solve();}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |