首页 > 其他 > 详细

POJ 1321

时间:2021-04-24 01:27:00      阅读:18      评论:0      收藏:0      [点我收藏+]

经典的八皇后问题的变种(可以称之为四车问题,手动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\n", DFS(0, 0, 0));
	}
}

POJ 1321

原文:https://www.cnblogs.com/Idi0t-N3/p/14695573.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!