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.

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

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




View Code
/*
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;
}

 






转载于:https://www.cnblogs.com/xxx111/archive/2012/09/22/2698104.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值