zoukankan      html  css  js  c++  java
  • Mondriaan's Dream POJ

    用2进制的01表示不放还是放
    第i行只和i-1行有关
    枚举i-1行的每个状态,推出由此状态能达到的i行状态
    如果i-1行的出发状态某处未放,必然要在i行放一个竖的方块,所以我对上一行状态按位取反之后的状态就是放置了竖方块的状态。
    然后用搜索扫一道在i行放横着的方块的所有可能,并且把这些状态累加上i-1的出发状态的方法数,如果该方法数为0,直接continue。
    举个例子
    2 4
    1111
    1111
    状态可以由
    1100 0000 0110 0011 1111
    0000 0000 0000 0000 0000
    这五种i-1的状态达到,故2 4 的答案为5
    代码:
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<iostream>
    using namespace std;
    #define maxn 12
    long long dp[2][1 <<maxn];
    int h, w;
    
    
    int main() {
    	while (scanf("%d %d", &h, &w) != EOF&&h) {
    		memset(dp, 0, sizeof(dp));
    		long long *now = dp[0], *next = dp[1];
    		now[0] = 1;
    
    
    		for (int i = h - 1; i >= 0; i--) {
    			for (int j = w - 1; j >= 0; j--) {
    				for (int s = 0; s < 1 << w; s++) {
    					if (s >> j & 1) {				//不用在(i,j)铺砖
    						next[s] = now[s&~(1 << j)];
    					}
    					else {
    						long long cnt = 0;
    						//横着放
    						if (j + 1 < w && !(s >> (j + 1) & 1)) {
    							cnt += now[s | 1 << (j + 1)];
    						}
    						//竖着放
    						if (i + 1 < h) {
    							cnt += now[s | 1 << j];
    						}
    						next[s] = cnt;
    					}
    				}
    				swap(now, next);
    			}
    		}
    		printf("%lld
    ", now[0]);
    	}
    }
    
    一开始万般纠结于横放竖放的代码那里。。
    想了很久才明白j不代表坐标。。所以位操作控制的是同一个位
    记住顶端的才是需要考虑的。。一开始白书上那图还觉得迷,总算看懂了
    今儿不熬夜了吧,快开学了调一下生物钟
  • 相关阅读:
    调用其他类的方法
    CString中 format、trimLeft和trimright、trim 和FindOneOf用法
    GetAsyncKeyState()& 0x8000
    C++打开剪切板,获取剪切板数据
    CString比较不区分大小写
    C++ string中find() 用法
    CString数组和CStringArray
    nested exception is java.io.FileNotFoundException: Could not open ServletContext resource
    SQLPlus获取oracle表操作SQL
    XShell实现服务器端文件的上传下载
  • 原文地址:https://www.cnblogs.com/Drenight/p/8611332.html
Copyright © 2011-2022 走看看