poj 1166 拨钟问题

Description

|-------|    |-------|    |-------|

|       |    |       |    |   |   |

|---O   |    |---O   |    |   O   |

|       |    |       |    |       |

|-------|    |-------|    |-------|

    A            B            C    



|-------|    |-------|    |-------|

|       |    |       |    |       |

|   O   |    |   O   |    |   O   |

|   |   |    |   |   |    |   |   |

|-------|    |-------|    |-------|

    D            E            F    



|-------|    |-------|    |-------|

|       |    |       |    |       |

|   O   |    |   O---|    |   O   |

|   |   |    |       |    |   |   |

|-------|    |-------|    |-------|

    G            H            I    

(Figure 1)

There are nine clocks in a 3*3 array (figure 1). The goal is to return all the dials to 12 o'clock with as few moves as possible. There are nine different allowed ways to turn the dials on the clocks. Each such way is called a move. Select for each move a number 1 to 9. That number will turn the dials 90' (degrees) clockwise on those clocks which are affected according to figure 2 below. 
Move   Affected clocks

 

 1         ABDE

 2         ABC

 3         BCEF

 4         ADG

 5         BDEFH

 6         CFI

 7         DEGH

 8         GHI

 9         EFHI    

   (Figure 2)

Input

Your program is to read from standard input. Nine numbers give the start positions of the dials. 0=12 o'clock, 1=3 o'clock, 2=6 o'clock, 3=9 o'clock.

Output

Your program is to write to standard output. Output a shortest sorted sequence of moves (numbers), which returns all the dials to 12 o'clock. You are convinced that the answer is unique.

Sample Input

3 3 0
2 2 2
2 1 2

Sample Output

4 5 8 9

思路:还是和熄灯的那个差不多,只不过这个麻烦了一点。用两个数组表示钟的状态和操作,枚举操作1,2,3,剩下的操作中只有4可以改变A,5可以改变B,6可以改变C,所以操作4,5,6可以推出来。操作7由D的状态推出,8由G推出,9从EFHI中任一个都可以,我用的是I,然后再验证剩下的三个钟是否合法即可。


代码:

#include <stdio.h>
#include <stdlib.h>


int main()
{
    int i,success=0,first=1,clock[10],move[10]={0};
    for(i=1;i<10;i++)
        scanf("%d",&clock[i]);
    while(!success){
        success=1;
        
        move[4]=(4-((clock[1]+move[1]+move[2])%4))%4;
        move[5]=(4-((clock[2]+move[1]+move[2]+move[3])%4))%4;
        move[6]=(4-((clock[3]+move[2]+move[3])%4))%4;
        move[7]=(4-(clock[4]+move[1]+move[4]+move[5])%4)%4;
        move[8]=(4-(clock[7]+move[4]+move[7])%4)%4;
        move[9]=(4-(clock[9]+move[6]+move[8])%4)%4;
        
        if((clock[5]+move[1]+move[3]+move[5]+move[7]+move[9])%4!=0)
            success=0;
        if((clock[6]+move[3]+move[5]+move[6]+move[9])%4!=0)
            success=0;
        if((clock[8]+move[5]+move[7]+move[8]+move[9])%4!=0)
            success=0;
        if(success)
            break;
        
        i=1;                                                //利用三进制模拟操作123的状态
        move[1]++;
        while(move[i]>3&&i<4){
            move[i]=0;
            i++;
            move[i]++;
        }
    }
    
    for(i=1;i<10;i++){
        while(move[i]-->0){
            if(first){
                first=0;
                printf("%d",i);
            }
            else
            printf(" %d",i);
        }
    }
    printf("\n");
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值