codeforces 342D Xenia and Dominoes(状压dp+容斥)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
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).
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.
Print a single number — the answer to the problem modulo 1000000007 (109 + 7).
5
....X
.O...
...X.
1
5
.....
.O...
.....
2
3
...
...
..O
4
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+容斥)的更多相关文章
- Codeforces 342D Xenia and Dominoes 状压dp
码就完事了. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define ...
- bzoj2669 [cqoi2012]局部极小值 状压DP+容斥
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2669 题解 可以发现一个 \(4\times 7\) 的矩阵中,有局部最小值的点最多有 \(2 ...
- 一本通 1783 矩阵填数 状压dp 容斥 计数
LINK:矩阵填数 刚看到题目的时候感觉是无从下手的. 可以看到有n<=2的点 两个矩形. 如果只有一个矩形 矩形外的方案数容易计算考虑 矩形内的 必须要存在x这个最大值 且所有值<=x. ...
- P3160 [CQOI2012]局部极小值 题解(状压DP+容斥)
题目链接 P3160 [CQOI2012]局部极小值 双倍经验,双倍快乐 解题思路 存下来每个坑(极小值点)的位置,以这个序号进行状态压缩. 显然,\(4*7\)的数据范围让极小值点在8个以内(以下示 ...
- HDU 5838 (状压DP+容斥)
Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...
- bzoj2560串珠子 状压dp+容斥(?)
2560: 串珠子 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 515 Solved: 348[Submit][Status][Discuss] ...
- [清华集训2015 Day1]主旋律-[状压dp+容斥]
Description Solution f[i]表示状态i所代表的点构成的强连通图方案数. g[i]表示状态i所代表的的点形成奇数个强连通图的方案数-偶数个强连通图的方案数. g是用来容斥的. 先用 ...
- NOIp模拟赛 巨神兵(状压DP 容斥)
\(Description\) 给定\(n\)个点\(m\)条边的有向图,求有多少个边集的子集,构成的图没有环. \(n\leq17\). \(Solution\) 问题也等价于,用不同的边集构造DA ...
- 【BZOJ2560】串珠子 状压DP+容斥
[BZOJ2560]串珠子 Description 铭铭有n个十分漂亮的珠子和若干根颜色不同的绳子.现在铭铭想用绳子把所有的珠子连接成一个整体. 现在已知所有珠子互不相同,用整数1到n编号.对于第i个 ...
随机推荐
- poj1094 topsort
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32275 Accepted: 11 ...
- 范围for语句 && 列表初始值&& 标准库函数begin和end
范围for语句: 引入的意义:简化传统for的编写,主要用于遍历给定序列中的每个元素并对序列中的每个值执行某种操作,其语法形式是: for( 声明: 给定序列) { 执行的操作. } 其中,“给定序列 ...
- linux下查找文件和文件内容
find /xxx -name "*" | xargs grep "某内容" /xxx表示路径,"*"表示在含有某关键字名字下的文件中查找, ...
- web api简单验证实现办法
需要使用WEBAPI,但是有验证问题没解决.后来参考网上文章做了一下DEMO 思路: 就是根据用户的账号在服务端加密一个字符串,然后返回给用户端. 具体: 一个用户编号用于唯一身份识别,密码,一个密钥 ...
- mysql(mariadb)重装
MariaDB是MySQL的一个分支,主要由开源社区进行维护和升级,而MySQL被Oracle收购以后,发展较慢.在CentOS 7的软件仓库中,将MySQL更替为了MariaDB. Centos ...
- 24C01的IIC 讀寫的c51程式
/*------------------------------------------------------------------------------ 為了安全起見,程式中很多NOP是冗餘的 ...
- yii基础知识-应用
应用是指请求处理中的执行上下文.它的主要任务是分析用户请求并将其分派到合适的控制器中以作进一步处理. 它同时作为服务中心,维护应用级别的配置.鉴于此,应用也叫做前端控制器. 应用由 入口脚本 创建为一 ...
- Search for a Range 解答
Question Given a sorted array of integers, find the starting and ending position of a given target v ...
- TCP快速重传和快速恢复
当tcp传送一个分组时会设置一个定时器,如果在规定的实际间隔内没有收到ACK分组,那么则重新传输该分组,但是 如果tcp收到三个连续的ACK分组,此时不管是否过超时间隔则重传该分组,具体步骤如下: 1 ...
- 7款纯CSS3实现的炫酷动画应用|慕课网只学有用的!
关于我们 | 时尚廊 ♦ 时尚廊,中国大陆地区首家以"Lounge"为概念的艺文空间 ♦ 7款纯CSS3实现的炫酷动画应用|慕课网只学有用的! 7款纯CSS3实现的炫酷动画应用