USACO Section 1.4 The Clocks(DFS)
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
/* ID:xxx111_1 LANG:C TASK:clocks */ #include<stdio.h> #include<stdlib.h> #include<string.h> int a[4][4],b[100],ans,num,c,flag,t; void A(int p) { a[1][1]=(a[1][1]+p)%4; } void B(int p) { a[1][2]=(a[1][2]+p)%4; } void C(int p) { a[1][3]=(a[1][3]+p)%4; } void D(int p) { a[2][1]=(a[2][1]+p)%4; } void E(int p) { a[2][2]=(a[2][2]+p)%4; } void F(int p) { a[2][3]=(a[2][3]+p)%4; } void G(int p) { a[3][1]=(a[3][1]+p)%4; } void H(int p) { a[3][2]=(a[3][2]+p)%4; } void I(int p) { a[3][3]=(a[3][3]+p)%4; } int step(int x,int p) { int i; for (i=1; i<=p ; i++) { num++; b[num]=x; } if (x==1) { A(p); B(p); D(p); E(p); } if (x==2) { A(p); B(p); C(p); } if (x==3) { B(p); C(p); E(p); F(p); } if (x==4) { A(p); D(p); G(p); } if (x==5) { B(p); D(p); E(p); F(p); H(p); } if (x==6) { C(p); F(p); I(p); } if (x==7) { D(p); E(p); G(p); H(p); } if (x==8) { G(p); H(p); I(p); } if (x==9) { E(p); F(p); H(p); I(p); } } void check() { int i,j; for (i=1; i<=3; i++) for (j=1; j<=3; j++) if (a[i][j]!=0) return; flag=1; return; } void dfs(int x) { int i,j,c; check(); if (flag==1 ) { for (i=1; i<num; i++) printf("%d ",b[i]); printf("%d\n",b[num]); fclose(stdin); fclose(stdout); exit(0); } /*printf("!!!%d %d\n",x,flag); for (j=1; j<=3; j++) { printf("%d %d %d \n",a[j][1],a[j][2],a[j][3]); } printf("\n"); scanf("%d",&c);*/ if ( (x==10) || (flag==1) ) return; for (i=0; i<=3; i++) { int tmp[4][4]; memcpy(tmp,a,sizeof(a)); step(x,i); dfs(x+1); memcpy(a,tmp,sizeof(tmp)); num=num-i; } } int main() { // freopen("clocks.in","r",stdin); // freopen("clocks.out","w",stdout); int i; for (i=1; i<=3; i++) { scanf("%d %d %d",&a[i][1],&a[i][2],&a[i][3]); a[i][1]=(a[i][1]/3)%4; a[i][2]=(a[i][2]/3)%4; a[i][3]=(a[i][3]/3)%4; } num=0; flag=0; dfs(1); return 0; }