mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-08-31 23:41:41 +00:00
feat: 实现炮兵阵地问题的动态规划解法
添加动态规划解决方案来计算炮兵阵地的最大部署数量。使用状态压缩和预处理来优化性能,处理输入地图并计算合法状态及其对应的炮兵数量。
This commit is contained in:
parent
84436ffbbe
commit
8b1f7317ff
@ -1,3 +1,81 @@
|
||||
int main(){
|
||||
|
||||
}
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
static inline constexpr int popcount(int x) {
|
||||
return __builtin_popcount(x);
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios_base::sync_with_stdio(false);
|
||||
std::cin.tie(NULL);
|
||||
|
||||
int n, m;
|
||||
std::cin >> n >> m;
|
||||
|
||||
std::vector<int> map(n + 1, 0);
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
std::string row;
|
||||
std::cin >> row;
|
||||
for (int j = 0; j < m; ++j) {
|
||||
if (row[j] == 'H') {
|
||||
map[i] |= (1 << j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int> states;
|
||||
std::vector<int> counts;
|
||||
for (int i = 0; i < (1 << m); ++i) {
|
||||
|
||||
if (!((i << 1) & i) && !((i << 2) & i)) {
|
||||
states.push_back(i);
|
||||
counts.push_back(popcount(i));
|
||||
}
|
||||
}
|
||||
int num = states.size();
|
||||
|
||||
std::vector<std::vector<std::vector<int>>> dp(n + 1, std::vector<std::vector<int>>(num, std::vector<int>(num, 0)));
|
||||
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
|
||||
for (int j = 0; j < num; ++j) {
|
||||
int si = states[j];
|
||||
|
||||
if (si & map[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int k = 0; k < num; ++k) {
|
||||
int sim1 = states[k];
|
||||
|
||||
if (si & sim1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int l = 0; l < num; ++l) {
|
||||
int sim2 = states[l];
|
||||
|
||||
if ((si & sim2) || (sim1 & sim2)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dp[i][j][k] = std::max(dp[i][j][k], dp[i - 1][k][l] + counts[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
for (int j = 0; j < num; ++j) {
|
||||
for (int k = 0; k < num; ++k) {
|
||||
ans = std::max(ans, dp[n][j][k]);
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << ans << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user