-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboardcover.cpp
90 lines (87 loc) · 2.19 KB
/
boardcover.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define FastIO ios_base::sync_with_stdio(false),cout.tie(nullptr),cin.tie(nullptr)
int c, h, w;
char** board;
int whiteCount;
int whiteTotal;
int dc[8] = {1, 1, 0, 1, 0, 1, -1, 0};
int dr[8] = {0, 1, 1, 0, 1, 1, 1, 1};
bool isRange(int r, int c) {
if(r < 0 || r > h-1 || c < 0 || c > w-1) {
return false;
}
return true;
}
int dfs(int r, int c) {
if(!isRange(r,c)) {//base condition
if(whiteCount == whiteTotal) {
return 1;
}
return 0;
}
if(board[r][c] == '#') {
if((c+1)%w == 0) {
return dfs(r+1, 0);
} else {
return dfs(r, c+1);
}
}
board[r][c] = '#';
whiteCount += 1;
int ret = 0;
for(int i=0; i<4; i++) {
pair<int ,int> pos[2];
bool flag = false;
for(int j=0; j<2; j++) {
int nr = r + dr[i*2+j];
int nc = c + dc[i*2+j];
if(isRange(nr, nc)) {
if(board[nr][nc] == '.') {
pos[j] = {nr, nc};
continue;
}
}
flag = true;
break;
}
if(!flag) {
whiteCount += 2;
board[pos[0].first][pos[0].second] = '#';
board[pos[1].first][pos[1].second] = '#';
if((c+1)%w == 0) {
ret += dfs(r+1, 0);
} else {
ret += dfs(r, c+1);
}
whiteCount -= 2;
board[pos[0].first][pos[0].second] = '.';
board[pos[1].first][pos[1].second] = '.';
}
}
board[r][c] = '.';
whiteCount -= 1;
return ret;
}
int main(void) {
FastIO;
cin >> c;
while(c--) {
cin >> h >> w;
board = (char**)malloc(sizeof(char*)*h);
for(int i=0; i<h; i++) {
board[i] = (char*)malloc(sizeof(char)*w);
}
for(int i=0; i<h; i++) {
cin >> board[i];
}
whiteTotal = 0;
for(int i=0; i<h; i++) {
for(int j=0; j<w; j++) {
if(board[i][j] == '.') whiteTotal++;
}
}
cout << dfs(0, 0) << endl;
}
}