转载请注明出处: 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. CCFileUtils::getFileData疑惑

    背景 这几天在使用cocos2d-x读取磁盘文件的时候,发现了CCFileUtils中一点不合理的地方,特此记录,以供研讨. 项目结构 ①我使用的是cocos2d-x 2.1.3版本,CCFileUt ...

  2. 【android】修改android默认应用图标

    我自己做的一个小程序,想更改程序安装后的默认显示图片,但是我发现只能改一次,以后再改还是显示第一次更改后的图片(此时我已把最后一次更改前的全部图片都删除了,所以不会是名称填错),这是为什么??求高人指 ...

  3. Bootstrap定制(一)less入门及编译

    第一篇博,希望支持. 近期在开发一个项目,项目前端定位于bootstrap,遂花了少许时间研究了bootstrap,将其整理整理,与众人共享. bootstrap官方的定制,功能还算完善,但是基于we ...

  4. post 报文请求接口方法

    /// <summary> /// post 报文到接口服务器 /// </summary> /// <param name="targetUri"& ...

  5. Non-unique Elements

    Non-unique Elements You are given a non-empty list of integers (X). For this task, you should return ...

  6. HDU 4611Balls Rearrangement(思维)

    Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  7. linux下能ping ip不能ping域名详解

    今天在开发的同事来说,内网不能通过域名访问自己的服务器!然后做了下面的测试发现这样的问题: [root@itmop ~]# ping www.downcc.com ping: unknown host ...

  8. mongodb----修改器

    $inc:增加或者减少指定键值,如果键不存在,就创建一个键. $set:指定一个健的值,如果键不存在,就创建一个键. $unset:删除指定的键. $push:向指定的数组末尾加添加一个元素,如果数组 ...

  9. Poj1741-Tree(树分治)

    题意:找树上有多少对距离小于K的对数解析:树分治模板题,见注释 代码 #include<cstdio> #include<cstring> #include<string ...

  10. hdu 1829 A Bug's Life(并查集)

                                                                                                    A Bu ...