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.
代码:枚举
#include<stdio.h>
#include<stdlib.h>
int main()
{
int min = 100000;
int state[10];
int opr[10];
for(int i = 1; i <= 9; i++)
scanf("%d",&state[i]);
for(int opr1 = 0; opr1 <= 3; opr1++)
for(int opr2 = 0; opr2 <= 3; opr2++)
for(int opr3 = 0; opr3 <= 3; opr3++)
for(int opr4 = 0; opr4 <= 3; opr4++)
for(int opr5 = 0; opr5 <= 3; opr5++)
for(int opr6 = 0; opr6 <= 3; opr6++)
for(int opr7 = 0; opr7 <= 3; opr7++)
for(int opr8 = 0; opr8 <= 3; opr8++)
for(int opr9 = 0; opr9 <= 3; opr9++)
if((opr1 + opr2 + opr4 + state[1]) % 4 == 0 && //A = 0
(opr1 + opr2 + opr3 + opr5 + state[2]) % 4 == 0 && //B = 0
(opr2 + opr3 + opr6 + state[3]) % 4 == 0 && //C = 0
(opr1 + opr4 + opr5 + opr7 + state[4]) % 4 == 0 && //D = 0
(opr1 + opr3 + opr5 + opr7 + opr9 + state[5]) % 4 == 0 &&//E = 0
(opr3 + opr5 + opr6 + opr9 + state[6]) % 4 == 0 && //F = 0
(opr4 + opr7 + opr8 + state[7]) % 4 == 0 && //G = 0
(opr5 + opr7 + opr8 + opr9 + state[8]) % 4 == 0 && //H = 0
(opr6 + opr8 + opr9 + state[9]) % 4 == 0)
{
int sum = opr1 + opr2 + opr3 + opr4 + opr5 + opr6 + opr7 + opr8 + opr9;
if(min > sum)
{
min = sum;
opr[1] = opr1;
opr[2] = opr2;
opr[3] = opr3;
opr[4] = opr4;
opr[5] = opr5;
opr[6] = opr6;
opr[7] = opr7;
opr[8] = opr8;
opr[9] = opr9;
}
}
for(int i = 1; i <= 9; i++)
while(opr[i]--)
printf("%d ",i);
printf("\n");
}