HDU5838 Mountain(状压DP + 容斥原理)
题目
Source
http://acm.hdu.edu.cn/showproblem.php?pid=5838
Description
Zhu found a map which is a N∗M rectangular grid.Each cell has a height and there are no two cells which have the same height. But this map is too old to get the clear information,so Zhu only knows cells which are valleys.
A cell is called valley only if its height is less than the heights of all its adjacent cells.If two cells share a side or a corner they are called adjacent.And one cell will have eight adjacent cells at most.
Now give you N strings,and each string will contain M characters.Each character will be '.' or uppercase 'X'.The j-th character of the i-th string is 'X' if the j-th cell of the i-th row in the mountain map is a valley, and '.' otherwise.Zhu wants you to calculate the number of distinct mountain maps that match these strings.
To make this problem easier,Zhu defines that the heights are integers between 1 and N∗M.Please output the result modulo 772002.
Input
The input consists of multiple test cases.
Each test case begins with a line containing two non-negative integers N and M. Then N lines follow, each contains a string which contains M characters. (1≤N≤5,1≤M≤5).
Output
For each test case, output a single line "Case #x: y", where x is the case number, starting from 1. And y is the answer after module 772002.
Sample Input
2 4
.X..
...X
4 2
X.
..
..
X.
1 2
XX
Sample Output
Case #1: 2100
Case #2: 2520
Case #3: 0
分析
题目大概说给一张大小n*m由'X'或'.'标记的图,要在其各个位置填入1到n*m这几个数,满足'X'填入的数是极小的(小于周围八个数)且'.'填入的数不是极小的,求有几种填法。
好难的感觉。。
考虑把数从小到大依次填入:
- dp[i][S]表示当前已经填入数字1...i,且已经被填数的'X'集合为S的方案数(因为合法的'X'最多有9个所以用9位二进制压缩表示S即可)
通过将i+1填入转移到dp[i+1]的状态:
- 可以选择把i+1填入下一个还没被填的'X'位置x
- 也可以填入'.'的位置,这个点'.'的位置不能是未填的'X'周围,因为是从小到大填数的
这样子得到的方案数得到的不是答案要的方案数,因为它还包含了'.'填入的数是极小的,这个用容斥原理搞一下,具体就是用一个dfs搜出所有合法的'.'变为'X'的情况,奇数个减,偶数个加。。
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int n,m; int dx[]={0,0,1,-1,1,-1,1,-1};
int dy[]={1,-1,0,0,1,-1,-1,1};
char map[5][5]; long long d[26][1<<9];
int pos[1<<9],siz[1<<9]; long long dp(){
int cnt=0,x[9],y[9];
for(int i=0; i<n; ++i){
for(int j=0; j<m; ++j){
if(map[i][j]=='X'){
x[cnt]=i; y[cnt]=j;
++cnt;
}
}
}
memset(siz,0,sizeof(siz));
for(int s=0; s<(1<<cnt); ++s){
for(int i=0; i<cnt; ++i){
if(s>>i&1){
map[x[i]][y[i]]='Y';
++siz[s];
}
}
int res=0;
for(int i=0; i<n; ++i){
for(int j=0; j<m; ++j){
if(map[i][j]!='.') continue;
bool flag=1;
for(int k=0; k<8; ++k){
int nx=i+dx[k],ny=j+dy[k];
if(nx<0 || nx>=n || ny<0 ||ny>=m) continue;
if(map[nx][ny]=='X'){
flag=0;
break;
}
}
if(flag) ++res;
}
}
pos[s]=res;
for(int i=0; i<cnt; ++i){
if(s>>i&1){
map[x[i]][y[i]]='X';
}
}
} memset(d,0,sizeof(d));
d[0][0]=1;
for(int i=0; i<n*m; ++i){
for(int j=0; j<(1<<cnt); ++j){
if(d[i][j]==0) continue;
for(int k=0; k<cnt; ++k){
if(j>>k&1) continue;
d[i+1][j^(1<<k)]+=d[i][j];
d[i+1][j^(1<<k)]%=772002;
}
d[i+1][j]+=d[i][j]*(pos[j]-i+siz[j]);
d[i+1][j]%=772002;
}
}
return d[n*m][(1<<cnt)-1];
} long long ans;
void dfs(int z,int k){
if(z==n*m){
if(k&1) ans-=dp();
else ans+=dp();
ans%=772002;
return;
}
int x=z/m,y=z%m;
bool flag=(map[x][y]!='X');
for(int i=0; i<8; ++i){
int nx=x+dx[i],ny=y+dy[i];
if(nx<0 || nx>=n || ny<0 || ny>=m) continue;
if(map[nx][ny]=='X'){
flag=0;
break;
}
}
if(flag){
map[x][y]='X';
dfs(z+1,k+1);
map[x][y]='.';
}
dfs(z+1,k);
} bool check(){
for(int i=0; i<n; ++i){
for(int j=0; j<m; ++j){
if(map[i][j]=='.') continue;
for(int k=0; k<8; ++k){
int nx=i+dx[k],ny=j+dy[k];
if(nx<0 || nx>=n || ny<0 || ny>=m) continue;
if(map[nx][ny]=='X') return 0;
}
}
}
return 1;
} int main(){
int cse=0;
while(~scanf("%d%d",&n,&m)){
for(int i=0; i<n; ++i){
for(int j=0; j<m; ++j){
scanf(" %c",&map[i][j]);
}
}
if(!check()){
printf("Case #%d: 0\n",++cse);
continue;
}
ans=0;
dfs(0,0);
if(ans<0) ans+=772002;
printf("Case #%d: %lld\n",++cse,ans);
}
return 0;
}
HDU5838 Mountain(状压DP + 容斥原理)的更多相关文章
- 【uoj#37/bzoj3812】[清华集训2014]主旋律 状压dp+容斥原理
题目描述 求一张有向图的强连通生成子图的数目对 $10^9+7$ 取模的结果. 题解 状压dp+容斥原理 设 $f[i]$ 表示点集 $i$ 强连通生成子图的数目,容易想到使用总方案数 $2^{sum ...
- 【bzoj2560】串珠子 状压dp+容斥原理
题目描述 有 $n$ 个点,点 $i$ 和点 $j$ 之间可以连 $0\sim c_{i,j}$ 条无向边.求连成一张无向连通图的方案数模 $10^9+7$ .两个方案不同,当且仅当:存在点对 $(i ...
- BZOJ2669 [cqoi2012]局部极小值 状压DP 容斥原理
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2669 题意概括 有一个n行m列的整数矩阵,其中1到nm之间的每个整数恰好出现一次.如果一个格子比所 ...
- 4455: [Zjoi2016]小星星|状压DP|容斥原理
OrzSDOIR1ak的晨神 能够考虑状压DP枚举子集,求出仅仅保证连通性不保证一一相应的状态下的方案数,然后容斥一下就是终于的答案 #include<algorithm> #includ ...
- 【BZOJ 2669】 2669: [cqoi2012]局部极小值 (状压DP+容斥原理)
2669: [cqoi2012]局部极小值 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 667 Solved: 350 Description 有一 ...
- 【BZOJ-2669】局部极小值 状压DP + 容斥原理
2669: [cqoi2012]局部极小值 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 561 Solved: 293[Submit][Status ...
- BZOJ 2669 CQOI2012 局部极小值 状压dp+容斥原理
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2669 题意概述:实际上原题意很简洁了我就不写了吧.... 二话不说先观察一下性质,首先棋盘 ...
- BZOJ3812 主旋律(状压dp+容斥原理)
设f[S]为S点集是SCC的方案数.考虑通过去掉不合法方案转移.可以枚举入度为0的SCC所含点集S',这样显然S^S'内部的边和由S'连向S^S'的边删还是不删任选.但是这样无法保证S'包含所有入度为 ...
- [BZOJ3812]主旋律:状压DP+容斥原理
分析 Miskcoo orz 令\(f[S]\)表示使得\(S\)这个点集强连通的方案数. 然后呢?不会了 考虑到将一个有向图SCC缩点后,得到的新图是一个DAG,所以我们可以类比带标号DAG计数的解 ...
随机推荐
- D3.js学习(二)
上一节中我们已经画出了一个基本的图表,不过忘了给坐标轴添加标签了,所以在本节中我们要给坐标轴加上标签,目标效果如下 给X轴添加标签 很明显,标签是不是一个text内容块啊,所以我们只要在svg中添加一 ...
- mysql 编译安装
mysql 编译安装方式: ```cd /home/oldboy/tools``` 创建 目录 if not have then mkd ...
- Python 学习小结
python 学习小结 python 简明教程 1.python 文件 #!/etc/bin/python #coding=utf-8 2.main()函数 if __name__ == '__mai ...
- 重写官方TodoList,对于初学react+redux的人来说,很有好处
虽然官网的TodoList的例子写的很详细,但是都是一步到位,就是给你一个action,好家伙,全部都写好了,给你一个reducer,所有功能也是都写好了,但是我们这些小白怎么可能一下就消化那么多,那 ...
- Unity3D配合AndroidStudio打包
SET UNITY_PATH="C:\Program Files\Unity\Editor\Unity.exe" echo UNITY_PATH=%UNITY_PATH% SET ...
- plist文件里边如果最外层是字典的话,读出来是无序的。
如题. 要想使字典有序的话,可以用数组来存放字典,然后读
- 创建/发布cocoapods公共库
对于大多数iOS开发者而言,cocoapods都是一个非常便捷的第三方库引导工具,该工具可以帮助我们快速导入所需第三方库,并且进行相关配置. 本文即为描述如何发布一个第三方库,提供给所有的开发者使用. ...
- 数据库SQL语句优化小结
网上查找的总结: 1.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如: select id from t where num is null ...
- 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优
libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...
- Servlet引擎tomcat之安装
原文来自:https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-ubuntu-14-04 ...