Poj1166 the clock Link
About this ~~
The number of 0-4 pointers to 9 clocks,
9 States for Loop
Traverse each status
Each operation will not exceed four times. After four times, it will start again.
Post Code ~~
Post code:
# Include <stdio. h> # include <string. h> int clock [10]; int original [10]; int rec [10]; char ch [10] [20] = {"ABDE", "ABC ", "BCEF", "ADG", "BDEFH", "CFI", "DEGH", "GHI", "EFHI"}; void change (int k, int num) {int I, j, len; k = K-1; len = strlen (ch [k]); for (I = 1; I <= num; I ++) {for (j = 0; j <len; j ++) clock [ch [k] [j]-'A' + 1] = (clock [ch [k] [j]-'A' + 1] + 1) % 4 ;}} void ori () {int I; for (I = 1; I <= 9; I ++) clock [I] = original [I];} int fun (int a, int B, int c, Int d, int e, int f, int g, int h, int I) {int k; change (1, a); change (2, B); change (3, c); change (4, d); change (5, e); change (6, f); change (7, g); change (8, h ); change (9, I); int sum = 0; for (k = 1; k <= 9; k ++) sum + = clock [k]; if (sum = 0) return 1; else return 0;} int main () {while (scanf ("% d", & clock [1])! = EOF) {int a, B, c, d, e, f, g, h, I, sum = 0, min = 999999; original [1] = clock [1]; for (I = 2; I <= 9; I ++) {scanf ("% d", & clock [I]); original [I] = clock [I];} // for (I = 1; I <= 9; I ++) // printf ("% d ", clock [I]); // printf ("\ n"); for (a = 0; a <4; a ++) {// 9-heavy for loop Enumeration The number of times of each operation for (B = 0; B <4; B ++) {for (c = 0; c <4; c ++) {for (d = 0; d <4; d ++) {for (e = 0; e <4; e ++) {for (f = 0; f <4; f ++) {for (g = 0; g <4; g ++) {for (h = 0; h <4; h ++) {for (I = 0; I <4; I ++) {sum = a + B + c + d + e + f + g + h + I; If (min <sum) continue; ori (); if (fun (a, B, c, d, e, f, g, h, I) = 1) {if (min> sum) {min = sum; rec [1] = a; rec [2] = B; rec [3] = c; rec [4] = d; rec [5] = e; rec [6] = f; rec [7] = g; rec [8] = h; rec [9] = I ;}}}}}}}} int x; for (x = 1; x <= 9; x ++) if (rec [x]! = 0) {rec [x] --; printf ("% d", x); break;} x = 1; while (x! = 10) {while (rec [x]! = 0) {printf ("% d", x); rec [x] --;} x ++;} printf ("\ n ");}}