链接:http://poj.org/problem?id=1166
题目描述:
The Clocks
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19016 Accepted: 7879
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
IOI 1994
分析:
用a,b,c,…h,i分别表示每个钟的值,然后用x[i] (i从0…8表示编号(i+1)的需要的次数,然后就是列方程,解方程了,手动解这个真费纸,(太惨了,别人直接枚举就O了,好菜啊)
贴下我手动解的几个主要的步骤:
9个等式:
最后结果如下:==
代码:
// #include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
const int N=1e5+10;
#define ll long long
const int mod = 4;
int Time[10];
int x[10];//x[i] (i从0..8表示编号(i+1)的需要的次数
int main()
{
while(~scanf("%d",&Time[0]))
{
for(int i=1;i<9;i++)
scanf("%d",&Time[i]);
int temp=Time[0]-2*Time[1]+Time[2]-2*Time[3] -2*Time[4] +3*Time[5] + Time[6] + 3*Time[7] -4*Time[8];
temp=(temp%mod+mod)%mod;
// cout<<temp<<endl;
for(int i=0;i<4;i++)
{
if(5*i%mod==temp)
{
x[0]=i;
break;
}
}
x[1]=2*x[0]- Time['a'-'a'] + Time['b'-'a']- Time['c'-'a'] +Time['d'-'a']+Time['e'-'a']-Time['f'-'a']-2*Time[ 'h'-'a'] +2*Time['i'-'a'];
x[2]=-4*x[0] +Time['a'-'a'] -2*Time['b'-'a']+Time['c'-'a']-Time['d'-'a']-2*Time['e'-'a']+2*Time['f'-'a']+3*Time['h'-'a']-3*Time['i'-'a'];
x[3]=-3*x[0] -Time['b'-'a']+Time['c'-'a']-Time['d'-'a']-Time['e'-'a']+Time['f'-'a']+2*Time['h'-'a']-2*Time['i'-'a'];
x[4]=x[0]+Time['i'-'a']+Time['e'-'a']-Time['h'-'a']-Time['f'-'a'];
x[5]=2*x[0]+Time['b'-'a']-Time['c'-'a']-Time['h'-'a']+Time['i'-'a']+Time['e'-'a']-Time['f'-'a'];
x[6]=x[0]+Time['b'-'a']-Time['c'-'a']-Time['h'-'a']+Time['i'-'a'];
x[7]=2*x[0]-Time['h'-'a']+Time['i'-'a']+Time['e'-'a']-Time['f'-'a']+Time['d'-'a']-Time['g'-'a'];
x[8]=-4*x[0] -Time['b'-'a']+Time['c'-'a']-Time['d'-'a']-2*Time['e'-'a'] + Time['g'-'a']+2*Time['f'-'a'] +2*Time['h'-'a'] -3*Time['i'-'a'];
// for(int i=0;i<9;i++)
// cout<<x[i]<<" i="<<i<<endl;
for(int i=1;i<9;i++)
{
x[i]=(x[i]%mod+mod)%mod;
}
int flag=0;
for(int i=0;i<9;i++)
{
if(x[i])
{
if(flag==0)
{
printf("%d",i+1);
flag=1;
x[i]--;
}
while(x[i])
{
printf(" %d",i+1);
x[i]--;
}
}
}
printf("\n");
}
return 0;
}