codeforces 342 D Xenia and Dominoes(状圧dp+反発)
21652 ワード
転載は出典を明記してください: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
output
input
output
input
output
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.
いい問題.
少なくとも1枚のドミノの骨牌が移動できるようにするにはいくつかの並べ方があります.ここで,O点とX点はドミノ骨牌を置くことはできないが,最終的にはO点で移動できることが要求される.X点は障害を表す.
要求されるのは4つの方向のうち少なくとも1つの方向が移動できるので、反発するときは、それをXで表し、pojのレンガ舗装問題のように、状圧します.
コード君
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:
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.
いい問題.
少なくとも1枚のドミノの骨牌が移動できるようにするにはいくつかの並べ方があります.ここで,O点とX点はドミノ骨牌を置くことはできないが,最終的にはO点で移動できることが要求される.X点は障害を表す.
要求されるのは4つの方向のうち少なくとも1つの方向が移動できるので、反発するときは、それをXで表し、pojのレンガ舗装問題のように、状圧します.
1 //#####################
2 //Author:fraud
3 //Blog: http://www.cnblogs.com/fraud/
4 //#####################
5 #include <iostream>
6 #include <sstream>
7 #include <ios>
8 #include <iomanip>
9 #include <functional>
10 #include <algorithm>
11 #include <vector>
12 #include <string>
13 #include <list>
14 #include <queue>
15 #include <deque>
16 #include <stack>
17 #include <set>
18 #include <map>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <cmath>
22 #include <cstring>
23 #include <climits>
24 #include <cctype>
25 using namespace std;
26 #define XINF INT_MAX
27 #define INF 0x3FFFFFFF
28 #define MP(X,Y) make_pair(X,Y)
29 #define PB(X) push_back(X)
30 #define REP(X,N) for(int X=0;X<N;X++)
31 #define REP2(X,L,R) for(int X=L;X<=R;X++)
32 #define DEP(X,R,L) for(int X=R;X>=L;X--)
33 #define CLR(A,X) memset(A,X,sizeof(A))
34 #define IT iterator
35 typedef long long ll;
36 typedef pair<int,int> PII;
37 typedef vector<PII> VII;
38 typedef vector<int> VI;
39 #define MAXN 100010
40 char a[5][MAXN];
41 int dp[MAXN][10];
42 int m[MAXN];
43 const int MOD=1000000007;
44 int n;
45 int gao(){
46 CLR(m,0);
47 for(int i=0;i<3;i++){
48 for(int j=0;j<n;j++){
49 if(a[i][j]=='X')m[j]|=1<<i;
50 }
51 }
52 CLR(dp,0);
53 dp[0][0]=1;
54 for(int i=0;i<n;i++){
55 for(int j=0;j<8;j++){
56 for(int k=0;k<8;k++){
57 if(k&m[i])continue;
58 int x=j|m[i]|k;
59 if((j&(k|m[i]))==0&&(x==7||x==4||x==1)){
60 dp[i+1][k]+=dp[i][j];
61 if(dp[i+1][k]>=MOD)dp[i+1][k]-=MOD;
62 }
63 }
64 }
65 }
66 return dp[n][0];
67 }
68 bool check(int x,int y){
69 if(x>=0&&x<3&&y>=0&&y<n&&a[x][y]=='.')return 0;
70 return 1;
71 }
72 int dx[4]={-1,0,0,1};
73 int dy[4]={0,1,-1,0};
74 int main()
75 {
76 ios::sync_with_stdio(false);
77 int ci,cj;
78 int x[110],y[110],tot=0;
79 cin>>n;
80 for(int i=0;i<3;i++){
81 for(int j=0;j<n;j++){
82 cin>>a[i][j];
83 if(a[i][j]=='O')ci=i,cj=j,a[i][j]='X';
84 }
85 }
86 int ans=0;
87 for(int i=1;i<16;i++){
88 int t=i;
89 tot=0;
90 int c=0;
91 for(int j=0;j<4;j++){
92 if(t&1){
93 x[tot]=dx[j];
94 y[tot++]=dy[j];
95 x[tot]=dx[j]*2;
96 y[tot++]=dy[j]*2;
97 c++;
98 }
99 t>>=1;
100 }
101 bool flag=1;
102 for(int j=0;j<tot;j++){
103 if(check(ci+x[j],cj+y[j]))flag=0;
104 }
105 if(flag){
106 for(int j=0;j<tot;j++){
107 a[ci+x[j]][cj+y[j]]='X';
108 }
109 if(c&1)ans+=gao();
110 else ans-=gao();
111 if(ans>=MOD)ans-=MOD;
112 else if(ans<0)ans+=MOD;
113 for(int j=0;j<tot;j++){
114 a[ci+x[j]][cj+y[j]]='.';
115 }
116 }
117 }
118 cout<<ans<<endl;
119
120
121 return 0;
122 }
コード君