IOI'94 - Day 2
|-------| |-------| |-------| | | | | | | | |---O | |---O | | O | | | | | | | |-------| |-------| |-------| A B C |-------| |-------| |-------| | | | | | | | O | | O | | O | | | | | | | | | | |-------| |-------| |-------| D E F |-------| |-------| |-------| | | | | | | | O | | O---| | O | | | | | | | | | |-------| |-------| |-------| G H I
The goal is to find a minimal sequence of moves to return all the dials to 12 o'clock. Nine different ways to turn the dials on the clocks are supplied via a table below; each way is called a move. Select for each move a number 1 through 9 which will cause the dials of the affected clocks (see next table) to be turned 90 degrees clockwise.
Move | Affected clocks |
1 | ABDE |
2 | ABC |
3 | BCEF |
4 | ADG |
5 | BDEFH |
6 | CFI |
7 | DEGH |
8 | GHI |
9 | EFHI |
Example
Each number represents a time accoring to following table:9 9 12 9 12 12 9 12 12 12 12 12 12 12 12 6 6 6 5 -> 9 9 9 8-> 9 9 9 4 -> 12 9 9 9-> 12 12 12 6 3 6 6 6 6 9 9 9 12 9 9 12 12 12
[But this might or might not be the `correct' answer; see below.]
PROGRAM NAME: clocks
INPUT FORMAT
Lines 1-3: | Three lines of three space-separated numbers; each number represents the start time of one clock, 3, 6, 9, or 12. The ordering of the numbers corresponds to the first example above. |
SAMPLE INPUT (file clocks.in)
9 9 12 6 6 6 6 3 6
OUTPUT FORMAT
A single line that contains a space separated list of the shortest sequence of moves (designated by numbers) which returns all the clocks to 12:00. If there is more than one solution, print the one which gives the lowest number when the moves are concatenated (e.g., 5 2 4 6 < 9 3 1 1).
SAMPLE OUTPUT (file clocks.out)
4 5 8 9
妈的原来是标题写错了。。。是clocks!!有个s握艹
/*
ID: des_jas1
PROG: clocks
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
//#define fin cin
//#define fout cout
using namespace std;
const int MAX=10000;
int N=MAX,used[10],cl[9],step[MAX],Path[MAX];
void Move(int p)
{
switch(p)
{
case 1:
{
cl[0]=(cl[0]+3)%4;cl[1]=(cl[1]+3)%4;cl[3]=(cl[3]+3)%4;cl[4]=(cl[4]+3)%4;
break;
}
case 2:
{
cl[0]=(cl[0]+3)%4;cl[1]=(cl[1]+3)%4;cl[2]=(cl[2]+3)%4;
break;
}
case 3:
{
cl[1]=(cl[1]+3)%4;cl[2]=(cl[2]+3)%4;cl[5]=(cl[5]+3)%4;cl[4]=(cl[4]+3)%4;
break;
}
case 4:
{
cl[0]=(cl[0]+3)%4;cl[6]=(cl[6]+3)%4;cl[3]=(cl[3]+3)%4;
break;
}
case 5:
{
cl[5]=(cl[5]+3)%4;cl[1]=(cl[1]+3)%4;cl[3]=(cl[3]+3)%4;cl[4]=(cl[4]+3)%4;cl[7]=(cl[7]+3)%4;
break;
}
case 6:
{
cl[2]=(cl[2]+3)%4;cl[5]=(cl[5]+3)%4;cl[8]=(cl[8]+3)%4;
break;
}
case 7:
{
cl[6]=(cl[6]+3)%4;cl[7]=(cl[7]+3)%4;cl[3]=(cl[3]+3)%4;cl[4]=(cl[4]+3)%4;
break;
}
case 8:
{
cl[6]=(cl[6]+3)%4;cl[7]=(cl[7]+3)%4;cl[8]=(cl[8]+3)%4;
break;
}
case 9:
{
cl[5]=(cl[5]+3)%4;cl[4]=(cl[4]+3)%4;cl[7]=(cl[7]+3)%4;cl[8]=(cl[8]+3)%4;
break;
}
}
}
bool IS_End()
{
int i;
for(i=0;i<9;i++)
if(cl[i])
return false;
return true;
}
void Trans(int n)
{
if(n>=N) //可以“=”因为是升序
return;
int i;
if(IS_End())
{
N=n;
for(i=0;i<N;i++)
Path[i]=step[i];
}
else
{
if(!n)
i=1;
else
i=step[n-1];
for(;i<=9;i++)
{
if(used[i]<3)
{
int j;
Move(i);
step[n]=i;
used[i]++;
Trans(n+1);
used[i]--;//老忘
for(j=0;j<3;j++) //移动4次相当于没移动
Move(i);
}
}
}
return;
}
int main()
{
ofstream fout ("clocks.out");
ifstream fin ("clocks.in");
int i,tp;
for(i=0;i<9;i++)
{
fin>>tp;
cl[i]=(12-tp)/3;
}
memset(used,0,sizeof(used));
memset(step,1,sizeof(step));
memset(Path,0,sizeof(Path));
Trans(0);
fout<<Path[0];
for(i=1;i<N;i++)
fout<<" "<<Path[i];
fout<<endl;
fout.close();
fin.close();
return 0;
}