POJ 1321 经典棋盘问题 的搜索和状态压缩解法

2024-08-22 09:58

本文主要是介绍POJ 1321 经典棋盘问题 的搜索和状态压缩解法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这就是 n 皇后问题,不过简单了点,对于对角线不做限制

搜索,因为只有 8!个状态,直接数就可以了

状态压缩dp,每一行用一排1010代表上面有没有放过旗子,1是放过,0是没有,这一个行的状态可以是完全来自以前或者在这一行合法的地方放一个棋子。

每行有 2^n 种状态,共 n 行,每次转移要 n,总的就是 2^n*n*n;

两种代码 

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>using namespace std;
#define MAX 1000009 
#define INF 0x3f3f3f3f
#define MS(x) memset(x,0,sizeof(x))
#define ll long long
#define P pair<int,int>
#define fst first
#define sec secondint ans;
char b[20][20];
int n;
void dfs(int r,int state,int k)
{if(!k){ans++;return ;}if(r>=n)return ;dfs(r+1,state,k);for(int i=0;i<n;i++){if(((1<<i)&state)==0&&b[r][i]=='#'){dfs(r+1,(1<<i)|state,k-1);}}
}int main()
{int k;while(scanf("%d%d",&n,&k)!=EOF&&n!=-1){ans=0;for(int i=0;i<n;i++)scanf("%s",b[i]);dfs(0,0,k);cout<<ans<<endl;}return 0;
}

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>using namespace std;
#define MAX 1000009 
#define INF 0x3f3f3f3f
#define MS(x) memset(x,0,sizeof(x))
#define ll long long
#define P pair<int,int>
#define fst first
#define sec secondint dp[10][1000];
char b[11][11];
int cnt[2000];
int n,k;
int check(int n)
{int ans=0;while(n){if(n%2)ans++;n/=2;}return ans;
}
int main()
{for(int i=0;i<1000;i++)cnt[i]=check(i);while(scanf("%d%d",&n,&k)&&n!=-1){MS(dp);for(int i=1;i<=n;i++)scanf("%s",b[i]+1);dp[0][0]=1;for(int i=1;i<=n;i++){for(int j=0;j<(1<<n);j++){if(cnt[j]<=k){dp[i][j]+=dp[i-1][j];for(int p=1;p<=n;p++){if( ((1<<p-1)&j) && b[i][p]=='#'){dp[i][j]+=dp[i-1][(~(1<<p-1))&j];}}}	}	}int ans=0;for(int i=0;i<(1<<n);i++)if(cnt[i]==k)ans+=dp[n][i];cout<<ans<<endl;}return 0;
}


这篇关于POJ 1321 经典棋盘问题 的搜索和状态压缩解法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1095951

相关文章

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

hdu1240、hdu1253(三维搜索题)

1、从后往前输入,(x,y,z); 2、从下往上输入,(y , z, x); 3、从左往右输入,(z,x,y); hdu1240代码如下: #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#inc

hdu1565(状态压缩)

本人第一道ac的状态压缩dp,这题的数据非常水,很容易过 题意:在n*n的矩阵中选数字使得不存在任意两个数字相邻,求最大值 解题思路: 一、因为在1<<20中有很多状态是无效的,所以第一步是选择有效状态,存到cnt[]数组中 二、dp[i][j]表示到第i行的状态cnt[j]所能得到的最大值,状态转移方程dp[i][j] = max(dp[i][j],dp[i-1][k]) ,其中k满足c

购买磨轮平衡机时应该注意什么问题和技巧

在购买磨轮平衡机时,您应该注意以下几个关键点: 平衡精度 平衡精度是衡量平衡机性能的核心指标,直接影响到不平衡量的检测与校准的准确性,从而决定磨轮的振动和噪声水平。高精度的平衡机能显著减少振动和噪声,提高磨削加工的精度。 转速范围 宽广的转速范围意味着平衡机能够处理更多种类的磨轮,适应不同的工作条件和规格要求。 振动监测能力 振动监测能力是评估平衡机性能的重要因素。通过传感器实时监

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int