USACO Section 1.4 The Clocks(DFS)

The Clocks
IOI'94 - Day 2

Consider nine clocks arranged in a 3x3 array thusly:

|-------|    |-------|    |-------|    
|       |    |       |    |   |   |    
|---O   |    |---O   |    |   O   |          
|       |    |       |    |       |           
|-------|    |-------|    |-------|    
    A            B            C

|-------|    |-------|    |-------|
|       |    |       |    |       |
|   O   |    |   O   |    |   O   |
|   |   |    |   |   |    |   |   |
|-------|    |-------|    |-------|
    D            E            F

|-------|    |-------|    |-------|
|       |    |       |    |       |
|   O   |    |   O---|    |   O   |
|   |   |    |       |    |   |   |
|-------|    |-------|    |-------|
    G            H            I

The goal is to find a minimal sequence of moves to return all the dials to 12 o'clock. Nine different ways to turn the dials on the clocks are supplied via a table below; each way is called a move. Select for each move a number 1 through 9 which will cause the dials of the affected clocks (see next table) to be turned 90 degrees clockwise.

Move Affected clocks
1 ABDE
2 ABC
3 BCEF
4 ADG
5 BDEFH
6 CFI
7 DEGH
8 GHI
9 EFHI

Example

Each number represents a time accoring to following table:

9 9 12       9 12 12       9 12 12        12 12 12      12 12 12 
6 6 6  5 ->  9  9  9  8->  9  9  9  4 ->  12  9  9  9-> 12 12 12 
6 3 6        6  6  6       9  9  9        12  9  9      12 12 12 

[But this might or might not be the `correct' answer; see below.]

PROGRAM NAME: clocks

INPUT FORMAT

Lines 1-3: Three lines of three space-separated numbers; each number represents the start time of one clock, 3, 6, 9, or 12. The ordering of the numbers corresponds to the first example above.

SAMPLE INPUT (file clocks.in)

9 9 12
6 6 6
6 3 6

OUTPUT FORMAT

A single line that contains a space separated list of the shortest sequence of moves (designated by numbers) which returns all the clocks to 12:00. If there is more than one solution, print the one which gives the lowest number when the moves are concatenated (e.g., 5 2 4 6 < 9 3 1 1).

SAMPLE OUTPUT (file clocks.out)

4 5 8 9

题意:通过上面九种操作把给定的矩阵变成全是12。
分析:首先要理解最终转变成 12 和操作的顺序无关,然后就是每个操作最多只能做 3 次,因为第 4 次又回到初始状态。所以依次从操作 1 开始枚举到操作 9,同时每层要枚举操作的次数依次是3 2 1 0。最后就是输出了要注意一下,最后不能有空格。

View Code
 1  /*
 2  ID: dizzy_l1
 3  LANG: C++
 4  TASK: clocks
 5  */
 6 #include<iostream>
 7 #include<cstring>
 8 #include<algorithm>
 9 #include<cstdio>
10 
11 using namespace std;
12 
13 int op[10][5]={{0,0,0,0,0},
14                {1,2,4,5,0},{1,2,3,0,0},{2,3,5,6,0},
15                {1,4,7,0,0},{2,4,5,6,8},{3,6,9,0,0},
16                {4,5,7,8,0},{7,8,9,0,0},{5,6,8,9,0},
17               };
18 int a[10],ans[10];
19 bool flag;
20 
21 bool judge()
22 {
23     int i;
24     for(i=1;i<10;i++)
25         if(a[i])
26             return false;
27     return true;
28 }
29 
30 void output(int p)//最后不能有空格
31 {
32     int i,j,k;
33     k=p;
34     while(ans[k]==0) k--;
35     for(i=1;i<k;i++)
36     {
37         for(j=0;j<ans[i];j++)
38             printf("%d ",i);
39     }
40     for(j=0;j<ans[k]-1;j++)
41         printf("%d ",k);
42     printf("%d\n",k);
43     flag=true;
44 }
45 
46 void dfs(int p)
47 {
48     if(judge())
49     {
50         output(p);return ;
51     }
52     if(flag||p>9) return ;
53     int i,j;
54     for(i=3;i>=0;i--)
55     {
56         int ta[10];
57         ans[p]=i;
58         memcpy(ta,a,sizeof(a));
59         for(j=0;j<5;j++)
60             if(op[p][j])
61                 a[op[p][j]]=(a[op[p][j]]+i)%4;
62         dfs(p+1);
63         memcpy(a,ta,sizeof(ta));
64     }
65 }
66 
67 int main()
68 {
69     //freopen("clocks.in","r",stdin);
70     //freopen("clocks.out","w",stdout);
71     int i;
72     for(i=1;i<10;i++)
73     {
74         scanf("%d",&a[i]);
75         a[i]=(a[i]/3)%4;
76     }
77     flag=false;
78     dfs(1);
79     return 0;
80 }
posted @ 2012-08-29 10:51  mtry  阅读(440)  评论(0)    收藏  举报