The Clocks
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 Source |
网上大神们都是用的高斯消元,诶,宝宝不会,用了暴力
ac代码:
#include <stdio.h>
int vis[10],num[10];
int main()
{
int i,j,sum;
int i1,i2,i3,i4,i5,i6,i7,i8,i9;
int j1,j2,j3,j4,j5,j6,j7,j8,j9;
for(i=1;i<10;i++)
scanf("%d",&vis[i]);
for(i1=0;i1<4;i1++)
{
for(i2=0;i2<4;i2++)
{
for(i3=0;i3<4;i3++)
{
for(i4=0;i4<4;i4++)
{
for(i5=0;i5<4;i5++)
{
for(i6=0;i6<4;i6++)
{
for(i7=0;i7<4;i7++)
{
for(i8=0;i8<4;i8++)
{
for(i9=0;i9<4;i9++)
{
num[1]=(vis[1]+i1+i2+i4)%4;//A
num[2]=(vis[2]+i1+i2+i3+i5)%4;//B
num[3]=(vis[3]+i2+i3+i6)%4;//C
num[4]=(vis[4]+i1+i4+i5+i7)%4;//D
num[5]=(vis[5]+i1+i3+i5+i7+i9)%4;//E
num[6]=(vis[6]+i3+i5+i6+i9)%4;//F
num[7]=(vis[7]+i4+i7+i8)%4;//G
num[8]=(vis[8]+i5+i7+i8+i9)%4;//H
num[9]=(vis[9]+i6+i8+i9)%4;//I
sum=0;
for(i=1;i<10;i++)
sum+=num[i];
if(sum==0)
{
for(j1=1;j1<=i1;j1++)
printf("1 ");
for(j2=1;j2<=i2;j2++)
printf("2 ");
for(j3=1;j3<=i3;j3++)
printf("3 ");
for(j4=1;j4<=i4;j4++)
printf("4 ");
for(j5=1;j5<=i5;j5++)
printf("5 ");
for(j6=1;j6<=i6;j6++)
printf("6 ");
for(j7=1;j7<=i7;j7++)
printf("7 ");
for(j8=1;j8<=i8;j8++)
printf("8 ");
for(j9=1;j9<=i9;j9++)
printf("9 ");
}
}
}
}
}
}
}
}
}
}
printf("\n");
return 0;
}
不过既然脑残就要多学习,我在这里http://blog.csdn.net/lin375691011/article/details/38412779 留个学习的链接