洛谷 - P3786 - 萃香抱西瓜 - 状压dp
重构一下就过了,不知道之前错在哪里。
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int INF=0x3f3f3f3f;
int solve();
int main() {
#ifdef Yinku
freopen("Yinku.in","r",stdin);
#endif // Yinku
solve();
}
int dp[6][6][101][1<<11];
//dp[w][h][t][ch] 表示t时间xy处的接到西瓜状态为ch的最小步数
int g[6][6][101];
//g[w][h][t] 表示t时间xy处的西瓜有哪些
int h,w;
inline void update(int x,int y,int t,int ost,int nst) {
if(g[x][y][t+1]&(1<<0))
return;
if(x>1)
dp[x][y][t+1][nst]=min(dp[x][y][t+1][nst],dp[x-1][y][t][ost]+1);
if(x<w)
dp[x][y][t+1][nst]=min(dp[x][y][t+1][nst],dp[x+1][y][t][ost]+1);
if(y>1)
dp[x][y][t+1][nst]=min(dp[x][y][t+1][nst],dp[x][y-1][t][ost]+1);
if(y<h)
dp[x][y][t+1][nst]=min(dp[x][y][t+1][nst],dp[x][y+1][t][ost]+1);
return;
}
int solve() {
int T,sx,sy;
scanf("%d%d%d%d%d",&h,&w,&T,&sx,&sy);
int n,m;
scanf("%d%d",&n,&m);
memset(g,0,sizeof(g));
int id=1;
for(int i=1; i<=n; i++) {
int t1,t2,a;
scanf("%d%d%d",&t1,&t2,&a);
for(int t=t1; t<t2; t++) {
int x,y;
scanf("%d%d",&x,&y);
if(a==1) {
g[x][y][t]|=(1<<id);
} else {
g[x][y][t]|=(1<<0);
}
}
if(a==1)
id++;
}
/*for(int t=1; t<=T; t++) {
for(int nx=1; nx<=w; nx++) {
for(int ny=1; ny<=h; ny++) {
cout<<bitset<4>(g[nx][ny][t])<<" ";
}
cout<<endl;
}
cout<<endl;
}*/
if(g[sx][sy][1]&(1<<0)) {
printf("-1\n");
return 0;
}
memset(dp,INF,sizeof(dp));
int ch=g[sx][sy][1];
dp[sx][sy][1][ch]=0;
for(int t=1; t<T; t++) {
for(int nx=1; nx<=w; nx++) {
for(int ny=1; ny<=h; ny++) {
int nch=g[nx][ny][t+1];
if(nch&(1<<0))
continue;
for(int ch=0; ch<(1<<id); ch++) {
dp[nx][ny][t+1][ch]=min(dp[nx][ny][t+1][ch],dp[nx][ny][t][ch]);
}
}
}
for(int nx=1; nx<=w; nx++) {
for(int ny=1; ny<=h; ny++) {
int nch=g[nx][ny][t+1];
if(nch&(1<<0))
continue;
for(int ch=0; ch<(1<<id); ch++) {
update(nx,ny,t,ch,ch|nch);
}
}
}
}
/*for(int t=1; t<=T; t++) {
printf("t=%d\n",t);
for(int i=0; i<((1<<id)-1); i++) {
cout<<"i="<<bitset<10>(i)<<endl;
for(int nx=1; nx<=w; nx++) {
for(int ny=1; ny<=h; ny++) {
printf("%2d ",dp[nx][ny][t][i]!=INF?dp[nx][ny][t][i]:-1);
}
printf("\n");
}
cout<<endl;
}
printf("\n");
}*/
int ans=INF;
for(int nx=1; nx<=w; nx++) {
for(int ny=1; ny<=h; ny++) {
if(g[nx][ny][T]&(1<<0))
continue;
else {
ans=min(ans,dp[nx][ny][T][(1<<id)-2]);
}
}
}
if(ans==INF)
ans=-1;
printf("%d\n",ans);
return 0;
}
洛谷 - P3786 - 萃香抱西瓜 - 状压dp的更多相关文章
- [luogu P3786]萃香抱西瓜 [spfa][状态压缩]
题目背景 伊吹萃香(Ibuki Suika)正在魔法之森漫步,突然,许多西瓜(Suika)从四周飞来,划出了绚丽的轨迹.虽然阵势有点恐怖,但她还是决定抱走一些西瓜. 题目描述 萃香所处的环境被简化为一 ...
- 【题解】洛谷P2704 [NOI2001] 炮兵阵地(状压DP)
洛谷P2704:https://www.luogu.org/problemnew/show/P2704 思路 这道题一开始以为是什么基于状压的高端算法 没想到只是一道加了一行状态判断的状压DP而已 与 ...
- 【题解】洛谷P1896 [SCOI2005] 互不侵犯(状压DP)
洛谷P1896:https://www.luogu.org/problemnew/show/P1896 前言 这是一道状压DP的经典题 原来已经做过了 但是快要NOIP 复习一波 关于一些位运算的知识 ...
- 洛谷P1171 售货员的难题【状压DP】
题目描述 某乡有n个村庄(1 输入格式: 村庄数n和各村之间的路程(均是整数). 输出格式: 最短的路程. 输入样例: 3 0 2 1 1 0 2 2 1 0 输出样例 3 说明 输入解释 3 {村庄 ...
- 2018.07.18 洛谷P1171 售货员的难题(状压dp)
传送门 感觉是一道经典的状压dp,随便写了一发卡了卡常数开了个O(2)" role="presentation" style="position: relati ...
- 洛谷P2761 软件补丁问题(状压dp)
传送门 啊咧……这题不是网络流二十四题么……为啥是个状压dp…… 把每一个漏洞看成一个状态,直接硬上状压dp 然后因为有后效型,得用spfa //minamoto #include<iostre ...
- 洛谷$P3226\ [HNOI2012]$集合选数 状压$dp$
正解:$dp$ 解题报告: 传送门$QwQ$ 考虑列一个横坐标为比值为2的等比数列,纵坐标为比值为3的等比数列的表格.发现每个数要选就等价于它的上下左右不能选. 于是就是个状压$dp$板子了$QwQ$ ...
- 洛谷 P2622 关灯问题II【状压DP】
传送门:https://www.luogu.org/problemnew/show/P2622 题面: 题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯--按下了第i个按钮,对于所有的 ...
- UOJ #129 / BZOJ 4197 / 洛谷 P2150 - [NOI2015]寿司晚宴 (状压dp+数论+容斥)
题面传送门 题意: 你有一个集合 \(S={2,3,\dots,n}\) 你要选择两个集合 \(A\) 和 \(B\),满足: \(A \subseteq S\),\(B \subseteq S\), ...
随机推荐
- java 对账关键点
原理:双方交易信息对比是否平账 注意:对账bean必须重写 equals 方法 如图: //对账方法
- 从士兵到程序员再到SOHO程序员 (二)
原文地址: http://blog.huhao.name/blog/2013/12/13/become-a-freelancer-2/ 作者:胡皓 Blog:From Soldier to Progr ...
- 模式匹配之sift--- sift图像特征提取与匹配算法代码
sift,The Scale Invariant Feature Transform ,尺度不变特征变换,是检测图像中具有唯一性.对图像平移.旋转.缩放.甚至仿射变换(如从不同角度拍摄图片)保持不变性 ...
- 《MySQL必知必会学习笔记》:子查询
子查询 在開始了解子查询之前,首先做下准备工作,建立3个表, 一个是customers表,当中包含:客户名字.客户ID.客户Tel等. 一个是orders表,当中包含:订单号.客户ID.订单时间等. ...
- 51 NOD 1753 相似子串 字符串hash
1735 相似子串 基准时间限制:5 秒 空间限制:131072 KB 分值: 80 两个字符串相似定义为:1.两个字符串长度相等2.两个字符串对应位置上有且仅有至多一个位置所对应的字符不 ...
- Message-oriented middleware
en.wikipedia.org/wiki/Message-oriented_middleware Message-oriented middleware (MOM) is software or h ...
- 【NOIP 模拟赛】 道路
题目描述在二维坐标平面里有 N 个整数点,信息班某一巨佬要访问这 N 个点.刚开始巨佬在点(0,0)处. 每一步,巨佬可以走到上.下.左.右四个点.即假设巨佬当前所在点的坐标是(x,y),那么它下一步 ...
- ansible 文件模块,很实用
摘自: http://blog.csdn.net/kellyseeme/article/details/50545521
- hadoop运行测试命令遇到的问题
2017-02-16 09:46:14,926 INFO mapreduce.Job: Task Id : attempt_1487148856575_0001_m_000001_0, Status ...
- c++变量定义
float **a 表示a是一个“指针的指针”,也可以理解为是一个二维数组的指针,***a具有类似的解释,可以理解为是一个三维数组的指针.