poj 2411 新写法
别以为我在刷水题。。。。
今天做了场srm,500pt想到了是dp但是无从下手,但是看了rng_58的神代码后顿觉海阔天空啊(盯着看了一个下午),相比于一年前的写法,真的是不忍直视啊,
TC真是个好地方。。。赞!
其实就是将普通的铺砖块问题用类似于插头dp逐格递推的思路来写。下面的代码相信大家应该都能看懂。
#include <cstdio>
#include <cstring>
#include <algorithm>
long long dp[2][1<<11];
int main() {
int n , m;
while(scanf("%d%d",&n,&m),n||m) {
int cur = 0 , nxt = 1;
dp[cur][0] = 1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
memset(dp[nxt],0,sizeof(dp[nxt]));
for(int s = 0; s < (1<<m); s++) if(dp[cur][s]){
if(s&1){dp[nxt][s>>1] += dp[cur][s];continue;}
if(j+1<m && !(s&2) ){
int mask = ( s | 2 ) >> 1;
dp[nxt][mask] += dp[cur][s];
}
if(i+1<n) {
int mask = (s | (1<<m)) >> 1;
dp[nxt][mask] += dp[cur][s];
}
}
std::swap(cur,nxt);
}
}
printf("%I64d\n",dp[cur][0]);
}
return 0;
}
下面是一年前的写法。。。
#include<stdio.h>
#include<string.h>
int h,w;
__int64 dp[15][2050];
void dfs1(int row,int state,int col,int state2){
if(col>w) return ;
if(col==w) {
dp[row+1][state2]+=dp[row][state];
return ;
}
if(!(state&(1<<col))) dfs1(row,state,col+1,state2+(1<<col));
else dfs1(row,state,col+1,state2);
}
void dfs2(int row,int state,int col,int state2){
if(col>w) return ;
if(col==w) {
if(state2!=state)
dp[row][state2]+=dp[row][state];
return ;
}
if(!(state&(1<<col)) && !(state&(1<<(col+1))))
dfs2(row,state,col+2,state2+(1<<col) + (1<<(col+1)));
dfs2(row,state,col+1,state2);
}
void gao(){
int i,j;
dp[0][(1<<w)-1]=1;
for(i=0;i<h;i++){
for(j=0;j<(1<<w);j++)
if(dp[i][j])
dfs1(i,j,0,0);
for(j=(1<<w)-1;j>=0;j--)
if(dp[i+1][j])
dfs2(i+1,j,0,j);
}
}
int main(){
while(scanf("%d%d",&h,&w)!=EOF && h+w){
int temp;
if(h<w){
temp=h;h=w;w=temp;
}
memset(dp,0,sizeof(dp));
gao();
printf("%I64d\n",dp[h][(1<<w)-1]);
}
return 0;
}
poj 2411 新写法的更多相关文章
- poj 1703 Find them, Catch them 【并查集 新写法的思路】
题目地址:http://poj.org/problem?id=1703 Sample Input 1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4 Sample Output N ...
- poj 2411 Mondriaan's Dream 【dp】
题目:id=2411" target="_blank">poj 2411 Mondriaan's Dream 题意:给出一个n*m的矩阵,让你用1*2的矩阵铺满,然 ...
- 《精通Matlab神经网络》例10-16的新写法
<精通Matlab神经网络>书中示例10-16,在创建BP网络时,原来的写法是: net = newff(minmax(alphabet),[S1 S2],{'logsig' 'logsi ...
- 状压DP POJ 2411 Mondriaan'sDream
题目传送门 /* 题意:一个h*w的矩阵(1<=h,w<=11),只能放1*2的模块,问完全覆盖的不同放发有多少种? 状态压缩DP第一道:dp[i][j] 代表第i行的j状态下的种数(状态 ...
- [BS-02] iOS数组、字典、NSNumber 新写法—— @[]、@{}
IOS数组.字典.NSNumber 新写法—— @[].@{} //标准写法 NSNumber * number = [NSNumber numberWithInt:]; NSArray * ar ...
- Poj 2411 Mondriaan's Dream(压缩矩阵DP)
一.Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, ...
- Mondriaan's Dream POJ - 2411
Mondriaan's Dream POJ - 2411 可以用状压dp,但是要打一下表.暴力枚举行.这一行的状态.上一行的状态,判断如果上一行的状态能转移到这一行的状态就转移. 状态定义:ans[i ...
- poj 2411 Mondriaan's Dream(状态压缩dP)
题目:http://poj.org/problem?id=2411 Input The input contains several test cases. Each test case is mad ...
- poj 2411 Mondriaan's Dream 轮廓线dp
题目链接: http://poj.org/problem?id=2411 题目意思: 给一个n*m的矩形区域,将1*2和2*1的小矩形填满方格,问一共有多少种填法. 解题思路: 用轮廓线可以过. 对每 ...
随机推荐
- UVa232.Crossword Answers
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- ubuntu 16.04 一些使用过程中遇到的问题
1 安装ssh 和 openssh-server之后通过SecureCRT 可以连接,FileZilla不能使用sftp方式进行连接, 安装vsftpd后测试ftp可以连接, 修改 /etc/ssh ...
- add.fun.php
<?php header("Content-type: text/html; charset=utf-8"); function add($min_int,$max_int) ...
- 开机后将sim/uim卡上的联系人写入数据库
tyle="margin:20px 0px 0px; font-size:14px; line-height:26px; font-family:Arial; color:rgb(51,51 ...
- Chrome开发者工具详解(1):Elements、Console、Sources面板
Chrome开发者工具面板 面板上包含了Elements面板.Console面板.Sources面板.Network面板. Timeline面板.Profiles面板.Application面板.Se ...
- html进阶css(1)
<!doctype html> <html> <head> <meta http-equiv="content-type" content ...
- oracle 两个时间相减
oracle 两个时间相减默认的是天数 oracle 两个时间相减默认的是天数*24 为相差的小时数 oracle 两个时间相减默认的是天数*24*60 为相差的分钟数 oracle 两个时间相减默认 ...
- poj1111 DFS
J - 搜索 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bit I ...
- latex 模版
texlive2012 通过 \documentclass[a4paper]{article} %\documentclass[twocolumn]{article} %\usepackage{g ...
- shell中的退出状态码
shell中的退出状态码最大只有255,如果超过这个值,就会进行取余运算,即如果执行如下命令: exit exitCode 如果exitCode大于255,那么实际的状态码为exitCode % 25 ...