USACO Section 1.4 The Clocks

还是感觉枚举好,搜索啥的越想越麻烦。

因为表是圆的,每个表转四下会回到原来的位置,所以,对于每个表,最多转3次,或者不转,也就是说,对于9个表,每个表有四种可能,那么,一共有4^9种可能,依次枚举,第一个符合条件的即是最小的

 1 /* ID:linyvxi1
2 PROB:clocks
3 LANG:C++
4 */
5 #include <stdio.h>
6 #include<string>
7 #include<algorithm>
8 using namespace std;
9
10 int ans[10], num[10], tmp[10];
11
12 int
13 main ()
14 {
15 freopen ("clocks.in", "r", stdin);
16 freopen ("clocks.out", "w", stdout);
17 int i;
18 for (i = 1; i <= 9; i++)
19 {
20 scanf ("%d", &ans[i]);
21 ans[i] /= 3;
22 }
23 for (num[1] = 0; num[1] <= 3; num[1]++) //每一种操作口令的执行次数都不会超过3次
24 for (num[2] = 0; num[2] <= 3; num[2]++)
25 for (num[3] = 0; num[3] <= 3; num[3]++)
26 for (num[4] = 0; num[4] <= 3; num[4]++)
27 for (num[5] = 0; num[5] <= 3; num[5]++)
28 for (num[6] = 0; num[6] <= 3; num[6]++)
29 for (num[7] = 0; num[7] <= 3; num[7]++)
30 for (num[8] = 0; num[8] <= 3; num[8]++)
31 for (num[9] = 0; num[9] <= 3; num[9]++)
32 {
33 tmp[1] = (ans[1] + num[1] + num[2] + num[4]) % 4;
34 tmp[2] =
35 (ans[2] + num[1] + num[2] + num[3] + num[5]) % 4;
36 tmp[3] = (ans[3] + num[2] + num[3] + num[6]) % 4;
37 tmp[4] =
38 (ans[4] + num[1] + num[4] + num[5] + num[7]) % 4;
39 tmp[5] =
40 (ans[5] + num[1] + num[3] + num[5] + num[7] +
41 num[9]) % 4;
42 tmp[6] =
43 (ans[6] + num[3] + num[5] + num[6] + num[9]) % 4;
44 tmp[7] = (ans[7] + num[4] + num[7] + num[8]) % 4;
45 tmp[8] =
46 (ans[8] + num[5] + num[7] + num[8] + num[9]) % 4;
47 tmp[9] = (ans[9] + num[6] + num[8] + num[9]) % 4;
48 if (tmp[1] + tmp[2] + tmp[3] + tmp[4] + tmp[5] +
49 tmp[6] + tmp[7] + tmp[8] + tmp[9] == 0)
50 {
51 bool s = false;
52 for (i = 1; i <= 9; i++)
53 {
54 if (num[i])
55 {
56 int j;
57 for (j = 1; j <= num[i]; j++)
58 {
59 if (!s)
60 s = true;
61 else
62 putchar (' ');
63 printf ("%d", i);
64 }
65 }
66 }
67
68 printf ("\n");
69 return 0;
70 }
71 }
72 }



原文地址:https://www.cnblogs.com/yangce/p/2343072.html