方格取数(1)

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8851    Accepted Submission(s): 3386

Problem Description
给你一个n*n的格子的棋盘,每个格子里面有一个非负数。
从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数的和最大。
 
Input
包括多个测试实例,每个测试实例包括一个整数n 和n*n个非负数(n<=20)
 
Output
对于每个测试实例,输出可能取得的最大的和
 
Sample Input
3
75 15 21
75 15 28
34 70 5
 
Sample Output
188
 
Author
ailyanlu
 
Source
 
Recommend
前面的骨牌堆放由于数据不是很大用暴力状压就过去了这个题目如果还是O(2^n*2^n*m)
得话复杂度显然爆炸,所以采用轮廓线dp的思想,O(n*m*2^(n))
对于每个格子影响他的只有四周相邻的格子,我们从左至右,由上而下递推得话,对于正在处理的格子,影响他的最多只有左边相邻和上方相邻的格子,
我们不妨以n为轮廓线长度,对于每个格子有两种操作,取or不取,不取得话可由任意状态得到,取得话只要左方&&上方状态为0表示不取即可
对于当前处理的格子我们枚举上个轮廓线的状态看是否能转移到当前轮廓线,如果可以,update即可。
最后遍历一遍得到答案。
之所以复杂度变低是采取了我为人人的方法,少遍历一遍2^n,很巧妙的思想,我会把骨牌堆放以这种方法再做一次。
#include<bits/stdc++.h>
using namespace std;
int
ans;
int
dp[][(<<)+];
bool
judge(int a,int b){return (a&(<<b));}
int
clr(int a,int b){ if(judge(a,b)){return (a^(<<b));}
else return
a;}
void
update(int cur,int now,int pre,int num)
{

dp[cur][now]=max(dp[cur][now],dp[-cur][pre]+num); //我为人人
}

int
main()
{

int
e[][],i,j,k,s,t,n;
while
(cin>>n){int cur=;
for
(i=;i<n;++i)
for
(j=;j<n;++j)
cin>>e[i][j];
memset(dp,,sizeof(dp));
for
(i=;i<n;++i){
for
(j=;j<n;++j){
cur^=;memset(dp[cur],,sizeof(dp[cur]));
for
(k=;k<(<<n);++k){
update(cur,clr(k<<,n),k,);
if
(!j){
if
(!judge(k,n-)){
update(cur,clr((k<<)+,n),k,e[i][j]);
}
}

else
{
if
((!judge(k,))&&(!judge(k,n-))){
update(cur,clr((k<<)+,n),k,e[i][j]);
}
}
}
}
}

ans=;
for
(i=;i<(<<n);++i) ans=max(ans,dp[cur][i]);
cout<<ans<<endl;
}

return
;
}
 

 

 



 

Given a rectangular grid, with dimensions

m


n

, compute the number of ways of completely tiling it

with dominoes. Note that if the rotation of one tiling matches another, they still count as different

ones. A domino is a shape formed by the union of two unit squares meeting edge-to-edge. Equivalently,

it is a matching in the grid graph formed by placing a vertex at the center of each square of the region

and connecting two vertices when they correspond to adjacent squares. An example of a tiling is shown

below.

Input

The input will consist of a set of lines with

mn

, given the restriction

n


m<

101.

Output

For each line of input, output the number of tilings in a separate line.

Sample Input

2 10

4 10

8 8

Sample Output

89

18061

12988816

 
题目不再赘述就是骨牌放置问题,这里采用轮廓线dp
要注意的就是横放时列数不能为1,竖放时行不能为1,md最近一直犯zz错误,cout ans数组我一直cout dp数组检查半天mb
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL dp[2][(1<<11)];
LL ans[101][101];
bool judge(int a,int b){return (a&(1<<b));}
int clr(int a,int b){return (a>>b)?(a^(1<<b)):a;}
void update(int cur,int now,int pre) { dp[cur][now]+=dp[1-cur][pre]; }
int main()
{
int i,j,k,n,m,s,t;
memset(ans,-1,sizeof(ans));
while(cin>>n>>m){int cur=0;
memset(dp,0,sizeof(dp));
if(n*m%2==1) {puts("0");continue;}
if(ans[n][m]+1) {cout<<ans[n][m]<<endl;continue;}
if(m>n) swap(m,n);
dp[cur][(1<<m)-1]=1;
for(i=1;i<=n;++i){
for(j=1;j<=m;++j){cur^=1;memset(dp[cur],0,sizeof(dp[cur]));
for(k=0;k<(1<<m);++k){
if(judge(k,m-1)) update(cur,clr((k<<1),m),k); //不放
if(i>1&&!judge(k,m-1)) update(cur,(k<<1)^1,k); //竖放
if(j>1&&(!judge(k,0))&&judge(k,m-1)) update(cur,clr((k<<1)^3,m),k); //横放
}
}
}
ans[n][m]=ans[m][n]=dp[cur][(1<<m)-1];
cout<<ans[n][m]<<endl;
}
return 0;
}


