## Enumeration dial clock problem

Cheng finally learned algorithm set up. The first chapter of life problem-solving report - although this problem is relatively simple. WA but still many times.

## topic:

Description
9 clock, arranged in a matrix of 3 * 3.
Now required with a minimum of movement of the pointer 9 clocks are appropriated for the 12 o'clock position. There were 9 different allows movement. As shown in Table, each mobile will be a number of clock dial pointer 90 degrees in the clockwise direction.
Effect of the clock movement
. 1 ABDE
2 the ABC
. 3 BCEF
. 5 BDEFH
. 6 the CFI
. 7 DEGH
. 8 GHI
. 9 EFHI
input
9 integers, representing the starting position of each clock hands, separated by a single space between the adjacent two integers. Where 0 = 12, 1 = 3:00, 6:00 = 2, 3 = 9.
Output
output a shortest travel pattern, so that the pointer points to the ninth clock 12:00. From small to large number of outputs in accordance with the movement. Separated by a single space between two adjacent integers.
Sample input
. 3. 3 0
2 2 2
2 2. 1
Sample Output
4589

## analysis:

This question is used to enumerate, the implied condition that each program take no more than four times the number (or restore the original state). But be careful enumeration order. On the first row of a table command sequence, occurs with A, A does not appear in B appear together, and so on. A triple loop occurs after the completion of enumeration, A will certainly point to 12, otherwise continue; when all appears to enumerate the complete B, B will certainly point to 12, otherwise continue, and so on.
The first point of note is back, that is, when the inner loop jump from the outer loop, we want to re-set the clock dial (or write recovery), because the program has changed the previous step, we must restore the clock to the state state before the step program.
Note that the second point is that all the operations do not necessarily add up to less than A containing 4 times. But I do not know how to prove or example ...... it should be because it also affects the back of the clock, but always think this is too much talking, I started to add up ...... this assumption, then crazy WA. Pruning ...... but I had wanted to get rid of this condition with a total program time will 16ms. (Exposed complexity miscalculated ...... sweat, at first thought it would time out, but Guo God, he said, the search can not be treated as the complexity of the entire process with the greatest complexity)
Overall, this is the question a no skills have no difficulty violence issues, forget too complicated.

## AC Code

``````#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int oriclock[9];
int cclock[9];
int methods[9];
int result[9];
void clockwise(int method, int t) {
methods[method] = t;
for (int j = 0; j < movement[method].length(); ++j)
cclock[int(movement[method][j] - 'A')] = (cclock[int(movement[method][j] - 'A')] + t) % 4;
}
int main() {
for (int i = 0; i < 9; ++i) {
cin >> oriclock[i];
}
int minimum = 50;
for (int a = 0; a < 4; ++a) {
for (int b = 0; b < 4; ++b) {
for (int d = 0; d < 4; ++d) {
copy(oriclock, oriclock + 9, cclock);
if ((cclock[0] + a + b + d) % 4 != 0) continue; //假设不成立，不一定这三个操作加起来数目小于4
clockwise(0,a);
clockwise(1,b);
clockwise(3,d);
for (int c = 0; c < 4; ++c) {
for (int e = 0; e < 4; ++e) {
copy(oriclock, oriclock + 9, cclock);
clockwise(0,a);
clockwise(1,b);
clockwise(3,d);
if ((cclock[1] + c + e) % 4 != 0) continue;
clockwise(2,c);
clockwise(4,e);
for (int f = 0; f < 4; ++f) {
copy(oriclock, oriclock + 9, cclock);
clockwise(0,a);
clockwise(1,b);
clockwise(3,d);
clockwise(2,c);
clockwise(4,e);
if((cclock[2] + f) % 4 != 0) continue;
clockwise(5,f);
for (int g = 0; g < 4; ++g) {
copy(oriclock, oriclock + 9, cclock);
clockwise(0,a);
clockwise(1,b);
clockwise(3,d);
clockwise(2,c);
clockwise(4,e);
clockwise(5,f);
if ((cclock[3] + g) % 4 != 0) continue;
clockwise(6,g);
for (int i = 0; i < 4; ++i) {
copy(oriclock, oriclock + 9, cclock);
clockwise(0,a);
clockwise(1,b);
clockwise(3,d);
clockwise(2,c);
clockwise(4,e);
clockwise(5,f);
clockwise(6,g);
if ((cclock[4] + i) % 4 != 0) continue;
if ((cclock[5] + i) % 4 != 0) continue;
clockwise(8,i);
for (int h = 0; h < 4; ++h) {
if ((cclock[6] + h) % 4 != 0) continue;
if ((cclock[7] + h) % 4 != 0) continue;
if ((cclock[8] + h) % 4 != 0) continue;
clockwise(7,h);
int movenum = a + b + c + d + e + f + g + h + i;
if (movenum < minimum) {
minimum = movenum;
copy(methods, methods + 9, result);
}
}
}
}
}
}
}
}
}
}
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < result[i]; ++j) {
cout << i + 1;
if (j < result[i] - 1) cout << " ";
}
if (i < 8 && result[i]) cout << " ";
}
return 0;
}
``````

Write long, not simple and beautiful, more violent ...... repeat things too much. At first thought it would be crazy overtime, Khan ...... complexity still have to be considered carefully ah. But the idea of ​​such relatively simple.

### Guess you like

Origin blog.csdn.net/weixin_44288817/article/details/90108641
Recommended
Ranking
Daily