2008 Asia Harbin Regional Contest Online 的 1005 Bricks,位DP+DFS
第二次碰到这样的题(第一次是POJ的2411),看到状态那么复杂,结果比赛时就没去code,今天下午终于下决心把它给KO了.
发现比想象中简单.
Time :0.0230 s Memory:424 k
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
7 int n, m, h, ans;
8 int curr[1 << 6][1 << 6], next[1 << 6][1 << 6];
9 int mem[105][7];
10 int mask[4] = {0, 1, 3, 7};
11
12 void dfs(int i, int j, int k, int bit, int v)
13 {
14 if (bit == m)
15 {
16 next[j][k] = max(next[j][k], v);
17 mem[h + 2][m] = max(mem[h + 2][m], v);
18 return;
19 }
20 if (bit >= 1)
21 {
22 if (!BIT(i, bit - 1, 1) && !BIT(j, bit - 1, 2))
23 //shape 0
24 dfs(i | ORBIT(bit - 1, 1), j | ORBIT(bit - 1, 2), k | ORBIT(bit - 1, 1), bit + 1, v + 4);
25 if (!BIT(i, bit - 1, 2) && !BIT(j, bit - 1, 1))
26 //shape 4
27 dfs(i | ORBIT(bit - 1, 2), j | ORBIT(bit - 1, 1), k | ORBIT(bit - 1, 1), bit + 1, v + 4);
28 }
29 if (bit >= 2)
30 {
31 if (!BIT(i, bit - 1, 1) && !BIT(j, bit - 2, 3))
32 //shape 1
33 dfs(i | ORBIT(bit - 1, 1), j | ORBIT(bit - 2, 3), k | ORBIT(bit - 1, 1), bit + 1, v + 5);
34 if (!BIT(i, bit - 2, 3) && !BIT(j, bit - 1, 1))
35 //shape 2
36 dfs(i | ORBIT(bit - 2, 3), j | ORBIT(bit - 1, 1), k, bit + 1, v + 4);
37 if (!BIT(j, bit - 2, 3))
38 //shape 2
39 dfs(i, j | ORBIT(bit - 2, 3), k | ORBIT(bit - 1, 1), bit + 1, v + 4);
40 if (!BIT(i, bit - 2, 3) && !BIT(j, bit - 2, 1))
41 //shape 3
42 dfs(i | ORBIT(bit - 2, 3), j | ORBIT(bit - 2, 1), k, bit + 1, v + 4);
43 if (!BIT(j, bit - 2, 3))
44 //shape 3
45 dfs(i, j | ORBIT(bit - 2, 3), k | ORBIT(bit - 2, 1), bit + 1, v + 4);
46 }
47 dfs(i, j, k, bit + 1, v);
48 }
49
50 int main()
51 {
52 n = 100, m = 6;
53
54 for (int i = 0; i <= n; i++)
55 for (int j = 0; j <= m; j++)
56 mem[i][j] = 1;
57
58 for (m = 1; m <= 6; m++)
59 {
60 memset(curr, 0, sizeof (curr));
61 curr[(1 << m) - 1][0] = 1;
62 for (h = 0; h < n - 1; h++)
63 {
64 memset(next, 0, sizeof (next));
65 for (int i = 0; i < (1 << m); i++)
66 for (int j = 0; j < (1 << m); j++)
67 if (curr[i][j])
68 dfs(i, j, 0, 1, curr[i][j]);
69 memcpy(curr, next, sizeof (next));
70 }
71 }
72
73 while (scanf("%d %d", &n, &m) != EOF)
74 printf("%d\n", mem[n][m] - 1);
75 return 0;
76
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
7 int n, m, h, ans;
8 int curr[1 << 6][1 << 6], next[1 << 6][1 << 6];
9 int mem[105][7];
10 int mask[4] = {0, 1, 3, 7};
11
12 void dfs(int i, int j, int k, int bit, int v)
13 {
14 if (bit == m)
15 {
16 next[j][k] = max(next[j][k], v);
17 mem[h + 2][m] = max(mem[h + 2][m], v);
18 return;
19 }
20 if (bit >= 1)
21 {
22 if (!BIT(i, bit - 1, 1) && !BIT(j, bit - 1, 2))
23 //shape 0
24 dfs(i | ORBIT(bit - 1, 1), j | ORBIT(bit - 1, 2), k | ORBIT(bit - 1, 1), bit + 1, v + 4);
25 if (!BIT(i, bit - 1, 2) && !BIT(j, bit - 1, 1))
26 //shape 4
27 dfs(i | ORBIT(bit - 1, 2), j | ORBIT(bit - 1, 1), k | ORBIT(bit - 1, 1), bit + 1, v + 4);
28 }
29 if (bit >= 2)
30 {
31 if (!BIT(i, bit - 1, 1) && !BIT(j, bit - 2, 3))
32 //shape 1
33 dfs(i | ORBIT(bit - 1, 1), j | ORBIT(bit - 2, 3), k | ORBIT(bit - 1, 1), bit + 1, v + 5);
34 if (!BIT(i, bit - 2, 3) && !BIT(j, bit - 1, 1))
35 //shape 2
36 dfs(i | ORBIT(bit - 2, 3), j | ORBIT(bit - 1, 1), k, bit + 1, v + 4);
37 if (!BIT(j, bit - 2, 3))
38 //shape 2
39 dfs(i, j | ORBIT(bit - 2, 3), k | ORBIT(bit - 1, 1), bit + 1, v + 4);
40 if (!BIT(i, bit - 2, 3) && !BIT(j, bit - 2, 1))
41 //shape 3
42 dfs(i | ORBIT(bit - 2, 3), j | ORBIT(bit - 2, 1), k, bit + 1, v + 4);
43 if (!BIT(j, bit - 2, 3))
44 //shape 3
45 dfs(i, j | ORBIT(bit - 2, 3), k | ORBIT(bit - 2, 1), bit + 1, v + 4);
46 }
47 dfs(i, j, k, bit + 1, v);
48 }
49
50 int main()
51 {
52 n = 100, m = 6;
53
54 for (int i = 0; i <= n; i++)
55 for (int j = 0; j <= m; j++)
56 mem[i][j] = 1;
57
58 for (m = 1; m <= 6; m++)
59 {
60 memset(curr, 0, sizeof (curr));
61 curr[(1 << m) - 1][0] = 1;
62 for (h = 0; h < n - 1; h++)
63 {
64 memset(next, 0, sizeof (next));
65 for (int i = 0; i < (1 << m); i++)
66 for (int j = 0; j < (1 << m); j++)
67 if (curr[i][j])
68 dfs(i, j, 0, 1, curr[i][j]);
69 memcpy(curr, next, sizeof (next));
70 }
71 }
72
73 while (scanf("%d %d", &n, &m) != EOF)
74 printf("%d\n", mem[n][m] - 1);
75 return 0;
76
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
7 int n, m, h, ans;
8 int curr[2 << 6][2 << 6], next[2 << 6][2 << 6];
9 int mem[105][7];
10 int mask[4] = {0, 1, 3, 7};
11
12 void dfs(int i, int j, int k, int bit, int v)
13 {
14 if (bit == m)
15 {
16 next[j][k] = max(next[j][k], v);
17 mem[h + 2][m] = max(mem[h + 2][m], v);
18 return;
19 }
20 if (bit >= 1)
21 {
22 if (!BIT(i, bit - 1, 1) && !BIT(j, bit - 1, 2))
23 //shape 0
24 dfs(i | ORBIT(bit - 1, 1), j | ORBIT(bit - 1, 2), k | ORBIT(bit - 1, 1), bit + 1, v + 4);
25 if (!BIT(i, bit - 1, 2) && !BIT(j, bit - 1, 1))
26 //shape 4
27 dfs(i | ORBIT(bit - 1, 2), j | ORBIT(bit - 1, 1), k | ORBIT(bit - 1, 1), bit + 1, v + 4);
28 }
29 if (bit >= 2)
30 {
31 if (!BIT(i, bit - 1, 1) && !BIT(j, bit - 2, 3))
32 //shape 1
33 dfs(i | ORBIT(bit - 1, 1), j | ORBIT(bit - 2, 3), k | ORBIT(bit - 1, 1), bit + 1, v + 5);
34 if (!BIT(i, bit - 2, 3) && !BIT(j, bit - 1, 1))
35 //shape 2
36 dfs(i | ORBIT(bit - 2, 3), j | ORBIT(bit - 1, 1), k, bit + 1, v + 4);
37 if (!BIT(j, bit - 2, 3))
38 //shape 2
39 dfs(i, j | ORBIT(bit - 2, 3), k | ORBIT(bit - 1, 1), bit + 1, v + 4);
40 if (!BIT(i, bit - 2, 3) && !BIT(j, bit - 2, 1))
41 //shape 3
42 dfs(i | ORBIT(bit - 2, 3), j | ORBIT(bit - 2, 1), k, bit + 1, v + 4);
43 if (!BIT(j, bit - 2, 3))
44 //shape 3
45 dfs(i, j | ORBIT(bit - 2, 3), k | ORBIT(bit - 2, 1), bit + 1, v + 4);
46 }
47 dfs(i, j, k, bit + 1, v);
48 }
49
50 int main()
51 {
52 n = 100, m = 6;
53
54 for (int i = 0; i <= n; i++)
55 for (int j = 0; j <= m; j++)
56 mem[i][j] = 1;
57
58 for (m = 1; m <= 6; m++)
59 {
60 memset(curr, 0, sizeof (curr));
61 curr[(2 << m) - 1][0] = 1;
62 for (h = 0; h < n - 1; h++)
63 {
64 memset(next, 0, sizeof (next));
65 for (int i = 0; i < (2 << m); i++)
66 for (int j = 0; j < (2 << m); j++)
67 if (curr[i][j])
68 dfs(i, j, 0, 1, curr[i][j]);
69 memcpy(curr, next, sizeof (next));
70 }
71 }
72
73 while (scanf("%d %d", &n, &m) != EOF)
74 printf("%d\n", mem[n][m] - 1);
75 return 0;
76 }