The Clocks
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 15944 |
|
Accepted: 6493 |
Description
|-------| | -------| | -------|
| | | | | | |
|---O | | ---O | | O |
| | | | | |
|-------| | -------| | -------|
A B C
|-------| | -------| | -------|
| | | | | |
| O | | O | | O |
| | | | | | | | |
|-------| | -------| | -------|
D E F
|-------| | -------| | -------|
| | | | | |
| O | | O---| | O |
| | | | | | | |
|-------| | -------| | -------|
G H I
(Figure 1)
There is nine clocks in a 3*3 array (Figure 1). The goal is to return all of the dials to O ' clock with as few moves as possible. There is nine different allowed ways to turn the dials on the clocks. Each such-by is called a move. Select for each move a number 1 to 9. That number would turn the dials "(degrees) clockwise on those clocks which is 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 was 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 O ' clock. Convinced that's the answer is unique.
Sample Input
3 3 02 2 22 1 2
Sample Output
4 5 8 9
Analysis: began to figure out BFS, but also a very large array of record path, written half a day did not write it out. So look at other people's blogs, copy the following methods. The main is what kind of move first, then what kind of movement, the results have no effect, so you can search in a fixed order.
Java AC Code
ImportJava.util.Scanner; Public classtheclocks_1166 { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); intA[] =New int[10];//array A is the input data for(inti = 1; I <= 9; i++) {A[i]=Sc.nextint (); } intB[] =New int[10]; intC[] =New int[10]; for(b[1]=0;b[1]<=3;b[1]++)//Array B is used to store 9 ways of moving, each of which can be used 0~3 times, then brute-force search continues for(b[2]=0;b[2]<=3;b[2]++)//a total of 4 of 9 species may for(b[3]=0;b[3]<=3;b[3]++)//because the AAB movement and the ABA movement result in the same results, so the order can be fixed for(b[4]=0;b[4]<=3;b[4]++) for(b[5]=0;b[5]<=3;b[5]++) for(b[6]=0;b[6]<=3;b[6]++) for(b[7]=0;b[7]<=3;b[7]++) for(b[8]=0;b[8]<=3;b[8]++) for(b[9]=0;b[9]<=3;b[9]++) {c[1]= (a[1]+b[1]+b[2]+b[4])%4;//The array C indicates the state of the clock after the move.C[2]= (a[2]+b[1]+b[2]+b[3]+b[5])%4;//and the effect of a movement on a clock.C[3]= (A[3]+b[2]+b[3]+b[6])%4; c[4]= (A[4]+b[1]+b[4]+b[5]+b[7])%4; c[5]= (a[5]+b[1]+b[3]+b[5]+b[7]+b[9])%4; c[6]= (a[6]+b[3]+b[5]+b[6]+b[9])%4; c[7]= (A[7]+b[4]+b[7]+b[8])%4; c[8]= (a[8]+b[5]+b[7]+b[8]+b[9])%4; c[9]= (a[9]+b[6]+b[8]+b[9])%4; if(c[1]+c[2]+c[3]+c[4]+c[5]+c[6]+c[7]+c[8]+c[9]==0)//when it's all 0, the output { for(inti=0;i<b[1];i++) System.out.print ("1"); for(inti=0;i<b[2];i++) System.out.print ("2"); for(inti=0;i<b[3];i++) System.out.print ("3"); for(inti=0;i<b[4];i++) System.out.print ("4"); for(inti=0;i<b[5];i++) System.out.print ("5"); for(inti=0;i<b[6];i++) System.out.print ("6"); for(inti=0;i<b[7];i++) System.out.print ("7"); for(inti=0;i<b[8];i++) System.out.print ("8"); for(inti=0;i<b[9];i++) System.out.print ("9"); System.out.print ("\ n"); return; } } }}
POJ 1166 The Clocks (Mob search)