-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathUVa 11391 - Blobs in the Board.cpp
86 lines (72 loc) · 2.06 KB
/
UVa 11391 - Blobs in the Board.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
#include <iostream>
#include <cstring>
using namespace std;
int R, C, N;
int dp[5][5][1 << 16];
const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
bool isOutOfBoard(int x, int y)
{
return x <= 0 || y <= 0 || x >= R + 1 || y >= C + 1;
}
int shift(int x, int y)
{
return (x - 1) * 4 + y - 1;
}
int search(int board)
{
if ((board & (board - 1)) == 0)
return 1;
if (dp[R][C][board] != -1)
return dp[R][C][board];
int ret = 0;
for (int x = 1; x <= R; ++x)
for (int y = 1; y <= C; ++y)
if (board & (1 << shift(x, y)))
for (int i = 0; i < 8; ++i)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (isOutOfBoard(nx, ny))
continue;
if (board & (1 << shift(nx, ny)))
{
int tx = nx + dx[i];
int ty = ny + dy[i];
if (isOutOfBoard(tx, ty))
continue;
if ((board & (1 << shift(tx, ty))) == 0)
ret += search(board
& ~(1 << shift(x, y))
& ~(1 << shift(nx, ny))
| (1 << shift(tx, ty))
);
}
}
return dp[R][C][board] = ret;
}
int solve(int initialBoard)
{
return search(initialBoard);
}
int main()
{
int T, Case = 1;
cin >> T;
// It's important to store the results across different test cases.
memset(dp, -1, sizeof(dp));
while ( T-- )
{
cin >> R >> C >> N;
int initialBoard = 0;
for (int i = 1; i <= N; ++i)
{
int x, y;
cin >> x >> y;
initialBoard |= (1 << shift(x, y));
}
cout << "Case " << Case++ << ": "
<< solve(initialBoard) << endl;
}
return 0;
}