11 public BufferedReader f
;
13 public PrintWriter out
;
17 public IO () throws IOException
{
18 f
= new BufferedReader(new FileReader(name
+ ".in"));
19 out
= new PrintWriter(new BufferedWriter(new FileWriter(name
+ ".out")));
22 public String
nextLine() throws IOException
{
28 public int nextIntLine() throws IOException
{
29 return Integer
.parseInt(nextLine());
32 public void tokenize() {
33 st
= new StringTokenizer(line
);
36 public String
nextToken() {
37 if(st
== null) tokenize();
38 return st
.nextToken();
41 public int nextIntToken() {
42 return Integer
.parseInt(nextToken());
46 out
.close(); // close the output file
47 System
.exit(0); // don't omit this!
51 static boolean debug
= false;
52 static String name
= "clocks";
56 //////////////////////////////////////////
58 static final byte[][] moves
= {
70 static State state_pool
= null;
76 public Move(Move pre
, int val
) {
83 io
.out
.print(val
+ 1);
86 io
.out
.printf(" %d", val
+ 1);
90 public void sys_print() {
92 System
.out
.print(val
+ 1);
95 System
.out
.printf(" %d", val
+ 1);
102 public int iter
; // move iteration
103 public int[] state
= null;
105 public State next
= null; // chain pointer
107 public State(Move move
, int iter
) {
113 for(int i
= 0; i
< 9; i
++){
114 if(state
[i
] != 0) return;
124 static State
genState(Move move
, int mid
, int iter
, State first
, State last
) {
127 if(state_pool
!= null){
129 state_pool
= st
.next
;
132 st
.move
= new Move(move
, mid
);
136 for(int i
= 0; i
< 9; i
++){
137 st
.state
[i
] = first
.state
[i
];
140 st
= new State(new Move(move
, mid
), iter
);
141 st
.state
= first
.state
.clone();
145 for(int i
= moves
[mid
].length
- 1; i
>= 0; i
--){
146 final int id
= moves
[mid
][i
];
147 st
.state
[id
] = (st
.state
[id
] + 1) % 4;
152 last
.next
= st
; // don't forget to append the state
157 static State
poolState(State st
){
160 st
.next
= state_pool
;
166 public static void main (String
[] args
) throws IOException
{
171 first
= last
= new State(null, 0);
173 first
.state
= new int [9];
175 for(int i
= 0; i
< 9; i
++){
176 if(i
% 3 == 0) io
.nextLine();
177 first
.state
[i
] = io
.nextIntToken() / 3 % 4;
182 for(int id
= 0; id
< 9; id
++){
183 last
= genState(null, id
, 0, first
, last
);
188 for(int iii
= 0; iii
< 1000000; iii
++){
189 final Move move
= first
.move
;
190 final int mid
= move
.val
;
191 final int iter
= first
.iter
;
194 last
= genState(move
, mid
, iter
+ 1, first
, last
);
197 for(int id
= mid
+ 1; id
< 9; id
++){
198 last
= genState(move
, id
, 0, first
, last
);
201 first
= poolState(first
);
204 // io.out.println(min_area);