-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva11391.cpp
143 lines (97 loc) · 2.18 KB
/
uva11391.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <limits.h>
#include <list>
#include <queue>
#include <map>
#define TRvi(c, it) \
for(vector<int>::iterator it = c.begin(); it != c.end(); it++)
#define BFS_WHITE -1
#define directions 8
using namespace std;
int R, C, N;
int memo[1 << 16][20];
int xAxis[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int yAxis[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int pos(int x, int y) //turn 2D position into linear position in bitmask
{
return R * x + y;
}
bool checkEdges(int x, int y)
{
return 0 <= x && x < R && 0 <= y && y < C;
}
int blobs(int bitmask, int jump)
{
if(memo[bitmask][jump] != -1)
return memo[bitmask][jump];
if(!(bitmask & (bitmask - 1))) //bitmask is power of 2 means only one blob
{
if(jump == 0)
return 1;
else
return 0;
} //not necessary but good pruning
if(jump == 0)
{
if(!(bitmask & (bitmask - 1)))
return 1;
else
return 0;
}
int combinations = 0;
for(int x = 0; x < R; x++)
{
for(int y = 0; y < C; y++)
{
if(bitmask & (1 << pos(x, y))) //if there is a blob on (x,y)
{
for(int i = 0; i < directions; i++)
{
int next_x = x + xAxis[i];
int next_y = y + yAxis[i];
int jump_x = x + 2 * xAxis[i];
int jump_y = y + 2 * yAxis[i];
//if blob on adjacent position
if(checkEdges(next_x, next_y) && (bitmask & (1 << pos(next_x, next_y))))
{
//if jump cell is empty
if(checkEdges(jump_x, jump_y) && !(bitmask & (1 << pos(jump_x, jump_y))))
{
int bm = bitmask;
bm |= (1 << pos(jump_x, jump_y));
bm &= ~(1 << pos(x, y));
bm &= ~(1 << pos(next_x, next_y));
combinations += blobs(bm, jump - 1);
}
}
}
}
}
}
return memo[bitmask][jump] = combinations;
}
int main()
{
int bitmask, x, y;
int T; cin >> T;
for(int tc = 1; tc <= T; tc++)
{
cin >> R >> C >> N;
bitmask = 0;
for(int i = 0; i < N; i++)
{
cin >> x >> y;
x--; y--;
bitmask |= (1 << pos(x, y));
}
memset(memo, -1, sizeof(memo));
cout << "Case " << tc << ": " << blobs(bitmask, N-1) << endl;
}
return 0;
}