转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

D. Xenia and Dominoes

Xenia likes puzzles very much. She is especially fond of the puzzles that consist of domino pieces. Look at the picture that shows one of such puzzles.

A puzzle is a 3 × n table with forbidden cells (black squares) containing dominoes (colored rectangles on the picture). A puzzle is calledcorrect if it meets the following conditions:

  • each domino occupies exactly two non-forbidden cells of the table;
  • no two dominoes occupy the same table cell;
  • exactly one non-forbidden cell of the table is unoccupied by any domino (it is marked by a circle in the picture).

To solve the puzzle, you need multiple steps to transport an empty cell from the starting position to some specified position. A move is transporting a domino to the empty cell, provided that the puzzle stays correct. The horizontal dominoes can be moved only horizontally, and vertical dominoes can be moved only vertically. You can't rotate dominoes. The picture shows a probable move.

Xenia has a 3 × n table with forbidden cells and a cell marked with a circle. Also, Xenia has very many identical dominoes. Now Xenia is wondering, how many distinct correct puzzles she can make if she puts dominoes on the existing table. Also, Xenia wants the circle-marked cell to be empty in the resulting puzzle. The puzzle must contain at least one move.

Help Xenia, count the described number of puzzles. As the described number can be rather large, print the remainder after dividing it by1000000007 (109 + 7).

Input

The first line contains integer n (3 ≤ n ≤ 104) — the puzzle's size. Each of the following three lines contains n characters — the description of the table. The j-th character of the i-th line equals "X" if the corresponding cell is forbidden; it equals ".", if the corresponding cell is non-forbidden and "O", if the corresponding cell is marked with a circle.

It is guaranteed that exactly one cell in the table is marked with a circle. It is guaranteed that all cells of a given table having at least one common point with the marked cell is non-forbidden.

Output

Print a single number — the answer to the problem modulo 1000000007 (109 + 7).

Sample test(s)
input
5
....X
.O...
...X.
output
1
input
5
.....
.O...
.....
output
2
input
3
...
...
..O
output
4
Note

Two puzzles are considered distinct if there is a pair of cells that contain one domino in one puzzle and do not contain it in the other one.

好题。

问有几种排法使得有至少一块多米诺骨牌可以移动。其中O点和X点不能放置多米诺骨牌,但要求最终的可以通过O点移动。X点表示障碍。

要求的即为四个方向中至少有一个方向可以移动,所以,容斥的时候,把那个用X表示好,然后类似poj的铺砖问题,状压。

 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 100010
char a[][MAXN];
int dp[MAXN][];
int m[MAXN];
const int MOD=;
int n;
int gao(){
CLR(m,);
for(int i=;i<;i++){
for(int j=;j<n;j++){
if(a[i][j]=='X')m[j]|=<<i;
}
}
CLR(dp,);
dp[][]=;
for(int i=;i<n;i++){
for(int j=;j<;j++){
for(int k=;k<;k++){
if(k&m[i])continue;
int x=j|m[i]|k;
if((j&(k|m[i]))==&&(x==||x==||x==)){
dp[i+][k]+=dp[i][j];
if(dp[i+][k]>=MOD)dp[i+][k]-=MOD;
}
}
}
}
return dp[n][];
}
bool check(int x,int y){
if(x>=&&x<&&y>=&&y<n&&a[x][y]=='.')return ;
return ;
}
int dx[]={-,,,};
int dy[]={,,-,};
int main()
{
ios::sync_with_stdio(false);
int ci,cj;
int x[],y[],tot=;
cin>>n;
for(int i=;i<;i++){
for(int j=;j<n;j++){
cin>>a[i][j];
if(a[i][j]=='O')ci=i,cj=j,a[i][j]='X';
}
}
int ans=;
for(int i=;i<;i++){
int t=i;
tot=;
int c=;
for(int j=;j<;j++){
if(t&){
x[tot]=dx[j];
y[tot++]=dy[j];
x[tot]=dx[j]*;
y[tot++]=dy[j]*;
c++;
}
t>>=;
}
bool flag=;
for(int j=;j<tot;j++){
if(check(ci+x[j],cj+y[j]))flag=;
}
if(flag){
for(int j=;j<tot;j++){
a[ci+x[j]][cj+y[j]]='X';
}
if(c&)ans+=gao();
else ans-=gao();
if(ans>=MOD)ans-=MOD;
else if(ans<)ans+=MOD;
for(int j=;j<tot;j++){
a[ci+x[j]][cj+y[j]]='.';
}
}
}
cout<<ans<<endl; return ;
}

