Usaco 1.4.2 -- clock

Source: Internet
Author: User

Finally, usaco is returned.

Translation:
Description

Consider arranging nine clocks in a 3x3 column:

| ------- |
|
| --- O |
|
| ------- |
A B C

| ------- |
|
| O |
|
| ------- |
D E F

| ------- |
|
| O --- | O |
|
| ------- |
G H I

The target needs to find a minimum moving order to point all the pointers to 12 points. The following table lists nine different methods for rotating pointers. Each method is called a moving operation. By moving the number 1 to 9, the pointer of the corresponding clock in the table is rotated 90 degrees clockwise.

Clock affected by the moving Method
1 ABDE
2 ABC
3 BCEF
4 ADG
5 BDEFH
6 CFI
7 DEGH
8 GHI
9 EFHI

Example

9 9 12 9 12 12 9 12 12 12 12 12 12 12
6 6 6 5-> 9 9 9 8-> 9 9 9 4-> 12 9 9 9-> 12 12 12
6 3 6 6 6 6 9 9 9 12 9 12 12

[But this may not be the correct method. Please refer to the following]

Format

Program name: clocks

Input format:

(File clocks. in)

Line 1-3: numbers separated by spaces. Each digit indicates the initial time of a clock, which is 3, 6, 9, or 12. The meaning of a number is the same as that in the previous example.

Output format:

(File clocks. out)

A single row contains a list of the shortest moving orders that point all pointers to separated by spaces.

If there are multiple solutions, output the one that makes it connect to the smallest number. (For example, 5 2 4 6 <9 3 1 1 ).

SAMPLE INPUT

9 9 12
6 6 6
6 3 6

SAMPLE OUTPUT

4 5 8 9

Analysis:
Use 0, 1, 2, and 3 to indicate the clock status, indicating, respectively.
Repeated results are generated after four operations, so 0 ~ 3,
4 ^ 9 is also very limited,
The status is small, and the impact of operations is known and fixed.
Therefore
Violent search... That's it...
(This question has plagued me for a long time)

View Code

1 {
2 ID: codeway3
3 PROG: clocks
4 LANG: PASCAL
5}
6. program clocks;
7 type
8 ll = array [0 .. 9] of longint;
9 var
10 I, i1, i2, i3, i4, i5, i6, i7, i8, i9, j, n, m, k, l: longint; // I series are cyclic Variables
11 a, B, c, d, e: ll;
12
13 procedure cz; // operation
14 var
15 I, j: longint;
16 begin // adjust the clock affected by each operation
17 I: = i1;
18 while I> 0 do begin dec (I); inc (B [1]); inc (B [2]); inc (B [4]); inc (B [5]); end;
19 I: = i2;
20 while I> 0 do begin dec (I); inc (B [1]); inc (B [2]); inc (B [3]); end;
21 I: = i3;
22 while I> 0 do begin dec (I); inc (B [2]); inc (B [3]); inc (B [5]); inc (B [6]); end;
23 I: = i4;
24 while I> 0 do begin dec (I); inc (B [1]); inc (B [4]); inc (B [7]); end;
25 I: = i5;
26 while I> 0 do begin dec (I); inc (B [2]); inc (B [4]); inc (B [5]); inc (B [6]); inc (B [8]); end;
27 I: = i6;
28 while I> 0 do begin dec (I); inc (B [3]); inc (B [6]); inc (B [9]); end;
29 I: = i7;
30 while I> 0 do begin dec (I); inc (B [4]); inc (B [5]); inc (B [7]); inc (B [8]); end;
31 I: = i8;
32 while I> 0 do begin dec (I); inc (B [7]); inc (B [8]); inc (B [9]); end;
33 I: = i9;
34 while I> 0 do begin dec (I); inc (B [5]); inc (B [6]); inc (B [8]); inc (B [9]); end;
35 end; // be careful and never make a mistake.
36
37 procedure print; // output
38 var
39 I, j: Longint;
40 begin // e stores the number of operations per operation
41 j: = 0;
42 for I: = 1 to 9 do
43 while e [I]> 0 do begin
44 if j = 1 then write ('');
45 dec (e [I]);
46 write (I );
47 j: = 1;
48 end;
49 writeln;
50 end;
51
52 procedure doing;
53 var
54 I, j: longint;
55 begin
56 fillchar (B, sizeof (B), 0 );
57 fillchar (d, sizeof (d), 0); // clear the temporary array, d save the new state, and B Save the status changes generated by the Temporary Operation
58 cz;
59 for I: = 1 to 9 do d [I]: = a [I] + B [I]; // a is in the original state and remains unchanged. Use d for comparison.
60 for I: = 1 to 9 do d [I]: = d [I] mod 4;
61 j: = 0;
62 for I: = 1 to 9 do if c [I] <> d [I] then j: = 1; // The c array is a standard status array.
63 if j = 0 then begin
64 l: = i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9;
65 e [1]: = i1;
66 e [2]: = i2;
67 e [3]: = i3;
68 e [4]: = i4;
69 e [5]: = i5;
70 e [6]: = i6;
71 e [7]: = i7;
72 e [8]: = i8;
73 e [9]: = i9; // The state of the temporary Enumeration
74 end;
75 end;
76
77 begin
78 assign (input, 'Clocks. in ');
79 reset (input );
80 assign (output, 'Clocks. out ');
81 rewrite (output );
82 for I: = 1 to 9 do begin read (a [I]); a [I]: = a [I] div 3; end;
83 l: = maxlongint;
84 for i9: = 0 to 3 do
85 for i8: = 0 to 3 do
86 for i7: = 0 to 3 do
87 for i6: = 0 to 3 do
88 for i5: = 0 to 3 do
89 for i4: = 0 to 3 do
90 for i3: = 0 to 3 do
91 for i2: = 0 to 3 do
92 for i1: = 0 to 3 do
93 if i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9 <l then doing; // forcefully enumerate and Process
94 print;
95 close (input );
96 close (output );
97 end.

Two errors,
For the first time, I forgot to open the input/output file)
The second time is to forget to pay attention to spaces. At the end of the line, a space is output, which is tragic. Pay attention to it ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.