The Java Program: A_block/blockgame.java
1 import java.io.*;
2 import java.util.*;
3 import java.awt.*;
4 import java.text.*;
5 import java.math.*;
6
7
8
9
10
11
12 class blockgame
13 {
14 public Scanner sc;
15 public PrintStream ps;
16
17 public String toString()
18 {
19 return "blockgame";
20 }
21
22
23
24 public static final int di[] = { -1, 0, 1, 0 };
25 public static final int dj[] = { 0, 1, 0, -1 };
26
27
28 public int n=6, m=6;
29
30
31
32
33
34 public class State
35 {
36
37 public char board[][];
38
39
40 public int move;
41
42
43
44
45
46
47
48 public State( char b[][], int mm )
49 {
50
51 move = mm;
52
53
54 board = new char[n][];
55 for( int i=0; i<b.length; i++ )
56 {
57 board[i] = Arrays.copyOf( b[i], b[i].length );
58 }
59 }
60
61
62
63
64
65
66 public char[][] getBoard()
67 {
68 return board;
69 }
70
71
72
73
74
75
76
77 public String getID()
78 {
79 StringBuffer buffer = new StringBuffer(100);
80 for( int i=0; i<board.length; i++ )
81 {
82 buffer.append( board[i] );
83 }
84
85 return buffer.toString();
86 }
87 }
88
89
90
91
92
93
94
95
96
97 public boolean onboard( char b[][], int i, int j )
98 {
99 return i>=0 && j>=0 && i<b.length && j<b[i].length;
100 }
101
102
103
104
105
106
107
108
109
110
111 public boolean canmove( char b[][], int i, int j, int d )
112 {
113
114 int upi = i+di[d];
115 int upj = j+dj[d];
116
117
118 int dni = i-di[d];
119 int dnj = j-dj[d];
120
121
122
123
124
125
126
127
128
129 return onboard( b, upi, upj ) && onboard( b, dni, dnj )
130 && b[upi][upj]=='.' && b[dni][dnj]==b[i][j];
131 }
132
133
134
135
136
137 public void doit() throws Exception
138 {
139 sc = new Scanner( System.in );
140 ps = System.out;
141
142 for(;;)
143 {
144
145 char ch = sc.next().charAt(0);
146 if( ch=='*' ) break;
147
148
149 char board[][] = new char[n][];
150 for( int i=0; i<n; i++ )
151 {
152 board[i] = sc.next().toCharArray();
153 }
154
155
156 LinkedList<State> queue = new LinkedList<State>();
157
158
159 HashSet<String> seen = new HashSet<String>();
160
161
162
163 State state = new State( board, 0 );
164 queue.add( state );
165 seen.add( state.getID() );
166
167
168 int solution = -1;
169
170
171
172 while( queue.size()>0 && solution<0 )
173 {
174
175
176 state = queue.removeFirst();
177 char b[][] = state.getBoard();
178
179
180
181
182 for( int i=0; i<n; i++ )
183 {
184 if( b[i][m-1]==ch )
185 {
186 solution = state.move;
187 break;
188 }
189 }
190
191
192 for( int i=0; i<b.length; i++ ) for( int j=0; j<b[i].length; j++ ) if( b[i][j]!='.')
193 {
194
195 for( int d=0; d<4; d++ )
196 {
197
198 if( canmove( b, i, j, d ) )
199 {
200
201
202 int ii = i;
203 int jj = j;
204
205
206 char newb[][] = new char[n][m];
207 for( int k=0; k<n; k++ )
208 {
209 newb[k] = Arrays.copyOf( b[k], m );
210 }
211
212
213
214 int i3 = i-2*di[d];
215 int j3 = j-2*dj[d];
216 int size = onboard( newb, i3, j3 ) && newb[i3][j3]==newb[ii][jj] ? 2 : 1;
217
218
219 while( canmove( newb, ii, jj, d ) )
220 {
221
222 newb[ii+di[d]][jj+dj[d]] = newb[ii][jj];
223 newb[ii-size*di[d]][jj-size*dj[d]] = '.';
224
225
226 State newstate = new State( newb, state.move+1 );
227 String newid = newstate.getID();
228
229
230
231 if( !seen.contains( newid ) )
232 {
233 queue.add( newstate );
234 seen.add( newid );
235 }
236
237
238 ii += di[d];
239 jj += dj[d];
240 }
241 }
242 }
243 }
244 }
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 ps.println( solution==0 ? 1 : solution );
260 }
261 }
262
263 public static void main(String[] args) throws Exception
264 {
265 long starttime = System.currentTimeMillis();
266 new blockgame().doit();
267 System.out.println( System.currentTimeMillis() - starttime );
268 }
269 }