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;
constexpr int nax = 3005;
char mat[nax][nax];
int thrus[nax][nax][2];
const string str = "RGW";
set<array<int, 4>> s;
int n, m;
bool check_ok(int i, int j, int p) {
if (i < 0 || j < 0 || i >= n || j >= m) return false;
string curr;
if (p == 0) {
for(int k = 0; k < 3; k++) {
curr += mat[i][j+k];
}
}
else {
for(int k = 0; k < 3; k++) {
curr += mat[i+k][j];
}
}
return (curr == str);
}
int thru(int i, int j, int p) {
int cnt = 0;
if (p == 0) {
for(int k = 0; k < 3; k++) {
cnt += check_ok(i-k, j+k, 1);
}
}
else {
for(int k = 0; k < 3; k++) {
cnt += check_ok(i+k, j-k, 0);
}
}
return cnt;
}
void rem(int i, int j, int p) {
if (p == 0) {
for(int k = 0; k < 3; k++) {
mat[i][j+k] = 'X';
}
}
else {
for(int k = 0; k < 3; k++) {
mat[i+k][j] = 'X';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> mat[i][j];
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if (check_ok(i, j, 0)) {
int cnt = thru(i, j, 0);
thrus[i][j][0] = cnt;
// cout << i << " " << j << " 0 " << cnt << "\n";
s.insert({cnt, i, j, 0});
}
if (check_ok(i, j, 1)) {
int cnt = thru(i, j, 1);
// cout << i << " " << j << " 1 " << cnt << "\n";
thrus[i][j][1] = cnt;
s.insert({cnt, i, j, 1});
}
}
}
int ans = 0;
while(!s.empty()) {
auto it = s.begin();
s.erase(it);
int x = (*it)[1], y = (*it)[2], p = (*it)[3];
if (!check_ok(x, y, p)) continue;
ans++;
rem(x, y, p);
for(int i = max(0, x-3); i < min(n, x+3); i++) {
for(int j = max(0, y-3); j < min(m, y+3); j++) {
if (check_ok(i, j, 0)) {
int cnt = thru(i, j, 0);
if (cnt < thrus[i][j][0]) {
thrus[i][j][0] = cnt;
s.insert({cnt, i, j, 0});
}
}
if (check_ok(i, j, 1)) {
int cnt = thru(i, j, 1);
if (cnt < thrus[i][j][1]) {
thrus[i][j][1] = cnt;
s.insert({cnt, i, j, 1});
}
}
}
}
}
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |