The Clocks
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15357 | Accepted: 6230 |
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,..9表示数字1是拨动ABDE,以此类推,对于一个操作来说,进行4次就直接变回原来没有操作,因此每个操作最多进行4次,对于每个钟,如果拨动超过4次那么就会循环,直接%4就可以了,4^9直接搞起……
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[10],i[10],m[10];
int main()
{
int j,sum;
for(j=1;j<=9;j++)
{
scanf("%d",&m[j]);
}
for(i[1]=0; i[1]<=3; i[1]++)
for(i[2]=0; i[2]<=3; i[2]++)
for(i[3]=0; i[3]<=3; i[3]++)
for(i[4]=0; i[4]<=3; i[4]++)
for(i[5]=0; i[5]<=3; i[5]++)
for(i[6]=0; i[6]<=3; i[6]++)
for(i[7]=0; i[7]<=3; i[7]++)
for(i[8]=0; i[8]<=3; i[8]++)
for(i[9]=0; i[9]<=3; i[9]++)
{
a[1]=(m[1]+i[1]+i[2]+i[4])%4;
a[2]=(m[2]+i[1]+i[2]+i[3]+i[5])%4;
a[3]=(m[3]+i[2]+i[3]+i[6])%4;
a[4]=(m[4]+i[1]+i[4]+i[5]+i[7])%4;
a[5]=(m[5]+i[1]+i[3]+i[5]+i[7]+i[9])%4;
a[6]=(m[6]+i[3]+i[5]+i[6]+i[9])%4;
a[7]=(m[7]+i[4]+i[7]+i[8])%4;
a[8]=(m[8]+i[5]+i[7]+i[8]+i[9])%4;
a[9]=(m[9]+i[6]+i[8]+i[9])%4;
sum=0;
for(j=1;j<=9;j++)
{
sum+=a[j];
}
//printf("sum==%d\n",sum);
if(sum==0)
{
for(j=1;j<=9;j++)
{
while(i[j]--)
{
printf("%d ",j);
}
}
return 0;
}
}
}