QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#472013 | #5416. Fabulous Fungus Frenzy | caijianhong | TL | 0ms | 3816kb | C++14 | 3.0kb | 2024-07-11 13:11:35 | 2024-07-11 13:11:36 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
using LL = long long;
vector<tuple<int, int, int>> fans;
int loc[30][30], tim;
struct matrix {
int n, m;
vector<char> a;
matrix() = default;
matrix(int n, int m) : n(n), m(m), a(n * m) {
for (int i = 0; i < n * m; i++) cin >> a[i];
}
char* operator[](int i) { return a.data() + i * m; }
valarray<int> count() {
valarray<int> buc(128);
for (int i = 0; i < n * m; i++) if (a[i] != '*') buc[(int)a[i]] += 1;
return buc;
}
void moveone(char ch, int tx, int ty) {
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
if (loc[x][y] < tim && a[x * m + y] == ch) {
while (y < ty) {
fans.emplace_back(-1, x, y);
swap(a[x * m + y], a[x * m + y + 1]);
++y;
}
while (y > ty) {
fans.emplace_back(-2, x, y);
swap(a[x * m + y], a[x * m + y - 1]);
--y;
}
while (x < tx) {
fans.emplace_back(-3, x, y);
swap(a[x * m + y], a[(x + 1) * m + y]);
++x;
}
while (x > tx) {
fans.emplace_back(-4, x, y);
swap(a[x * m + y], a[(x - 1) * m + y]);
--x;
}
return ;
}
}
}
throw "Not Found!";
}
};
int n, m, k, star;
matrix A, B, f[30];
int main() {
#ifndef LOCAL
cin.tie(nullptr)->sync_with_stdio(false);
#endif
cin >> n >> m >> k;
B = matrix(n, m);
A = matrix(n, m);
for (int i = 1; i <= k; i++) {
int r, c;
cin >> r >> c;
f[i] = matrix(r, c);
}
while ((A.count() > B.count()).max()) {
auto ca = A.count();
bool flag = false;
for (int t = 1; t <= k; t++) {
auto cf = f[t].count();
valarray<int> v = cf - ca;
v[v < 0] = 0;
if (v.sum() <= min(f[t].n * f[t].m - 1, n * m - ca.sum())) {
++tim;
for (int i = 0; i < f[t].n; i++) {
for (int j = 0; j < f[t].m; j++) {
if (v[f[t][i][j]]) --v[f[t][i][j]], A.moveone('*', i, j);
else A.moveone(f[t][i][j], i, j);
loc[i][j] = tim;
}
}
fans.emplace_back(t, 0, 0);
for (int i = 0; i < f[t].n; i++) {
for (int j = 0; j < f[t].m; j++) {
A[i][j] = '*';
}
}
flag = true;
break;
}
}
if (!flag) {
cout << -1 << endl;
return 0;
}
}
valarray<int> v = B.count() - A.count();
v[v < 0] = 0;
++tim;
for (int i = 0; i < B.n; i++) {
for (int j = 0; j < B.m; j++) {
if (v[B[i][j]]) --v[B[i][j]], A.moveone('*', i, j);
else A.moveone(B[i][j], i, j);
loc[i][j] = tim;
}
}
reverse(fans.begin(), fans.end());
cout << fans.size() << endl;
for (auto [op, x, y] : fans) cout << op << " " << x + 1 << " " << y + 1 << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3816kb
input:
3 3 1 OOO GOG BGB OOO GGG BBB 3 1 B G B
output:
5 1 1 1 -2 3 2 -2 2 2 -4 2 1 -4 3 1
result:
ok puzzle solved
Test #2:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
2 2 1 OO OO PP PP 1 2 OP
output:
-1
result:
ok puzzle solved
Test #3:
score: -100
Time Limit Exceeded
input:
4 8 4 11122222 33344444 55556666 77777777 NIxSHUOx DExDUIxx DANxSHIx YUANSHEN 2 3 NIy DEx 3 8 zzzzzzzz DANNSH9I YUA9SHEN 1 1 x 2 5 SHO8y DUUI8