代码君

codeforces 342D Xenia and Dominoes(状压dp+容斥)的更多相关文章

  1. Codeforces 342D Xenia and Dominoes 状压dp

    码就完事了. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define ...

  2. bzoj2669 [cqoi2012]局部极小值 状压DP+容斥

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2669 题解 可以发现一个 \(4\times 7\) 的矩阵中,有局部最小值的点最多有 \(2 ...

  3. 一本通 1783 矩阵填数 状压dp 容斥 计数

    LINK:矩阵填数 刚看到题目的时候感觉是无从下手的. 可以看到有n<=2的点 两个矩形. 如果只有一个矩形 矩形外的方案数容易计算考虑 矩形内的 必须要存在x这个最大值 且所有值<=x. ...

  4. P3160 [CQOI2012]局部极小值 题解(状压DP+容斥)

    题目链接 P3160 [CQOI2012]局部极小值 双倍经验,双倍快乐 解题思路 存下来每个坑(极小值点)的位置,以这个序号进行状态压缩. 显然,\(4*7\)的数据范围让极小值点在8个以内(以下示 ...

  5. HDU 5838 (状压DP+容斥)

    Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...

  6. bzoj2560串珠子 状压dp+容斥(?)

    2560: 串珠子 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 515  Solved: 348[Submit][Status][Discuss] ...

  7. [清华集训2015 Day1]主旋律-[状压dp+容斥]

    Description Solution f[i]表示状态i所代表的点构成的强连通图方案数. g[i]表示状态i所代表的的点形成奇数个强连通图的方案数-偶数个强连通图的方案数. g是用来容斥的. 先用 ...

  8. NOIp模拟赛 巨神兵(状压DP 容斥)

    \(Description\) 给定\(n\)个点\(m\)条边的有向图,求有多少个边集的子集,构成的图没有环. \(n\leq17\). \(Solution\) 问题也等价于,用不同的边集构造DA ...

  9. 【BZOJ2560】串珠子 状压DP+容斥

    [BZOJ2560]串珠子 Description 铭铭有n个十分漂亮的珠子和若干根颜色不同的绳子.现在铭铭想用绳子把所有的珠子连接成一个整体. 现在已知所有珠子互不相同,用整数1到n编号.对于第i个 ...

随机推荐

  1. (原)10-folder交叉验证

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6069731.html 参考网址: https://github.com/cmusatyalab/ope ...

  2. 获取本机CPU,硬盘等使用情况

    早上的时候接到主管的一个任务,要获取服务器上的cpu,硬盘, 数据库等 的使用情况,并以邮件的方式发给boss, = =没办法,公司的服务器真是不敢恭维,顺便吐槽一下公司的网速,卡的时候30k左右徘徊 ...

  3. 利用Azure高级存储搭建高性能Linux服务器(2)

    我们首先来测试随机写的IOPS,可以看到随机写的IOPS可以达到,顺序写的IOPS可以达到: $ sudo fio -filename=/data/testfile -direct=1 -iodept ...

  4. 新闻动态切换图片html(flash)

    效果图: 代码: <table id="table_zi"> <tr> <td class="width330"> < ...

  5. strace基本操作

    可以发现很多真正在系统层面发生的调用,以及很细微的返回错误信息,用于调试工作.(比如,软件出错,或是性能变慢...) strace -p 32000 -o strace.txt 基本上完整的用法是这样 ...

  6. SSL和SSH和OpenSSH,OpenSSL有什么区别

    ssl是通讯链路的附加层.可以包含很多协议.https, ftps, ..... ssh只是加密的shell,最初是用来替代telnet的.通过port forward,也可以让其他协议通过ssh的隧 ...

  7. CDT+Eclipse代码自动提示

    1.查看GCC的版本:$gcc -v————————————————————————gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)———————— ...

  8. HashMap非线程安全分析

    通过各方资料了解,HashMap不是线程安全的,但是为什么不是线程安全的,在什么情况下会出现问题呢? 1. 下面对HashMap做一个实验,两个线程,并发写入不同的值,key和value相同,最后再看 ...

  9. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  10. Java虚拟机体系结构深入研究总结

    工作以来,代码越写越多,程序也越来越臃肿,效率越来越低,对于我这样一个追求完美的程序员来说,这是绝对不被允许的,于是除了不断优化程序结构外,内存优化和性能调优就成了我惯用的“伎俩”. 要对Java程序 ...