转载请注明出处: 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. 给软件增加注册功能 c#

    1.软件注册类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  2. wordpress安装地址与博客地址

    可千万别乱改动你的wordpress安装地址和博客地址 一月 27th, 2009 Posted in web学习, 博客建设 | 12 Comments » 我最近一个月学习数据库的一些使用,把他爱 ...

  3. NPAPI开发常识

    其实,就在几天前,我根本不知道什么是NPAPI插件.因为最近的项目中用到需要在非IE下检测客户端是否安装,最终找到浏览器插件NPAPI. 以下资料来源于网络收集,以此给那些和我一样迷茫的人.本文的目的 ...

  4. [HDU] 3711 Binary Number [位运算]

    Binary Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  5. Xming + PuTTY 在Windows下远程Linux主机

    Xming + PuTTY 在Windows下远程Linux主机 一.原理     Linux/Unix的X Window具有网络透明性.X Window系统里有一个统一的Server来负责各个程序与 ...

  6. 转:更新Android SDK之后Eclipse提示ADT版本过低的一个简易解决办法

    拜GFW所赐,对于初学者的我来说,总会出现一些莫名其妙的问题 首先说明一下发表这一篇博文的“历史原因”吧,因为在更新SDK之后,进入Eclipse设置Android SDK目录的时候,会突然说我的版本 ...

  7. HashMap非线程安全分析

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

  8. vijos1781 同余方程

    描述 求关于x的同余方程ax ≡ 1 (mod b)的最小正整数解. 格式 输入格式 输入只有一行,包含两个正整数a, b,用一个空格隔开. 输出格式 输出只有一行,包含一个正整数x0,即最小正整数解 ...

  9. 软硬结合的可穿戴式app

    可穿戴设备已经非常的火热了,各种手环.手表之类的硬件设备已经层出不穷,并且功能也已经越发强大,从简单的运动.心率追踪,到现在的血糖.心电图监测,“量化自我”的愿景似乎已经变得越来越明朗,但也正是在这样 ...

  10. UGUI 过渡动画插件,模仿NGUI的Tween (转载)

    最近在相亲,后来好朋友跟我说他写了一个好插件,于是我就把女朋友甩了,看看他的插件,可以在UGUI制作简单过渡动画. 我看了下是模仿NGUI的Tween, 我在筱程的基础上稍微改到人性化, 简单支持的让 ...