writer,coder,jcer all from
状压,全称(状态压缩)
-
引入
-
状压定义
- 不用说了将dp状态进行压缩的dp,可以让我们更方便的推出状转
-
状压
数组中做状压
-
题型及思路
蓝边射手被LeavingZ禁止玩《原神》。然而,今天LeavingZ前往华中科技大学,网络科学与工程学院,去收获2024年中国湖北省国际大学生程序设计竞赛的金牌。
《原神》中一个名为Dodoco的炸弹冒险的活动已经开始。这是一个单人游戏,每局游戏涉及一个池塘。池塘可以被划分为一个乘的网格,其中第行第列的单元格用表示。在这些单元格中,有个单元格包含鱼,而你将扮演用炸药钓鱼的火花骑士克莱。
如果克莱在处投下炸弹,所有满足条件的单元格将被爆炸覆盖。对于每个被爆炸覆盖的单元格,克莱将从中捕获一条鱼。克莱可以在任何位置投下炸弹。问题是,为了捕获所有的鱼,至少需要多少个Jumpy Dumpty炸弹?
/*
writer: Luna_Frome_StHhyy
Problem: Genshin Impact Startup Forbidden III
Knowledge: DFS + 状态记忆化
Time: O(4^k * k * 25)
PS:arr don't have dick
*/
/*
Note:
思路:
状态表示
因为每个鱼塘最多有 3条鱼,k个鱼塘的鱼数量组合可以用一个 k 维的状态数组 来表示。
用 arr[15] 存储当前每个鱼塘的剩余鱼数量,用 dp[4][4]...[4](共 k 个维度)存储对应状态下的最少炸弹数,初始值设为无穷大 0x3f3f3f3f。
cur 用来快速访问当前 arr 状态对应的 dp 值。
操作:
炸弹的覆盖范围是曼哈顿距离 ≤1的区域(即 3×3 十字区域)。
对于任意一个有鱼的鱼塘 c,枚举以它为中心的 5 个可能的炸弹投放点(自身 + 上下左右),每个投放点的覆盖范围会影响到其他鱼塘的鱼数量。
投放 剩余鱼数 枚炸弹,会让被覆盖的鱼塘的剩余鱼数 减少 剩余鱼数。
状态更新
如果当前 step 小于 cur,更新 cur 为 step,表示找到更优策略。
枚举所有可能的操作:遍历每个有鱼的鱼塘,枚举炸弹投放点,修改剩余鱼数量状态,递归搜索下一层,最后 回溯恢复状态。
初始状态与答案
初始状态 arr 存储输入的每个鱼塘的鱼数量,调用 dfs(0) 从炸弹数为 0 开始搜索。
最终答案是 dp[0][0]...[0],即所有鱼塘鱼数为 0 时的最少炸弹数。
*/
#include<bits/stdc++.h>
#define ll long long
#define sqr(a)((a) *(a))
#define un unsigned
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unordered_map
#define un_s unordered_set
using namespace std;
const int N = 20005;
const int M = 100005;
const int INF = 1e9;
const int mod = 998244353;
using point = struct ___{int x, y;};
using vn = struct __{int u, v;};
bool cmp(vn x, vn y){return false;}
std::mt19937 md(233);
const int P = 2147483647;
int seed = 233;
int Rand(){
return seed =(int)(seed * 482711LL % P);
}
int arr[15];
int dp[4][4][4][4][4][4][4][4][4][4];
#define now (dp[arr[0]][arr[1]][arr[2]][arr[3]][arr[4]][arr[5]][arr[6]][arr[7]][arr[8]][arr[9]])
int n, m, k;
PII pos[15];
int dir[5][2] = {
{0, 0},
{1, 0},
{-1, 0},
{0, 1},
{0, -1}
};
void dfs(int st){
if(st >= now){
return;
}
now = st;
vector<int> lst_(k);
for(int s = 0; s < k; s++){
if(arr[s]){
int cnt = arr[s];
for(int l = 0; l < 5; l++){
int x = pos[s].fi + dir[l][0],
y = pos[s].se + dir[l][1];
for(int i = 0; i < 5; i++){
int dx = x + dir[i][0],
dy = y + dir[i][1];
for(int j = 0; j < k; j++){
if(dx == pos[j].fi && dy == pos[j].se){
lst_[j] = arr[j];
arr[j] = max(0, arr[j] - cnt);
}
}
}
dfs(st + cnt);
for(int i = 0; i < 5; i++){
int dx = x + dir[i][0],
dy = y + dir[i][1];
for(int j = 0; j < k; j++){
if(dx == pos[j].fi && dy == pos[j].se){
arr[j] = lst_[j];
}
}
}
}
}
}
}
namespace work{
namespace lemon_tea{
namespace by_cmy{
namespace lemon{
void mango(){
cin >> n >> m >> k;
for(int i = 0; i < k; i++){
int x, y, z;
cin >> x >> y >> z;
pos[i] = {x, y};
arr[i] = z;
}
memset(dp, 0x3f, sizeof dp);
dfs(0);
cout << dp[0][0][0][0][0][0][0][0][0][0];
}
}
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int oT_To = 1;while(oT_To--){work::lemon_tea::by_cmy::lemon::mango();}
int a = 1, b = 1;
swap(a, b);
int op = Rand();
if(op % 2 == 0){
return 0;
}else{
if(1 + 1 == 3){
return 114514;
}else{
if(a != b){
cout << "WA吧";
}else{
return 0;
}
}
}
}