The Clocks
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 16797 |
|
Accepted: 6939 |
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
设C为当前操作的时钟状态(取值0~3),Xi为执行movei的次数,ai为movei是否能操作当前时钟,能为1,不能为0。
可得方程:(C + a1*X1 + a2*X2 + ... + a9*X9)mod4 = 0
即 (a1*X1 + a2*X2 + ... + a9*X9)mod4 = (4-C)mod4。
然后高斯消元求解。
-------------------------------
消元的时候如果选择绝对值最大的那一列就会wa,原因思考了一下,
如果我们的数据是
0 0 0
0 0 0
0 0 2
那么选择最大值与不选最大值得出的结果是不一样的。
我们观察他们的最终矩阵
选最大值:
1 1 0 1 0 0 0 0 0 0
0 3 0 0 1 0 1 0 0 0
0 0 3 0 3 3 3 0 0 0
0 0 0 1 0 1 1 0 0 0
0 0 0 0 1 0 0 0 3 0
0 0 0 0 0 3 0 1 0 0
0 0 0 0 0 0 1 0 3 0
0 0 0 0 0 0 0 2 3 2
0 0 0 0 0 0 0 0 3 2
不选最大值:
1 1 0 1 0 0 0 0 0 0
0 1 1 0 0 1 0 0 0 0
0 0 1 3 1 0 0 0 0 0
0 0 0 1 0 1 1 0 0 0
0 0 0 0 3 0 0 0 1 0
0 0 0 0 0 3 0 1 0 0
0 0 0 0 0 0 3 0 1 0
0 0 0 0 0 0 0 3 1 0
0 0 0 0 0 0 0 0 3 2
可以发现不选最大值的矩阵中A[i][i]刚好都为1或3,而选最大值的矩阵中出现了一个A[7][7]=2。
可这道题所取的模为4,这样的话,由于我们消元与最终求解时都需要用到A[i][i],所以当有A[i][i]=2时我们并不能保证最终结果是正确的。
因为已知p是素数时,模p剩余系里每个除0以外的元素都有唯一的逆,但p不为素数时,比如这道题的模4,2就没有逆。
所以其实选最大值时跳过2也可以过。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include