听说1038是哈尔滨网络赛1005的简化版.
感觉写起来比1005难多了..
又3进制什么的,Debug半天都过不了自己的数据..无奈用回二进制来保存状态.
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
#include <iostream>
2
#define BIT(x, i, l) (((x) >> (i))&mask[l])
3
#define ORBIT(i, l) (mask[l] << i)
4
#define max(a, b) ((a) > (b) ? (a) : (b))
5
using namespace std;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
int p[11] =
{1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049}, mask[4] =
{0, 1, 3, 7};
8
int curr[60000], next[60000];
9
int map[2 << 10][2 << 10], hash[60000][2];
10
int black[155];
11
int d, n, m, h, k, sta, ans;
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
int cast(int i, int j)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
15
int ret = 0;
16
for (int x = 0; x < 10; x++)
17
if (BIT(j, x, 1))
18
ret += p[x] *2;
19
else if (BIT(i, x, 1))
20
ret += p[x];
21
return ret;
22
}
23![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
void init()
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
26
memset(map, 0, sizeof (map));
27
memset(hash, 0, sizeof (hash));
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
for (int i = 0; i < p[10]; i++)
30
for (int j = 0, k; j < 10; j++)
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
k = (i / p[j]) % 3;
33
if (k != 2)
34
hash[i][0] |= (k << j);
35
else
36
hash[i][0] |= (1 << j), hash[i][1] |= (1 << j);
37
}
38
}
39![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
40
void dfs(int i, int j, int k, int x, int v)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
42
if (x >= m)
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
if (!map[j][k])
45
map[j][k] = cast(j, k) + 1;
46
next[map[j][k] - 1] = max(next[map[j][k] - 1], v);
47
return;
48
}
49![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
if (!BIT(i, x - 1, 2) && !BIT(j, x - 1, 2) && !BIT(k, x - 1, 2))
51
dfs(i | ORBIT(x - 1, 2), j | ORBIT(x - 1, 2), k | ORBIT(x - 1, 2), x + 2, v + 1);
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
if (x >= 2)
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
if (!BIT(i, x - 2, 3) && !BIT(j, x - 2, 3))
56
dfs(i | ORBIT(x - 2, 3), j | ORBIT(x - 2, 3), k, x + 2, v + 1);
57
if (!BIT(j, x - 2, 3) && !BIT(k, x - 2, 3))
58
dfs(i, j | ORBIT(x - 2, 3), k | ORBIT(x - 2, 3), x + 2, v + 1);
59
}
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
dfs(i, j, k, x + 1, v);
62
}
63![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
64
int main()
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
66
init();
67
scanf("%d", &d);
68
while (d--)
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70
scanf("%d %d %d", &n, &m, &k);
71![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
memset(black, 0, sizeof (black));
73
memset(curr, 0, sizeof (curr));
74![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
for (int i = 0, x, y; i < k; i++)
76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
scanf("%d %d", &y, &x);
78
black[y] |= (1 << (m - x));
79
}
80
black[0] = black[n + 1] = (2 << m) - 1;
81![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
sta = 0;
83
for (int i = 0; i < m; i++)
84
if (!BIT(black[1], i, 1))
85
sta += p[i];
86
else
87
sta += 2 * p[i];
88
curr[sta] = 1;
89![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
90
for (int y = 0; y < n - 1; y++)
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92
memset(next, 0, sizeof (next));
93
for (int i = 0; i < p[m]; i++)
94
if (curr[i])
95
dfs(hash[i][0], hash[i][1], black[y + 2], 1, curr[i]);
96
memcpy(curr, next, sizeof (next));
97
}
98![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
ans = 0;
100
for (int i = 0; i < p[m]; i++)
101
ans = max(ans, curr[i] - 1);
102
printf("%d\n", ans);
103
}
104
return 0;
105
}