|
1 #include <stdio.h> |
|
2 #include <string.h> |
|
3 |
|
4 #define BOX '$' |
|
5 #define WALL '#' |
|
6 #define MAN '@' |
|
7 #define PLATFORM '.' |
|
8 #define BOXINPLATFORM '*' |
|
9 #define MANINPLATFORM 'E' |
|
10 #define BLANK ' ' |
|
11 #define CANTO '-' |
|
12 #define MANCANMOVE '+' |
|
13 |
|
14 #define NBOX(n) ((n)<<2) |
|
15 |
|
16 #define DIR_LEFT 0 |
|
17 #define DIR_RIGHT 1 |
|
18 #define DIR_UP 2 |
|
19 #define DIR_DOWN 3 |
|
20 |
|
21 #define MAX_X 50 |
|
22 #define MAX_Y 50 |
|
23 #define MAX_MOVES 50 |
|
24 #define MAX_BOXES 15 |
|
25 |
|
26 #define MOVE_OK 1 |
|
27 #define MOVE_BOX 2 |
|
28 #define MOVE_ILLEGAL 0 |
|
29 |
|
30 /* SOKOBAN Solution Finder |
|
31 * |
|
32 * Cerca totes les possibilitats de tots els nombres de combinacions possibles, |
|
33 * menys la que tots els moviments són a l'esquerra del número de combinacions |
|
34 * incials triat. |
|
35 |
|
36 * 13/01/2002 |
|
37 * Comentari afegit el 4/01/2003: |
|
38 * Cerca la solució segons el moviment de caixes, i no el de l'home. |
|
39 * Falta optimitzar: Llocs on no posar caixes segons les caixes (són dinàmics |
|
40 * segons cada element de l'array de mapes!) |
|
41 */ |
|
42 |
|
43 |
|
44 struct Map |
|
45 { |
|
46 char Cells[MAX_Y][MAX_X]; |
|
47 char ManMoves[MAX_Y][MAX_X]; |
|
48 int SizeX, SizeY; |
|
49 int ManX, ManY; |
|
50 int NumPlatforms; |
|
51 int NumBoxesInPlatform; |
|
52 int BoxX[MAX_BOXES]; |
|
53 int BoxY[MAX_BOXES]; |
|
54 int NumBoxes; |
|
55 }; |
|
56 |
|
57 |
|
58 void CopyMap (struct Map *Mdest, struct Map *Morig) |
|
59 { |
|
60 memcpy((void *) Mdest, (void *) Morig, sizeof (struct Map)); |
|
61 } |
|
62 |
|
63 |
|
64 void ReadMap(struct Map *M, char *FileName) |
|
65 { |
|
66 FILE *Fitxer; |
|
67 int i,j; |
|
68 |
|
69 if(!(Fitxer = fopen(FileName, "r"))) |
|
70 { |
|
71 printf("Error opening %s!", FileName); |
|
72 exit(1); |
|
73 } |
|
74 |
|
75 M->SizeX=0; |
|
76 M->SizeY=0; |
|
77 while (!feof(Fitxer)) |
|
78 { |
|
79 fgets(M->Cells[M->SizeY], MAX_X, Fitxer); |
|
80 M->SizeY++; |
|
81 } |
|
82 M->SizeY--; |
|
83 M->SizeX = strlen(M->Cells[0]) - 1; |
|
84 |
|
85 M->NumPlatforms = 0; |
|
86 M->NumBoxesInPlatform = 0; |
|
87 M->NumBoxes = 0; |
|
88 for (j = 0; j<M->SizeY; j++) |
|
89 for (i=0; i<M->SizeX; i++) |
|
90 { |
|
91 if (M->Cells[j][i] == MAN) |
|
92 { |
|
93 M->ManX = i; M->ManY = j; |
|
94 M->Cells[M->ManY][M->ManX] = BLANK; |
|
95 } |
|
96 |
|
97 if (M->Cells[j][i] == PLATFORM) |
|
98 M->NumPlatforms++; |
|
99 else if (M->Cells[j][i] == BOXINPLATFORM) |
|
100 { |
|
101 M->Cells[j][i] = PLATFORM; |
|
102 |
|
103 M->NumPlatforms++; |
|
104 M->NumBoxesInPlatform++; |
|
105 |
|
106 M->BoxX[M->NumBoxes] = i; |
|
107 M->BoxY[M->NumBoxes] = j; |
|
108 M->NumBoxes++; |
|
109 } else if (M->Cells[j][i] == BOX) |
|
110 { |
|
111 M->Cells[j][i] = BLANK; |
|
112 |
|
113 M->BoxX[M->NumBoxes] = i; |
|
114 M->BoxY[M->NumBoxes] = j; |
|
115 M->NumBoxes++; |
|
116 } else if (M->Cells[j][i] != WALL) |
|
117 { |
|
118 if ( (M->Cells[j][i-1] == WALL && |
|
119 M->Cells[j-1][i] == WALL) || |
|
120 (M->Cells[j][i-1] == WALL && |
|
121 M->Cells[j+1][i] == WALL) || |
|
122 (M->Cells[j][i+1] == WALL && |
|
123 M->Cells[j-1][i] == WALL) || |
|
124 (M->Cells[j][i+1] == WALL && |
|
125 M->Cells[j+1][i] == WALL)) |
|
126 M->Cells[j][i] = CANTO; |
|
127 } |
|
128 |
|
129 } |
|
130 |
|
131 } |
|
132 |
|
133 |
|
134 void ShowMap (struct Map *M) |
|
135 { |
|
136 struct Map Temp; |
|
137 int i,j; |
|
138 |
|
139 CopyMap(&Temp, M); |
|
140 |
|
141 Temp.Cells[Temp.ManY][Temp.ManX] = MAN; |
|
142 |
|
143 for (i = 0; i < Temp.NumBoxes; i++) |
|
144 { |
|
145 if (Temp.Cells[Temp.BoxY[i]][Temp.BoxX[i]] == BLANK) |
|
146 Temp.Cells[Temp.BoxY[i]][Temp.BoxX[i]] = BOX; |
|
147 else if (Temp.Cells[Temp.BoxY[i]][Temp.BoxX[i]] == PLATFORM) |
|
148 Temp.Cells[Temp.BoxY[i]][Temp.BoxX[i]] = BOXINPLATFORM; |
|
149 } |
|
150 |
|
151 for (j = 0; j<Temp.SizeY; j++) |
|
152 { |
|
153 for (i=0; i<Temp.SizeX; i++) |
|
154 printf("%c", Temp.Cells[j][i]); |
|
155 printf("\n"); |
|
156 } |
|
157 // Print Where the man can move |
|
158 for (j = 0; j<Temp.SizeY; j++) |
|
159 { |
|
160 for (i=0; i<Temp.SizeX; i++) |
|
161 printf("%i", Temp.ManMoves[j][i]); |
|
162 printf("\n"); |
|
163 } |
|
164 |
|
165 printf("Man is at (%i,%i)\n", Temp.ManX, Temp.ManY); |
|
166 printf("Platforms: %i, BoxesInPlatform: %i\n", Temp.NumPlatforms, |
|
167 Temp.NumBoxesInPlatform); |
|
168 } |
|
169 |
|
170 |
|
171 |
|
172 int InverseMove(char Dir1, char Dir2) |
|
173 { |
|
174 if ((Dir1 + Dir2 == DIR_LEFT + DIR_RIGHT) || |
|
175 (Dir1 + Dir2 == DIR_UP + DIR_DOWN)) |
|
176 return 1; |
|
177 return 0; |
|
178 } |
|
179 |
|
180 |
|
181 void GetManMoves(struct Map *M) |
|
182 { |
|
183 int X[MAX_MOVES], Y[MAX_MOVES]; |
|
184 int NewX[MAX_MOVES], NewY[MAX_MOVES]; |
|
185 int NumMoves, NewMoves; |
|
186 int j, i; |
|
187 |
|
188 NumMoves = 1; |
|
189 for (j = 0; j<M->SizeY; j++) |
|
190 for (i=0; i<M->SizeX; i++) |
|
191 M->ManMoves[j][i] = (M->Cells[j][i] == WALL)?WALL:BLANK; |
|
192 for (i = 0; i<M->NumBoxes; i++) |
|
193 M->ManMoves[M->BoxY[i]][M->BoxX[i]] = WALL; |
|
194 |
|
195 NewMoves = 1; |
|
196 NewX[0] = M->ManX; |
|
197 NewY[0] = M->ManY; |
|
198 while (NewMoves) |
|
199 { |
|
200 NumMoves = NewMoves; |
|
201 for (i=0; i<NewMoves; i++) |
|
202 { |
|
203 X[i] = NewX[i]; |
|
204 Y[i] = NewY[i]; |
|
205 } |
|
206 NewMoves = 0; |
|
207 for (i=0; i<NumMoves; i++) |
|
208 { |
|
209 if(M->ManMoves[Y[i]][X[i]-1] == BLANK) |
|
210 { |
|
211 NewX[NewMoves] = X[i]-1; |
|
212 NewY[NewMoves] = Y[i]; |
|
213 M->ManMoves[Y[i]][X[i]-1] = MANCANMOVE; |
|
214 NewMoves++; |
|
215 } |
|
216 else if(M->ManMoves[Y[i]][X[i]+1] == BLANK) |
|
217 { |
|
218 NewX[NewMoves] = X[i]+1; |
|
219 NewY[NewMoves] = Y[i]; |
|
220 M->ManMoves[Y[i]][X[i]+1] = MANCANMOVE; |
|
221 NewMoves++; |
|
222 } |
|
223 else if(M->ManMoves[Y[i]-1][X[i]] == BLANK) |
|
224 { |
|
225 NewX[NewMoves] = X[i]; |
|
226 NewY[NewMoves] = Y[i]-1; |
|
227 M->ManMoves[Y[i]-1][X[i]] = MANCANMOVE; |
|
228 NewMoves++; |
|
229 } |
|
230 else if(M->ManMoves[Y[i]+1][X[i]] == BLANK) |
|
231 { |
|
232 NewX[NewMoves] = X[i]; |
|
233 NewY[NewMoves] = Y[i]+1; |
|
234 M->ManMoves[Y[i]+1][X[i]] = MANCANMOVE; |
|
235 NewMoves++; |
|
236 } |
|
237 } |
|
238 } |
|
239 } |
|
240 |
|
241 int MoveBox(struct Map *M, int NumBox, int Direction) |
|
242 { |
|
243 char InPlatform; |
|
244 |
|
245 InPlatform = (M->Cells[M->BoxY[NumBox]][M->BoxX[NumBox]] == PLATFORM); |
|
246 |
|
247 switch(Direction) |
|
248 { |
|
249 case DIR_LEFT: |
|
250 if(M->ManMoves[M->BoxY[NumBox]][M->BoxX[NumBox]+1] == |
|
251 MANCANMOVE) |
|
252 { |
|
253 if(InPlatform) |
|
254 M->NumBoxesInPlatform--; |
|
255 M->BoxX[NumBox]--; |
|
256 if(M->Cells[M->BoxY[NumBox]][M->BoxX[NumBox]] == |
|
257 PLATFORM) |
|
258 M->NumBoxesInPlatform++; |
|
259 return 1; |
|
260 }else return 0; |
|
261 break; |
|
262 case DIR_RIGHT: |
|
263 if(M->ManMoves[M->BoxY[NumBox]][M->BoxX[NumBox]-1] == |
|
264 MANCANMOVE) |
|
265 { |
|
266 if(InPlatform) |
|
267 M->NumBoxesInPlatform--; |
|
268 M->BoxX[NumBox]++; |
|
269 if(M->Cells[M->BoxY[NumBox]][M->BoxX[NumBox]] == |
|
270 PLATFORM) |
|
271 M->NumBoxesInPlatform++; |
|
272 return 1; |
|
273 }else return 0; |
|
274 break; |
|
275 case DIR_UP: |
|
276 if(M->ManMoves[M->BoxY[NumBox]-1][M->BoxX[NumBox]] == |
|
277 MANCANMOVE) |
|
278 { |
|
279 if(InPlatform) |
|
280 M->NumBoxesInPlatform--; |
|
281 M->BoxY[NumBox]--; |
|
282 if(M->Cells[M->BoxY[NumBox]][M->BoxX[NumBox]] == |
|
283 PLATFORM) |
|
284 M->NumBoxesInPlatform++; |
|
285 return 1; |
|
286 }else return 0; |
|
287 break; |
|
288 case DIR_DOWN: |
|
289 if(M->ManMoves[M->BoxY[NumBox]+1][M->BoxX[NumBox]] == |
|
290 MANCANMOVE) |
|
291 { |
|
292 if(InPlatform) |
|
293 M->NumBoxesInPlatform--; |
|
294 M->BoxY[NumBox]++; |
|
295 if(M->Cells[M->BoxY[NumBox]][M->BoxX[NumBox]] == |
|
296 PLATFORM) |
|
297 M->NumBoxesInPlatform++; |
|
298 return 1; |
|
299 }else return 0; |
|
300 break; |
|
301 } |
|
302 return 0; |
|
303 } |
|
304 |
|
305 |
|
306 int main(int argn, char **argv) |
|
307 { |
|
308 struct Map Morigin; |
|
309 struct Map M[MAX_MOVES+1]; |
|
310 int Moves[MAX_MOVES]; |
|
311 int NumMoves; |
|
312 int OldMaps; |
|
313 int IllegalMove; |
|
314 int Carry; |
|
315 |
|
316 int i; |
|
317 |
|
318 if (argn != 3) |
|
319 { |
|
320 printf("Usage: %s <mapfile> <initial NumMoves>\n", argv[0]); |
|
321 exit(3); |
|
322 } |
|
323 ReadMap(&Morigin, argv[1]); |
|
324 sscanf(argv[2], "%i", &NumMoves); |
|
325 |
|
326 ShowMap(&Morigin); |
|
327 |
|
328 for (i = 0; i < NumMoves; i++) |
|
329 Moves[i] = NBOX(0) + DIR_LEFT; |
|
330 |
|
331 // Reget the original map |
|
332 CopyMap(&M[0], &Morigin); |
|
333 CopyMap(&M[NumMoves], &Morigin); // For the first while cond. |
|
334 |
|
335 GetManMoves(&M[0]); |
|
336 ShowMap(&Morigin); |
|
337 |
|
338 IllegalMove = NumMoves - 1; |
|
339 // Process the combination |
|
340 for (i = 0; i < NumMoves; i++) |
|
341 { |
|
342 CopyMap(&M[i+1], &M[i]); |
|
343 if (!MoveBox(&M[i+1], Moves[i]>>2, Moves[i] & 0x03)) |
|
344 { |
|
345 IllegalMove = i; |
|
346 break; |
|
347 } |
|
348 } |
|
349 |
|
350 // Main solution finder loop |
|
351 while (M[NumMoves].NumPlatforms != M[NumMoves].NumBoxesInPlatform) |
|
352 { |
|
353 // Increase the Counter |
|
354 { |
|
355 Carry = 1; |
|
356 // Reset the direction of sure-invalid moves |
|
357 for (i = IllegalMove + 1; i < NumMoves; i++) |
|
358 Moves[i] = NBOX(0) + DIR_LEFT; |
|
359 // Increase Counter for a new try of moves |
|
360 for (i = IllegalMove; i >= 0 && Carry; i--) |
|
361 { |
|
362 Moves[i]++; |
|
363 Carry = 0; |
|
364 if (Moves[i] ==NBOX(M[0].NumBoxes-1)+DIR_DOWN+1) |
|
365 { Moves[i] = NBOX(0) + DIR_LEFT; Carry = 1; } |
|
366 } |
|
367 OldMaps = i+1; // Sure? I think it's i+1 |
|
368 // If we change the number of movements for solution |
|
369 if (Carry) |
|
370 { |
|
371 if (NumMoves > MAX_MOVES) |
|
372 break; // MAX MOVES EXCEEDED!!! |
|
373 NumMoves++; |
|
374 for (i = 0; i < NumMoves; i++) |
|
375 Moves[i] = NBOX(0) + DIR_LEFT; |
|
376 OldMaps=0; |
|
377 CopyMap(&M[NumMoves], &Morigin); |
|
378 // For the first while cond. |
|
379 printf("Provant %i moviments...\n", NumMoves); |
|
380 } |
|
381 } |
|
382 |
|
383 /* |
|
384 // Print the combination |
|
385 |
|
386 for (i=0; i < NumMoves; i++) |
|
387 { |
|
388 printf("%c", Moves[i]); |
|
389 } |
|
390 printf("\n"); |
|
391 */ |
|
392 |
|
393 IllegalMove = NumMoves - 1; |
|
394 // Process the combination |
|
395 for (i = OldMaps; i < NumMoves - 1; i++) |
|
396 { |
|
397 CopyMap(&M[i+1], &M[i]); |
|
398 if (!MoveBox(&M[i+1], Moves[i]>>2, Moves[i] & 0x03)) |
|
399 { |
|
400 IllegalMove = i; |
|
401 break; |
|
402 } /*else |
|
403 if (((Moves[i] + Moves[i-1] == DIR_LEFT + DIR_RIGHT) || |
|
404 (Moves[i] + Moves[i-1] == DIR_UP + DIR_DOWN)) && |
|
405 !M[i].BoxMoved) |
|
406 { |
|
407 IllegalMove = i; |
|
408 break; |
|
409 }*/ |
|
410 GetManMoves(&M[i+1]); |
|
411 |
|
412 } |
|
413 // Here i = NumMoves - 1 |
|
414 CopyMap(&M[i+1], &M[i]); |
|
415 if (!MoveBox(&M[i+1], Moves[i]>>2, Moves[i] & 0x03)) |
|
416 IllegalMove = i; |
|
417 |
|
418 } |
|
419 |
|
420 // Print the combination |
|
421 for (i=0; i < NumMoves; i++) |
|
422 { |
|
423 printf("%i", Moves[i] >> 2); |
|
424 if ((Moves[i] & 0x03) == DIR_LEFT) |
|
425 printf("L"); |
|
426 else if ((Moves[i] & 0x03) == DIR_RIGHT) |
|
427 printf("R"); |
|
428 else if ((Moves[i] & 0x03) == DIR_UP) |
|
429 printf("U"); |
|
430 else |
|
431 printf("D"); |
|
432 printf(","); |
|
433 } |
|
434 printf("\n"); |
|
435 |
|
436 /* |
|
437 // Print The Steps |
|
438 for (i=0; i < NumMoves; i++) |
|
439 { |
|
440 ShowMap(&M[i]); |
|
441 } |
|
442 */ |
|
443 return 0; |
|
444 |
|
445 } |