http://poj.org/problem?id=1166
The Clocks
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12385 | Accepted: 4898 |
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
//暴力求解法用num[i]示按第i中方式波动钟表
//用num[i]表示按第i中方式涉及的钟表
#include<stdio.h>
#include<string.h>
int ans[10],num[10],tmp[10];
int main()
{
int i;
for(i=1;i<=9;i++)
scanf("%d",&ans[i]);
for(num[1]=0;num[1]<=3;num[1]++)//每一种操作口令的执行次数都不会超过3次
for(num[2]=0;num[2]<=3;num[2]++)
for(num[3]=0;num[3]<=3;num[3]++)
for(num[4]=0;num[4]<=3;num[4]++)
for(num[5]=0;num[5]<=3;num[5]++)
for(num[6]=0;num[6]<=3;num[6]++)
for(num[7]=0;num[7]<=3;num[7]++)
for(num[8]=0;num[8]<=3;num[8]++)
for(num[9]=0;num[9]<=3;num[9]++)
{
tmp[1]=(ans[1]+num[1]+num[2]+num[4])%4;//影响钟表1的操作口令有1,2,4,下面的同理
tmp[2]=(ans[2]+num[1]+num[2]+num[3]+num[5])%4;
tmp[3]=(ans[3]+num[2]+num[3]+num[6])%4;
tmp[4]=(ans[4]+num[1]+num[4]+num[5]+num[7])%4;
tmp[5]=(ans[5]+num[1]+num[3]+num[5]+num[7]+num[9])%4;
tmp[6]=(ans[6]+num[3]+num[5]+num[6]+num[9])%4;
tmp[7]=(ans[7]+num[4]+num[7]+num[8])%4;
tmp[8]=(ans[8]+num[5]+num[7]+num[8]+num[9])%4;
tmp[9]=(ans[9]+num[6]+num[8]+num[9])%4;
if(tmp[1]+tmp[2]+tmp[3]+tmp[4]+tmp[5]+tmp[6]+tmp[7]+tmp[7]+tmp[8]+tmp[9]==0)
{
for(i=1;i<=num[1];i++)
printf("1 ");
for(i=1;i<=num[2];i++)
printf("2 ");
for(i=1;i<=num[3];i++)
printf("3 ");
for(i=1;i<=num[4];i++)
printf("4 ");
for(i=1;i<=num[5];i++)
printf("5 ");
for(i=1;i<=num[6];i++)
printf("6 ");
for(i=1;i<=num[7];i++)
printf("7 ");
for(i=1;i<=num[8];i++)
printf("8 ");
for(i=1;i<=num[9];i++)
printf("9 ");
printf("\n");
return 0;
}
}
}
//用num[i]表示按第i中方式涉及的钟表
#include<stdio.h>
#include<string.h>
int ans[10],num[10],tmp[10];
int main()
{
}