QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#268345 | #7052. Xian Xiang | 8BQube# | AC ✓ | 469ms | 4424kb | C++20 | 3.1kb | 2023-11-28 16:06:40 | 2023-11-28 16:06:41 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define SZ(a) ((int)a.size())
string grid[10][10], val[20];
pii pos[20];
int score[10], mat[20][20];
int dp[1 << 18], num[10][10];
void chmax(int &x, int v) {
x = max(x, v);
}
int sign(int x) {
return x == 0 ? 0 : x > 0 ? 1 : -1;
}
void solve() {
int n, m, k, pt = 0;
cin >> n >> m >> k;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
cin >> grid[i][j];
for (int i = 0; i <= k; ++i)
cin >> score[i];
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
if (grid[i][j][0] != '-') {
num[i][j] = pt;
val[pt] = grid[i][j];
pos[pt] = pii(i, j);
++pt;
}
else
num[i][j] = -1;
}
if (pt == 0) {
cout << "0\n";
return;
}
for (int i = 0; i < pt; ++i)
for (int j = i + 1; j < pt; ++j) {
int cnt = 0;
for (int p = 0; p < k; ++p)
if (val[i][p] == val[j][p])
++cnt;
mat[i][j] = mat[j][i] = score[cnt];
}
auto chk = [&](int mask, int u, int v) {
if ([&](){
auto [x, y] = pos[u];
while (x != pos[v].X) {
x += sign(pos[v].X - x);
if (pii(x, y) != pos[v] && num[x][y] >= 0 && !(mask >> num[x][y] & 1))
return false;
}
while (y != pos[v].Y) {
y += sign(pos[v].Y - y);
if (pii(x, y) != pos[v] && num[x][y] >= 0 && !(mask >> num[x][y] & 1))
return false;
}
return true;
}()) return true;
if ([&](){
auto [x, y] = pos[u];
while (y != pos[v].Y) {
y += sign(pos[v].Y - y);
if (pii(x, y) != pos[v] && num[x][y] >= 0 && !(mask >> num[x][y] & 1))
return false;
}
while (x != pos[v].X) {
x += sign(pos[v].X - x);
if (pii(x, y) != pos[v] && num[x][y] >= 0 && !(mask >> num[x][y] & 1))
return false;
}
return true;
}()) return true;
return false;
};
fill_n(dp, 1 << pt, 0);
for (int i = 0; i < (1 << pt); ++i) {
if (i != 0 && !dp[i]) continue;
for (int j = 0; j < pt; ++j)
if (!(i >> j & 1)) {
for (int p = j + 1; p < pt; ++p)
if (!(i >> p & 1) && chk(i, j, p)) {
//cerr << "dp " << i << " relax " << (i | (1 << j) | (1 << p)) << " (match " << j << " " << p << ") " << dp[i] << " + " << mat[j][p] << "\n";
chmax(dp[i | (1 << j) | (1 << p)], dp[i] + mat[j][p]);
}
}
}
cout << dp[(1 << pt) - 1] << "\n";
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3340kb
input:
3 2 2 3 aaa aaa bbb bbb 1 10 100 1000 2 3 3 aaa --- bbb bbb --- aaa 1 10 100 1000 1 4 3 aaa bba abb aaa 1 10 100 1000
output:
2000 2 1010
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 469ms
memory: 4424kb
input:
20 5 6 5 dbdaa cbcdc ----- dadad dbdaa bdbcc dbbdc aadcc dbbdc bdbad ----- ----- bccad abdab ----- bbdda ----- ddcbd adcbb ----- ----- ----- bdbcc ----- ----- ----- bbdda ----- ----- ----- 156 1333 2713 2853 3242 9881 6 7 4 ---- acaa ---- ---- ---- ---- ---- accc accc dcda ---- cbcb bbda ---- bbda d...
output:
49276 65059 38746 34398 39744 73820 31466 33302 43342 26102 45570 69039 61574 63160 66552 57157 69615 76688 79535 34563
result:
ok 20 lines