HDU1565 方格取数 &&uva 11270 轮廓线DP的更多相关文章

  1. HDU1565 方格取数(1) —— 状压DP or 插头DP(轮廓线更新) or 二分图点带权最大独立集(最小割最大流)

    题目链接:https://vjudge.net/problem/HDU-1565 方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory L ...

  2. HDU1565 方格取数(1)(状态压缩dp)

    题目链接. 分析: 说这题是状态压缩dp,其实不是,怎么说呢,题目数据太水了,所以就过了.手动输入n=20的情况,超时.正解是网络流,不太会. A这题时有个细节错了,是dp[i][j]还是dp[i][ ...

  3. Hdu-1565 方格取数(1) (状态压缩dp入门题

    方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  4. HDU 1565 - 方格取数(1) - [状压DP][网络流 - 最大点权独立集和最小点权覆盖集]

    题目链接:https://cn.vjudge.net/problem/HDU-1565 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32 ...

  5. HDU-1565 方格取数(1)

    http://acm.hdu.edu.cn/showproblem.php?pid=1565 方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Me ...

  6. hdu 1565 方格取数(1) 状态压缩dp

    方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. hdu 2167 方格取数 【状压dp】(经典)

    <题目链接> 题目大意: 给出一些数字组成的n*n阶矩阵,这些数字都在[10,99]内,并且这个矩阵的  3<=n<=15,从这个矩阵中随机取出一些数字,在取完某个数字后,该数 ...

  8. UVA - 11270 轮廓线DP

    其实这题还能用状压DP解决,可是时间达到2000ms只能过掉POJ2411.状压DP解法详见状压DP解POJ2411 贴上POJ2411AC代码 : 2000ms 时间复杂度h*w*(2^w)*(2^ ...

  9. TYVJ 1011 NOIP 2008&&NOIP 2000 传纸条&&方格取数 Label:多线程dp

    做题记录:2016-08-15 15:47:07 背景 NOIP2008复赛提高组第三题 描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行 ...

随机推荐

  1. CodeForces 19D Points (线段树+set)

    D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  2. TFIDF练习

    直接上代码吧: """ 测试Demo """ import lightgbm as lgb import numpy as np from ...

  3. Oracle HA 之 OGG部署流水

    1.GG组件及其功能简介:    manager进程:总管其他所以进程及处理相应GGSCI命令.    capture进程:从源端的联机日志文件或归档日志文件抓取commit的信息.    sourc ...

  4. Java的平台无关性如何体现出来的

    传统的编程中,源代码编译为可执行的代码后,只能针对特定的平台(操作系统),换句话说,针对Windows编写和编译的代码,只能在Windows上运行... java程序则编译为字节码.字节码本身不能运行 ...

  5. 对django框架架构和request/response处理流程的分析

    一. 处理过程的核心概念 如下图所示django的总览图,整体上把握以下django的组成: 核心在于中间件middleware,django所有的请求.返回都由中间件来完成. 中间件,就是处理HTT ...

  6. django-session和cookie

    在Django里面,使用Cookie和Session看起来好像是一样的,使用的方式都是request.COOKIES[XXX]和request.session[XXX],其中XXX是您想要取得的东西的 ...

  7. Nginx 使用总结

    一.使用 nginx 实现 灰度发布 灰度发布,现在是很多大项目的一个标配运维特性,我们可以将一个“新的版本代码”发布到集群中的少数几台(组)机器上,以便引入线上少量真实用 户进行测试,用于验证产品改 ...

  8. Jenkins的持续集成

    持续集成:不需要人工干预,持久化.重复的运行一个任务.将代码自动的更新到最新,然后自动运行. 新建项目之前要再Jenkins的全局工具配置里面把git的路径设置好.[全局工具配置]-->[Git ...

  9. MR的shuffle和Spark的shuffle之间的区别

    mr的shuffle mapShuffle 数据存到hdfs中是以块进行存储的,每一个块对应一个分片,maptask就是从分片中获取数据的 在某个节点上启动了map Task,map Task读取是通 ...

  10. jq的$(function(){})与window.onload的区别

    最近一直在研究jq的源码,书写jq的代码我们通常会包裹在一个$(function(){})函数中,jq的$(function(){})也就是$(document).ready(function(){} ...