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
Output
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;
}