The Clocks
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 16232 | Accepted: 6638 |
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 thedials(刻度) to 12 o'clock with as few moves aspossible. There are nine different allowed ways to turn the dials on theclocks. 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 whichare 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 toread 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 towrite 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 isunique.
Sample Input
3 3 0
2 2 2
2 1 2
Sample Output
4 5 8 9
Source
题目大意:给出9个时钟初始的时间,要求全部调到12点,有9种操作,问应该怎么操作。
每种操作只能出现0次、1次、2次、3次,而时钟需要调的次数也是给定的,直接列方程组,高斯消元
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
char str[9][10] = { "ABDE","ABC","BCEF","ADG","BDEFH","CFI","DEGH","GHI","EFHI" } ;
int Map[10][10] , a[10] , ans[10] ;
void solve() {
int i , j , k , l , temp , sum ;
for(i = 0 , k = 0 ; i < 9 , k < 9 ; i++ , k++) {
for(j = i ; j < 9 ; j++) {
if( Map[j][k] ) break ;
}
if( j >= 9 ) {
i-- ;
continue ;
}
if( i != j ) {
for(l = k ; l < 9 ; l++)
swap(Map[i][l],Map[j][l]) ;
swap(a[i],a[j]) ;
}
for(j = i+1 ; j < 9 ; j++) {
if( !Map[j][k] ) continue ;
temp = Map[j][k] ;
for(l = k ; l < 9 ; l++) {
Map[j][l] = Map[i][l]*temp - Map[j][l]*Map[i][k] ;
Map[j][l] = (Map[j][l]%4+4)%4 ;
}
a[j] = a[i]*temp - a[j]*Map[i][k] ;
a[j] = (a[j]%4+4)%4 ;
}
}
sum = 0 ;
for(i = 8 ; i >= 0 ; i--) {
for(j = i+1 ; j < 9 ; j++) {
a[i] -= ans[j]*Map[i][j] ;
a[i] = (a[i]%4+4)%4 ;
}
for(j = 0 ; j < 4 ; j++)
if( Map[i][i]*j%4 == a[i] )
ans[i] = j , sum += j ;
}
for(i = 0 ; i < 9 ; i++) {
while( ans[i]-- ) {
sum-- ;
if( sum )
printf("%d ", i+1) ;
else
printf("%d\n", i+1) ;
}
}
return ;
}
int main() {
int i , j , l ;
memset(Map,0,sizeof(Map)) ;
for(i = 0 ; i < 9 ; i++) {
l = strlen(str[i]) ;
for(j = 0 ; j < l ; j++) {
Map[str[i][j]-'A'][i] = 1 ;
}
}
for(i = 0 ; i < 3 ; i++) {
for(j = 0 ; j < 3 ; j++) {
scanf("%d", &a[i*3+j]) ;
a[i*3+j] = (4-a[i*3+j])%4 ;
}
}
solve() ;
return 0 ;
}