题目

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 + 容斥原理)的更多相关文章

  1. 【uoj#37/bzoj3812】[清华集训2014]主旋律 状压dp+容斥原理

    题目描述 求一张有向图的强连通生成子图的数目对 $10^9+7$ 取模的结果. 题解 状压dp+容斥原理 设 $f[i]$ 表示点集 $i$ 强连通生成子图的数目,容易想到使用总方案数 $2^{sum ...

  2. 【bzoj2560】串珠子 状压dp+容斥原理

    题目描述 有 $n$ 个点,点 $i$ 和点 $j$ 之间可以连 $0\sim c_{i,j}$ 条无向边.求连成一张无向连通图的方案数模 $10^9+7$ .两个方案不同,当且仅当:存在点对 $(i ...

  3. BZOJ2669 [cqoi2012]局部极小值 状压DP 容斥原理

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2669 题意概括 有一个n行m列的整数矩阵,其中1到nm之间的每个整数恰好出现一次.如果一个格子比所 ...

  4. 4455: [Zjoi2016]小星星|状压DP|容斥原理

    OrzSDOIR1ak的晨神 能够考虑状压DP枚举子集,求出仅仅保证连通性不保证一一相应的状态下的方案数,然后容斥一下就是终于的答案 #include<algorithm> #includ ...

  5. 【BZOJ 2669】 2669: [cqoi2012]局部极小值 (状压DP+容斥原理)

    2669: [cqoi2012]局部极小值 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 667  Solved: 350 Description 有一 ...

  6. 【BZOJ-2669】局部极小值 状压DP + 容斥原理

    2669: [cqoi2012]局部极小值 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 561  Solved: 293[Submit][Status ...

  7. BZOJ 2669 CQOI2012 局部极小值 状压dp+容斥原理

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2669 题意概述:实际上原题意很简洁了我就不写了吧.... 二话不说先观察一下性质,首先棋盘 ...

  8. BZOJ3812 主旋律(状压dp+容斥原理)

    设f[S]为S点集是SCC的方案数.考虑通过去掉不合法方案转移.可以枚举入度为0的SCC所含点集S',这样显然S^S'内部的边和由S'连向S^S'的边删还是不删任选.但是这样无法保证S'包含所有入度为 ...

  9. [BZOJ3812]主旋律:状压DP+容斥原理

    分析 Miskcoo orz 令\(f[S]\)表示使得\(S\)这个点集强连通的方案数. 然后呢?不会了 考虑到将一个有向图SCC缩点后,得到的新图是一个DAG,所以我们可以类比带标号DAG计数的解 ...

随机推荐

  1. solr 安装

    1:solr简介 solr是一个开源的搜索引擎,是对lucene做了封装,对外提供类似于webservice接口, 可以使用http请求的方式对solr进行操作. lucene.solr.elasti ...

  2. Spring4读书笔记(2)- 使用场景

    Spring使用场景 完成规模的spring web应用程序 Spring做中间层,与第三方web框架集成 spring远程调用场景 EJB集成-重用现有的POJOs

  3. UOJ#213——【UNR #1】争夺圣杯

    1.题意:给一个序列,枚举长度x,然后在这个序列中所有长度为x的区间,我们求出这些区间的最大值之和并取模,最后将所有的异或起来就好啦 2.分析:听说好多人写的 ,特来写一发 的算法骗访问量 话说这个东 ...

  4. Android控制ScrollView滚动

    有两种办法,第一种,使用scrollTo(),这个方法不需要handler,直接调用就行 第二种方式,使用fullScrol() 下面我们看一下这个函数: scrollView.fullScroll( ...

  5. python拆分CANLog

    通过CANOE 导出的log通常有很多个ID的数据,如何才能找到某一个ID下的特殊的信号?利用python可以简单的进行这个步骤,代码如下: 说明: 最终的效果是将log信息,分不同的ID进行拆分,并 ...

  6. BZOJ 4668: 冷战

    Description 在一个图上,在两个点间连一条边,问这两个点最早在什么时候联通. Sol 并查集+启发式合并. 按秩合并的并查集...我也不知道什么是按秩合并,反正就跟启发式合并差不多,合并的时 ...

  7. python模块引用问题(比较杂乱,懒得整理)

    1 在stackoverflows摘抄 If the import module in the same dir, use e.g: from . import core If the import ...

  8. 域名管理系统DNS

    域名系统DNS,将域名转化为ip地址.域名到ip地址解析过程是以这种方式进行的,当某一程序需要把主机名解析为IP地址时,该应用进程就调用解析程序(本地程序),这时候该进程就变成了DNS的一个客户,将待 ...

  9. python supervisor使用

    Supervisor 是基于 Python 的进程管理工具,只能运行在 Unix-Like 的系统上,也就是无法运行在 Windows 上.Supervisor 官方版目前只能运行在 Python 2 ...

  10. 关于SimpleAdapter和ListView结合使用,实现列表视图的笔记

    使用ListView需要为其添加适配器: 适配器有两种:1.ArrayAdapter  --用于单独文字显示 2.SimpleAdapter --用于文字和图片显示 这里主要记录SimpleAdapt ...