有没有 WA #25 的人来帮帮孩子啊?5k 代码调不出来了/ll
#include <bits/stdc++.h>
using namespace std;
const int N = 60;
typedef pair<int, int> PII;
#define x first
#define y second
int n, m, k, q;
int a[N][N], b[N][N], c[N][N];
int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
int col[2];
bool st[N][N];
int res;
vector<PII> S;
int buc[N << 1];
bool vaild(int x, int y, int p, int q) {
for (int d = 0; d < 4; d ++ ) {
int _a = x + dx[d], _b = y + dy[d];
if (_a == p && _b == q) return 1;
} return 0;
}
queue<PII> qu;
void fall() {
for (int i = 1; i <= m; i ++ ) {
vector<PII> cl;
for (int j = n; j; j -- ) {
if (!st[j][i]) cl.push_back({a[j][i], b[j][i]});
}
for (int j = n; j >= n - (int)cl.size() + 1; j -- )
a[j][i] = cl[n - j].x, b[j][i] = cl[n - j].y;
for (int j = n - (int)cl.size(); j; j -- ) a[j][i] = b[j][i] = 0;
}
}
int sz = 0;
void dfs(int x, int y) {
sz ++ ; st[x][y] = 1;
for (int d = 0; d < 4; d ++ ) {
int _a = x + dx[d], _b = y + dy[d];
if (_a < 1 || _a > n || _b < 1 || _b > m || c[_a][_b] != c[x][y] || st[_a][_b]) continue ;
dfs(_a, _b);
}
}
int c1, c2, c3;
void calc() {
memset(st, 0, sizeof st);
for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ ) {
if (c[i][j] && !st[i][j]) {
sz = 0; dfs(i, j);
res += 50 * (sz - 3) * (sz - 3); c3 += 50 * (sz - 3) * (sz - 3);
}
}
}
int main() {
freopen("P8422_25.in", "r", stdin);
scanf("%d%d%d%d", &n, &m, &k, &q);
for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ ) scanf("%d", &a[i][j]);
for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ ) scanf("%d", &b[i][j]);
bool f1 = 0;
while (q -- ) {
int x, y, _x, _y; c1 = 0, c2 = 0, c3 = 0;
scanf("%d%d%d%d", &x, &y, &_x, &_y);
if (!a[x][y] || !a[_x][_y] || !vaild(x, y, _x, _y)) { f1 = 1; continue ; }
swap(a[x][y], a[_x][_y]); swap(b[x][y], b[_x][_y]);
int cnt = 0;
auto rebuild = [&]() -> int {
memset(c, 0, sizeof c); while (qu.size()) qu.pop();
int fl = -1; col[0] = col[1] = -1;
for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ ) {
if (j > 1 && j < m) {
if (!a[i][j] || c[i][j]) continue ;
int L = j;
while (L && a[i][L] == a[i][j]) L -- ;
int R = j;
while (R <= m && a[i][R] == a[i][j]) R ++ ;
if (R - L - 1 >= 3) {
for (int _ = L + 1; _ <= R - 1; _ ++ )
qu.push({i, _}), c[i][_] = a[i][j];
if (col[0] == -1) col[0] = a[i][j];
if (~col[0] && col[0] != a[i][j]) col[1] = a[i][j];
fl = 1;
}
}
if (i > 1 && i < n) {
if (!a[i][j] || c[i][j]) continue ;
int L = i, R = i;
while (L && a[L][j] == a[i][j]) L -- ;
while (R <= n && a[R][j] == a[i][j]) R ++ ;
if (R - L - 1 >= 3) {
for (int _ = L + 1; _ <= R - 1; _ ++ )
qu.push({_, j}), c[_][j] = a[i][j];
if (col[0] == -1) col[0] = a[i][j];
if (~col[0] && col[0] != a[i][j]) col[1] = a[i][j];
fl = 1;
}
}
}
if (!cnt) S.push_back({col[0], col[1]});
return fl;
};
auto del = [&]() -> void {
memset(st, 0, sizeof st);
while (qu.size()) {
PII t = qu.front(); qu.pop();
int x = t.x, y = t.y;
if (st[x][y]) continue ;
st[x][y] = 1; res += a[x][y] * cnt, c1 += a[x][y] * cnt;
if (b[x][y] == 1) {
for (int i = 1; i <= m; i ++ ) if (!st[x][i]) qu.push({x, i});
} else if (b[x][y] == 2) {
for (int i = 1; i <= n; i ++ ) if (!st[i][y]) qu.push({i, y});
} else if (b[x][y] == 3) {
for (int i = 1; i <= m; i ++ ) if (!st[x][i]) qu.push({x, i});
for (int i = 1; i <= n; i ++ ) if (!st[i][y]) qu.push({i, y});
} else if (b[x][y] == 4) {
for (int i = max(1, x - 1); i <= min(n, x + 1); i ++ )
for (int j = max(1, y - 1); j <= min(m, y + 1); j ++ )
if (!st[i][j]) qu.push({i, j});
} else if (b[x][y] == 5) {
for (int i = max(1, x - 2); i <= min(n, x + 2); i ++ )
for (int j = max(1, y - 2); j <= min(m, y + 2); j ++ )
if (!st[i][j]) qu.push({i, j});
} else if (b[x][y] == 6) {
for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ )
if (!st[i][j] && a[i][j] == a[x][y]) qu.push({i, j});
}
}
};
while (~rebuild()) {
cnt ++ ;
del();
fall(); calc();
}
if (!cnt) {
swap(a[x][y], a[_x][_y]); swap(b[x][y], b[_x][_y]);
S.pop_back(); f1 = 1; continue ;
}
res += 80 * (cnt - 1) * (cnt - 1); c2 += 80 * (cnt - 1) * (cnt - 1);
if ((int)S.size() == 5) {
int maxv = -1;
for (int _ = 0; _ < 32; _ ++ ) {
vector<int> pos; bool flg = 1;
for (int i = 0; i < 5; i ++ )
if (_ >> i & 1) {
if (S[i].y == -1) flg = 0;
pos.push_back(S[i].y);
} else pos.push_back(S[i].x);
if (!flg) continue ;
for (int i = 0; i < 5; i ++ ) buc[pos[i]] ++ ;
sort(pos.begin(), pos.end(), [](int x, int y) {
if (buc[x] != buc[y]) return buc[x] > buc[y];
return x > y;
});
int ans = 0;
if (buc[pos[0]] == 5) ans = 1000 + pos[0] * 10;
else if (buc[pos[0]] == 4) ans = 750 + pos[0] * 5;
else if (buc[pos[0]] == 3 && pos[3] == pos[4]) ans = 500 + pos[0] * 3 + pos[3];
else if (buc[pos[0]] == 3 && pos[3] != pos[4]) ans = 300 + pos[0] * 3;
else if (pos[0] == pos[1] && pos[2] == pos[3]) ans = 200 + pos[0] * 2 + pos[3];
else if (pos[0] == pos[1]) ans = 100 + pos[0] * 2;
else ans = 50 + pos[0];
for (int v : pos) buc[v] = 0;
maxv = max(maxv, ans);
} res += maxv; S.clear();
}
// poker score.
}
if (!f1) res += 1000;
bool f2 = 0;
for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ )
if (a[i][j]) f2 = 1;
if (!f2) res += 10000;
printf("%d\n", res); return 0;
}