zoukankan      html  css  js  c++  java
  • POJ 1321

    经典的八皇后问题的变种(可以称之为四车问题,手动doge),并利用状态压缩帮助优化,不过开始思路方向错了,或许说不合适更好些,状态记录的是可以防止棋子的地方的状态,但是这样即使状态压缩以后,相关记录数组也是大的不可接受(OJ上一直RUNTIEMERROR)

    后来浏览解决状压数组太大问题,概览发现用了一个非常取巧的DP: dp(row, col, depth)记录为row行及以下,并且此前列状态为col,已经放置棋子数为depth(这个变量名可能不太合适了)

    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <string>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <stack>
    #include <map>
    #include <set>
    using namespace std;
    
    typedef unsigned int uint;
    const int maxn= 11;
    const int maxv= maxn*maxn;
    const int maxs= (1<<8)+5;
    
    char bd[maxn][maxn];
    int dv[maxn][maxs][maxn];
    int n, k;
    int tot;
    
    uint DFS(const int row, const int col, const int depth)
    {
    	if (depth== k){
    		return 1;
    	}
    	if (row> n){
    		return 0;
    	}
    	if (~dv[row][col][depth]){
    		return dv[row][col][depth];
    	}
    	uint ans= 0;
    	for (int i= 1; i<= n; ++i){
    		if ('#'== bd[row][i]){
    			if (col & (1<<i)){
    				continue;
    			}
    			ans+= DFS(row+1, col|(1<<i), depth+1);
    		}	
    	}
    	ans+= DFS(row+1, col, depth);
    
    	return dv[row][col][depth]= ans;
    }
    
    int main()
    {
    	while (~scanf("%d %d", &n, &k) && ~n){
    		tot= 0;
    		for (int i= 1; i<= n; ++i){
    			scanf(" %s", bd[i]+1);
    		}
    		memset(dv, -1, sizeof(dv));
    		printf("%u
    ", DFS(0, 0, 0));
    	}
    }
    
  • 相关阅读:
    git代码提交
    bootstrap的用法、bootstrap图标
    HTML 5 Web 存储(客户端存储数据)
    require.js
    WebStrom的使用技巧
    event事件
    $().each 和 $each( )的区别
    js基础字符串
    if return的用法 逻辑运算 switch for break等用法
    date-id自定义属性
  • 原文地址:https://www.cnblogs.com/Idi0t-N3/p/14695573.html
Copyright © 2011-2022 走看看