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
枚举。注意每种移动有可能做多次,但是不会超过4次,因为到了4次,就会回到原点,无意义。
用C语言实现的,调试了多次才过,原来POJ编译器C语言声明变量要放在程序开头,还有这个程序9层循环也是厉害。
#include<stdio.h>
int main(){
int i;
int l1,l2,l3,l4,l5,l6,l7,l8,l9;
int mincount = 99999;
int nums[9];
int contain[10] = {0};
for(i = 0; i < 9; i ++){
scanf("%d", &nums[i]);
}
for(l1 = 0; l1 < 4; l1 ++){
for(l2 = 0; l2 < 4; l2 ++){
for(l3 = 0; l3 < 4; l3 ++){
for(l4 = 0; l4 < 4; l4 ++){
for(l5 = 0; l5 < 4; l5 ++){
for(l6 = 0; l6 < 4; l6 ++){
for(l7 = 0; l7 < 4; l7 ++){
for(l8 = 0; l8 < 4; l8 ++){
for(l9 = 0; l9 < 4; l9 ++){
if((nums[0] + l1 + l2 + l4) % 4 == 0 && (nums[1] + l1 + l2 + l3 + l5) % 4 == 0
&& (nums[2] + l2 + l3 + l6) % 4 == 0 && (nums[3] + l1 + l4 + l5 + l7) % 4 == 0
&& (nums[4] + l1 + l3 + l5 + l7 + l9) %4 == 0 && (nums[5] + l3 + l5 + l6 + l9) % 4 == 0
&& (nums[6] + l4 + l7 + l8) % 4 == 0 && (nums[7] + l5 + l7 + l8 + l9) % 4 == 0
&& (nums[8] + l6 + l8 + l9) % 4 == 0){
int count = l1 + l2 + l3 + l4 + l5 + l6 + l7 + l8 + l9;
if(count < mincount){
mincount = count;
contain[1] = l1;
contain[2] = l2;
contain[3] = l3;
contain[4] = l4;
contain[5] = l5;
contain[6] = l6;
contain[7] = l7;
contain[8] = l8;
contain[9] = l9;
}
}
}
}
}
}
}
}
}
}
}
for(i = 1; i < 10; i ++){
while(contain[i] --) printf("%d ", i);
}
printf("\n");
}