-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwater.pas
1453 lines (1282 loc) · 36.7 KB
/
water.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit water;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Spin,
ExtCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
BSolve: TButton;
BUndo: TButton;
CBSingle: TCheckBox;
Panel1: TPanel;
TBRandom: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Memo1: TMemo;
NColorsSpin: TSpinEdit;
NFreeVialSpin: TSpinEdit;
NVolumeSpin: TSpinEdit;
procedure BSolveClick(Sender: TObject);
procedure BUndoClick(Sender: TObject);
procedure CBSingleChange(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
procedure FormKeyUp(Sender: TObject; var Key: word; Shift: TShiftState);
procedure NColorsSpinChange(Sender: TObject);
procedure NFreeVialSpinChange(Sender: TObject);
procedure NVolumeSpinChange(Sender: TObject);
procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: integer);
procedure Panel1Paint(Sender: TObject);
procedure TBRandomClick(Sender: TObject);
private
public
end;
{$minEnumSize 1}
TCls = (EMPTY, BLUE, RED, LIME, YELLOW, FUCHSIA, AQUA, GRAY, ROSE, OLIVE,
BROWN, LBROWN, GREEN, LBLUE, BLACK);
{$minEnumSize normal}
TState = array of array of TList;
THash = array of array of array of UInt32;
TVialsDef = array of array of TCls;
TColDef = array of TColor;
TVialTopInfo = record
empty: integer; //empty volume of vial
topcol: integer;//surface color, 0 for empty vial
topvol: integer;//volume of surface color, NVOLUME for empty vial
end;
TMoveInfo = record
srcVial: UInt8; //source and destination of a move
dstVial: UInt8;
merged: boolean;//move reduced number of blocks or keeps number
end;
{ TVial }
TVial = class
public
color: array of TCls;//colors starting from top of vial
pos: UInt8; //Index of vial
constructor Create(var c: array of TCls; p: UInt8);
destructor Destroy; override;
function getTopInfo: TVialTopInfo;
function vialBlocks: integer;
end;
{ TNode }
TNode = class
public
vial: array of TVial;
hash: UInt32;
mvInfo: TMoveInfo;
constructor Create(t: TVialsDef);
constructor Create(node: TNode);
destructor Destroy; override;
procedure printRaw(w: TMemo);
procedure print(w: TMemo);
function getHash: UInt32;
procedure writeHashbit;
function isHashedQ: boolean;
function nodeBlocks: integer;
function equalQ(node: TNode): boolean;
function lastmoves: string;
function Nlastmoves: integer;
function emptyVials: integer;
end;
const
cols: array of TColor = (clWhite, clBlue, clRed, clLime, clYellow, clFuchsia, clAqua,
clGray, TColor($8c5eeb), clOlive, TColor($2d6cc1), TColor($7aacff), TColor($1a6201),
TColor($FF8C00), clBlack);
XOFF = 10;
YOFF = 20;
APPNAME = 'ColorSortOptimalSolver';
N_NOTDECREASE = 1000;
N_HISTORY = 1000;
N_MAXNODES = 2000000;//abort if reached to avoid heap getting to big
var
Form1: TForm1;
NCOLORS, NVIALS, NEMPTYVIALS, NVOLUME, NEXTRA: integer;
hashbits: array of UInt64;
vialsDefHist: array [0..N_HISTORY] of TVialsDef;
undoHist: integer;
stop: boolean;// abort optimal solving
shifted: boolean;// shift state
singleMode: boolean;//Single Block Mode or Multi Block Mode
implementation
uses Math;
{$R *.lfm}
var
state: TState;
hsh: THash;
globVialdef: TVialsDef;
srcVial, dstVial, srcblock, dstblock: integer;
function compare(v1, v2: TVial): integer;
var
i: integer;
begin
for i := 0 to NVOLUME - 1 do
begin
if v1.color[i] < v2.color[i] then
exit(1);
if v1.color[i] > v2.color[i] then
exit(-1);
end;
Result := 0;
end;
procedure sortNode(var node: TNode; iLo, iHi: integer);
var
Lo, Hi: integer;
Pivot, T: TVial;
begin
Lo := iLo;
Hi := iHi;
Pivot := node.vial[(Lo + Hi) div 2];
repeat
while compare(node.vial[Lo], Pivot) = 1 do
Inc(Lo);
while compare(node.vial[Hi], Pivot) = -1 do
Dec(Hi);
if Lo <= Hi then
begin
T := node.vial[Lo];
node.vial[Lo] := node.vial[Hi];
node.vial[Hi] := T;
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then
sortNode(node, iLo, Hi);
if Lo < iHi then
sortNode(node, Lo, iHi);
end;
procedure init(global: boolean = True);
var
i, j, k: integer;
begin
NCOLORS := Form1.NColorsSpin.Value;
NEMPTYVIALS := Form1.NFreeVialSpin.Value;
NVOLUME := Form1.NVolumeSpin.Value;
NVIALS := NCOLORS + NEMPTYVIALS;
singlemode := Form1.CBSingle.Checked;
Randomize;
SetLength(state, 0, 0);
//We allow N_NOTDECREASE moves which do not decrease total block number
SetLength(state, NCOLORS * (NVOLUME - 1) + 1, N_NOTDECREASE + 1);
SetLength(hsh, 0, 0, 0);
SetLength(hsh, NCOLORS + 1, NVOLUME, NVIALS);
for i := 0 to NCOLORS do
for j := 0 to NVOLUME - 1 do
for k := 0 to NVIALS - 1 do
hsh[i, j, k] := Random(4294967295); //color, position, vial
SetLength(hashbits, 67108864);
for i := 0 to 67108864 - 1 do
hashbits[i] := 0;
if global then //Reset all vials
begin
SetLength(globVialdef, NVIALS, NVOLUME);
for i := 0 to NCOLORS - 1 do
for j := 0 to NVOLUME - 1 do
globVialdef[i, j] := TCls(i + 1);
for i := NCOLORS to NVIALS - 1 do
for j := 0 to NVOLUME - 1 do
globVialdef[i, j] := EMPTY;
undoHist := -1;
Form1.Caption := APPNAME;
Form1.Panel1.Invalidate;
end;
srcVial := -1;
dstVial := -1;
srcblock := -1;
dstblock := -1;
//shifted:=false;
end;
function nearoptimalSolution_single(nblock, y0: integer): string;
var
i, j, k, x, y, src, dst, ks, kd, vmin, solLength, addmove: integer;
nd, ndcand: TNode;
ndlist: TList;
viS, viD: TVialTopInfo;
resback, ft, mv2: string;
A: TStringArray;
label
freemem;
begin
if stop then
begin
Result := 'Computation aborted!';
goto freemem;
end;
if state[nblock - NCOLORS, y0].Count = 0 then
begin
Result := 'No solution. Undo moves or create new puzzle.';
goto freemem;
end;
if nblock = NCOLORS then //puzzle is almost solved
begin
Result := TNode(state[0, 0][0]).lastmoves;
goto freemem;
end;
Result := '';
if NVIALS > 9 then
ft := '%2d->%2d,'
else
ft := '%d->%d,';
x := nblock - NCOLORS;
y := y0;
nd := TNode(state[x, y][0]);
addMove := nd.Nlastmoves; //add last moves seperate
mv2 := nd.lastmoves;
src := nd.mvInfo.srcVial;
dst := nd.mvInfo.dstVial;
Result := Result + Format(ft, [src + 1, dst + 1]);
if nd.mvInfo.merged then
begin
Dec(x);
end
else
begin
Dec(y);
end;
solLength := 1;
while (x <> 0) or (y <> 0) do
begin
ndlist := state[x, y];
for i := 0 to ndlist.Count - 1 do
begin
ndcand := TNode.Create(TNode(ndlist.Items[i]));
ks := 0;
while ndcand.vial[ks].pos <> src do
Inc(ks);
kd := 0;
while ndcand.vial[kd].pos <> dst do
Inc(kd);
viS := ndcand.vial[ks].getTopInfo;
viD := ndcand.vial[kd].getTopInfo;
if viS.empty = NVOLUME then
begin
ndcand.Free;
continue;//source is empty vial
end;
if (viD.empty = 0)(*destination vial full*) or
((viD.empty < NVOLUME) and (viS.topcol <> viD.topcol))
(*destination not empty and top colors different*) or
((viD.empty = NVOLUME) and (viS.empty = NVOLUME - 1))
(*destination empty and only one ball in source*) then
begin
ndcand.Free;
continue;
end;
ndcand.vial[kd].color[viD.empty - 1] := TCls(viS.topcol);
ndcand.vial[ks].color[viS.empty] := EMPTY;
sortNode(ndcand, 0, NVIALS - 1);
if nd.equalQ(ndcand) then
begin
ndcand.Free;
nd := TNode(ndlist.Items[i]);
src := nd.mvInfo.srcVial;
dst := nd.mvInfo.dstVial;
Result := Result + Format(ft, [src + 1, dst + 1]);
Inc(solLength);
if nd.mvInfo.merged then
Dec(x)
else
Dec(y);
break;
end;
ndcand.Free;
end;//i;
end;
Form1.Memo1.Lines.Add(Format('Near-optimal solution in %d moves',
[solLength + addMove]));
A := Result.Split(','); //Reverse move string
k := Length(A);
resback := '';
for i := k - 1 downto 0 do
begin
resback := resback + A[i] + ' ';
if (k - i - 1) mod 10 = 0 then
resback := resback + sLineBreak;
end;
Result := resback + mv2;
freemem:
for i := 0 to nblock - NCOLORS do
for j := 0 to y0 + 1 do
begin
for k := 0 to state[i, j].Count - 1 do
TNode(state[i, j][k]).Free;
state[i, j].Free;
end;
end;
procedure solve_single(def: TVialsDef);
var
nd, ndnew: TNode;
nblockV, i, j, k, lmin, kmin, x, y, ks, kd, newnodes, total: integer;
ndlist: TList;
viS, viD: TVialTopInfo;
blockdecreaseQ, solutionFound: boolean;
tmp: Pointer;
label
abort;
begin
init(False); //false: do not reset color definitions in display
nd := TNode.Create(def);
sortNode(nd, 0, NVIALS - 1);
y := 0;
nblockV := nd.nodeBlocks + nd.emptyVials - NEMPTYVIALS;//total number of blocks
for i := 0 to nblockV - NCOLORS do
state[i, y] := TList.Create;
state[0, 0].Add(nd);
nd.writeHashbit;
total := 1;
solutionFound := False;
repeat
newnodes := 0;
for i := 0 to nblockV - NCOLORS do
state[i, y + 1] := TList.Create; //prepare next column
for x := 0 to nblockV - NCOLORS - 1 do
begin
if stop then
goto abort;
Application.ProcessMessages;
ndlist := state[x, y];
for i := 0 to ndlist.Count - 1 do
begin
nd := TNode(ndlist.Items[i]);
for ks := 0 to NVIALS - 1 do
begin
viS := nd.vial[ks].getTopInfo;
if viS.empty = NVOLUME then
continue;//source is empty vial
for kd := 0 to NVIALS - 1 do
begin
if kd = ks then
continue;//source vial= destination vial
viD := nd.vial[kd].getTopInfo;
if (viD.empty = 0)(*destination vial full*) or
((viD.empty < NVOLUME) and (viS.topcol <> viD.topcol))
(*destination not empty and top colors different*) or
((viD.empty = NVOLUME) and (viS.empty = NVOLUME - 1))
(*destination empty and only one ball in source*) then
continue;
if (viS.topvol = 1) and (viS.empty <> NVOLUME - 1) then
blockdecreaseQ := True //exactly one block on different blocks
else
blockdecreaseQ := False;
ndnew := TNode.Create(nd);
ndnew.vial[kd].color[viD.empty - 1] := TCls(viS.topcol);
ndnew.vial[ks].color[viS.empty] := EMPTY;
sortNode(ndnew, 0, NVIALS - 1);
ndnew.hash := ndnew.getHash;
if ndnew.isHashedQ then
begin
ndnew.Free;
continue; //node presumely already exists, no hash collision detection
end;
ndnew.writeHashbit;
Inc(total);
if total > N_MAXNODES then
begin
Form1.Memo1.Lines.Add('');
Form1.Memo1.Lines.Add(Format('Node limit %d exceeded!', [N_MAXNODES]));
stop := True;
goto abort;
end;
ndnew.mvInfo.srcVial := nd.vial[ks].pos;
ndnew.mvInfo.dstVial := nd.vial[kd].pos;
if blockdecreaseQ then
begin
ndnew.mvInfo.merged := True;
state[x + 1, y].Add(ndnew);
end
else
begin
ndnew.mvInfo.merged := False;
state[x, y + 1].Add(ndnew);
Inc(newnodes);//new node in next column
end;
end;//destination vial;
end;//source vial
end;//list interation
end; //column interation
if state[nblockV - NCOLORS, y].Count > 0 then
solutionFound := True;
Inc(y); //next column
// Form1.Memo1.Lines.Add(Format('%d',[GetHeapStatus.TotalAllocated]));
until solutionFound or (newnodes = 0);
if solutionfound then
begin
//Form1.Memo1.Lines.Add(IntToStr(nblockV) + ' ' + IntToStr(NCOLORS));
//for i := 0 to nblockV - NCOLORS do
// for j := 0 to y - 1 do
// Form1.Memo1.Lines.Add(IntToStr(i) + ' ' + IntToStr(j) + ' ' +
// IntToStr(state[i, j].Count));
lmin := 99999; //select solution with the fewest correction moves
for k := 0 to state[nblockV - NCOLORS, y - 1].Count - 1 do
begin
//TNode(state[nblockV - NCOLORS, y - 1][k]).printRaw(Form1.Memo1);
j := TNode(state[nblockV - NCOLORS, y - 1][k]).Nlastmoves;
if j < lmin then
begin
kmin := k;
lmin := j;
end;
end;
tmp := state[nblockV - NCOLORS, y - 1][0];
state[nblockV - NCOLORS, y - 1][0] := state[nblockV - NCOLORS, y - 1][kmin];
state[nblockV - NCOLORS, y - 1][kmin] := tmp;
end;
Form1.Memo1.Lines.Add('');
Form1.Memo1.Lines.Add(Format('%d nodes generated', [total]));
Form1.Memo1.Lines.Add(nearoptimalSolution_single(nblockV, y - 1));
Exit;
abort:
Form1.Memo1.Lines.Add(nearoptimalSolution_single(nblockV, y));
end;
function optimalSolution_multi(nblock, y0: integer): string;
var
i, j, k, x, y, src, dst, ks, kd, vmin, solLength: integer;
nd, ndcand: TNode;
ndlist: TList;
viS, viD: TVialTopInfo;
resback, ft: string;
A: TStringArray;
label
freemem;
begin
if stop then
begin
Result := 'Computation aborted!';
goto freemem;
end;
if state[nblock - NCOLORS, y0].Count = 0 then
begin
Result := 'No solution. Undo moves or create new puzzle.';
goto freemem;
end;
if nblock = NCOLORS then
begin
Result := 'Puzzle already solved!';
goto freemem;
end;
Result := '';
if NVIALS > 9 then
ft := '%2d->%2d,'
else
ft := '%d->%d,';
x := nblock - NCOLORS;
y := y0;
nd := TNode(state[x, y][0]);
src := nd.mvInfo.srcVial;
dst := nd.mvInfo.dstVial;
Result := Result + Format(ft, [src + 1, dst + 1]);
if nd.mvInfo.merged then
begin
Dec(x);
end
else
begin
Dec(y);
end;
solLength := 1;
while (x <> 0) or (y <> 0) do
begin
ndlist := state[x, y];
for i := 0 to ndlist.Count - 1 do
begin
ndcand := TNode.Create(TNode(ndlist.Items[i]));
ks := 0;
while ndcand.vial[ks].pos <> src do
Inc(ks);
kd := 0;
while ndcand.vial[kd].pos <> dst do
Inc(kd);
viS := ndcand.vial[ks].getTopInfo;
viD := ndcand.vial[kd].getTopInfo;
if viS.empty = NVOLUME then
begin
ndcand.Free;
continue;//source is empty vial
end;
if (viD.empty = 0)(*destination vial full*) or
((viD.empty < NVOLUME) and (viS.topcol <> viD.topcol))
(*destination not empty and top colors different*) or
((viD.empty = NVOLUME) and (viS.topvol + viS.empty = NVOLUME))
(*destination empty and only one color in source*) then
begin
ndcand.Free;
continue;
end;
vmin := Min(viD.empty, viS.topvol);
for j := 1 to vmin do
begin
ndcand.vial[kd].color[viD.empty - j] := TCls(viS.topcol);
ndcand.vial[ks].color[vis.empty - 1 + j] := EMPTY;
end;
sortNode(ndcand, 0, NVIALS - 1);
if nd.equalQ(ndcand) then
begin
ndcand.Free;
nd := TNode(ndlist.Items[i]);
src := nd.mvInfo.srcVial;
dst := nd.mvInfo.dstVial;
Result := Result + Format(ft, [src + 1, dst + 1]);
Inc(solLength);
if nd.mvInfo.merged then
Dec(x)
else
Dec(y);
break;
end;
ndcand.Free;
end;//i;
end;
Form1.Memo1.Lines.Add(Format('Optimal solution in %d moves', [solLength]));
A := Result.Split(','); //Reverse move string
k := Length(A);
resback := '';
for i := k - 1 downto 0 do
begin
resback := resback + A[i] + ' ';
if (k - i - 1) mod 10 = 0 then
resback := resback + sLineBreak;
end;
// Form1.Memo1.Lines.Add(resback);
Result := resback;
freemem:
for i := 0 to nblock - NCOLORS do
for j := 0 to y0 + 1 do
begin
for k := 0 to state[i, j].Count - 1 do
TNode(state[i, j].Items[k]).Free;
state[i, j].Free;
end;
end;
procedure solve_multi(def: TVialsDef);
var
nd, ndnew: TNode;
nblockV, i, j, newnodes, x, y, ks, kd, vmin, total: integer;
ndlist: TList;
viS, viD: TVialTopInfo;
blockdecreaseQ, solutionFound: boolean;
label
abort;
begin
init(False); //false: do not reset color definitions in display
nd := TNode.Create(def);
sortNode(nd, 0, NVIALS - 1);
y := 0;
nblockV := nd.nodeBlocks;//total number of blocks in start configuration
for i := 0 to nblockV - NCOLORS do
state[i, y] := TList.Create;
state[0, 0].Add(nd);
nd.writeHashbit;
total := 1;
solutionFound := False;
repeat
newnodes := 0;
for i := 0 to nblockV - NCOLORS do
state[i, y + 1] := TList.Create; //prepare next column
for x := 0 to nblockV - NCOLORS - 1 do
begin
if stop then
goto abort;
Application.ProcessMessages;
ndlist := state[x, y];
for i := 0 to ndlist.Count - 1 do
begin
nd := TNode(ndlist.Items[i]);
for ks := 0 to NVIALS - 1 do
begin
viS := nd.vial[ks].getTopInfo;
if viS.empty = NVOLUME then
continue;//source is empty vial
for kd := 0 to NVIALS - 1 do
begin
if kd = ks then
continue;//source vial= destination vial
viD := nd.vial[kd].getTopInfo;
if (viD.empty = 0)(*destination vial full*) or
((viD.empty < NVOLUME) and (viS.topcol <> viD.topcol))
(*destination not empty and top colors different*) or
((viD.empty = NVOLUME) and (viS.topvol + viS.empty = NVOLUME))
(*destinaion empty and only one color in source*) then
continue;
if (viD.empty < NVOLUME) and (viD.empty >= viS.topvol) then
blockdecreaseQ := True //two color blocks are merged
else
blockdecreaseQ := False;
vmin := Min(viD.empty, viS.topvol);
ndnew := TNode.Create(nd);
for j := 1 to vmin do
begin
ndnew.vial[kd].color[viD.empty - j] := TCls(viS.topcol);
ndnew.vial[ks].color[viS.empty - 1 + j] := EMPTY;
end;
sortNode(ndnew, 0, NVIALS - 1);
ndnew.hash := ndnew.getHash;
if ndnew.isHashedQ then
begin
ndnew.Free;
continue; //node presumely already exists, no hash collision detection
end;
ndnew.writeHashbit;
Inc(total);
if total > N_MAXNODES then
begin
Form1.Memo1.Lines.Add('');
Form1.Memo1.Lines.Add(Format('Node limit %d exceeded!', [N_MAXNODES]));
stop := True;
goto abort;
end;
ndnew.mvInfo.srcVial := nd.vial[ks].pos;
ndnew.mvInfo.dstVial := nd.vial[kd].pos;
if blockdecreaseQ then
begin
ndnew.mvInfo.merged := True;
state[x + 1, y].Add(ndnew);
end
else
begin
ndnew.mvInfo.merged := False;
state[x, y + 1].Add(ndnew);
Inc(newnodes);//new node in next column
end;
end;//destination vial;
end;//source vial
end;//list interation
end; //column interation
if state[nblockV - NCOLORS, y].Count > 0 then
solutionFound := True;
Inc(y); //next column
until solutionFound or (newnodes = 0);
Form1.Memo1.Lines.Add('');
Form1.Memo1.Lines.Add(Format('%d nodes generated', [total]));
Form1.Memo1.Lines.Add(optimalSolution_multi(nblockV, y - 1));
Exit;
abort:
Form1.Memo1.Lines.Add(optimalSolution_multi(nblockV, y));
end;
{ TNode }
constructor TNode.Create(t: TVialsDef);
var
i, nvial: integer;
begin
nvial := High(t);
Setlength(self.vial, nvial + 1);
for i := 0 to nvial do
self.vial[i] := TVial.Create(t[i], i);
self.hash := getHash;
end;
constructor TNode.Create(node: TNode);
var
i, nvial: integer;
begin
nvial := High(node.vial);
Setlength(self.vial, nvial + 1);
for i := 0 to nvial do
begin
self.vial[i] := TVial.Create(node.vial[i].color, node.vial[i].pos);
end;
self.hash := node.hash;
end;
destructor TNode.Destroy;
var
n, i: integer;
begin
n := High(self.vial);
for i := 0 to n do
self.vial[i].Destroy;
Setlength(self.vial, 0);
inherited;
end;
procedure TNode.printRaw(w: TMemo);
var
s, sn: string;
hv, hc, i, j: integer;
begin
hv := High(self.vial);
hc := High(self.vial[1].color);
for i := 0 to hc do
begin
s := '';
for j := 0 to hv do
begin
sn := IntToStr(byte(self.vial[j].color[i]));
s := s + Format('%5s', [sn]);
end;
w.Lines.Add(s);
end;
w.Lines.Add('');
end;
procedure TNode.print(w: TMemo);
var
s, sn: string;
hv, hc, i, j: integer;
vialpos: array of byte;
begin
hv := High(self.vial);
hc := High(self.vial[1].color);
SetLength({%H-}vialpos, hv + 1); //{%H-} to supress warning
for i := 0 to hv do
vialpos[self.vial[i].pos] := i;
for i := 0 to hc do
begin
s := '';
for j := 0 to hv do
begin
sn := IntToStr(byte(self.vial[vialpos[j]].color[i]));
s := s + Format('%5s', [sn]);
end;
w.Lines.Add(s);
end;
w.Lines.Add('');
end;
function TNode.getHash: UInt32;
var
p, v: integer;
begin
Result := 0;
for v := 0 to NVIALS - 1 do
for p := 0 to NVOLUME - 1 do
begin
Result := Result xor hsh[integer(self.vial[v].color[p]), p, v];
end;
end;
procedure TNode.writeHashbit;
var
base, offset: integer;
begin
base := self.hash div 64;
offset := self.hash mod 64;
hashbits[base] := hashbits[base] or (UInt64(1) shl offset);
end;
function TNode.isHashedQ: boolean;
var
base, offset: integer;
begin
base := self.hash div 64;
offset := self.hash mod 64;
if (hashbits[base] and (UInt64(1) shl offset)) <> 0 then
Result := True
else
Result := False;
end;
function TNode.nodeBlocks: integer;
var
i: integer;
begin
Result := 0;
for i := 0 to NVIALS - 1 do
begin
Inc(Result, self.vial[i].vialBlocks);
//we count emtpty vials as 1 block in singleBLock mode
//if singleMode and (self.vial[i].color[NVOLUME - 1] = EMPTY) then
// Inc(Result);
end;
end;
function TNode.equalQ(node: TNode): boolean;
//test vials for equality. vials are assumed to be already sorted.
var
i, j: integer;
begin
Result := True;
for i := 0 to NVIALS - 1 do
for j := 0 to NVOLUME - 1 do
begin
if self.vial[i].color[j] <> node.vial[i].color[j] then
exit(False);
end;
end;
function TNode.lastmoves: string;
//we assume nd is sorted
var
i, j, k, n, cl, src, dst, vol: integer;
ft: string;
begin
if NVIALS > 9 then
ft := '%2d->%2d '
else
ft := '%d->%d ';
Result := '';
for i := 1 to NCOLORS do
begin
j := NVIALS - 1;
while self.vial[j].getTopInfo.topcol <> i do
Dec(j);
if self.vial[j].getTopInfo.empty = 0 then
continue;//vial with this color is full
for k := 0 to j - 1 do
if self.vial[k].getTopInfo.topcol = i then
for n := 0 to self.vial[k].getTopInfo.topvol - 1 do
Result := Result + Format(ft, [self.vial[k].pos + 1, self.vial[j].pos + 1]);
end;
if Result = '' then
Result := 'Puzzle is solved!';
end;
function TNode.Nlastmoves: integer;
var
i: integer;
begin
if singlemode then
begin
Result := 0;
for i := 0 to NEMPTYVIALS - 1 do
Inc(Result, self.vial[i].getTopInfo.topvol);
end
else
begin
Result := NEMPTYVIALS;
for i := 0 to NEMPTYVIALS - 1 do
if vial[i].color[NVOLUME - 1] = EMPTY then
Dec(Result);
end;
end;
function TNode.emptyVials: integer;
var
i: integer;
begin
Result := 0;
for i := 0 to NVIALS - 1 do
if self.vial[i].color[NVOLUME - 1] = EMPTY then
Inc(Result);
end;
{ TVial }
constructor TVial.Create(var c: array of TCls; p: UInt8);
var
i: integer;
begin
Setlength(self.color, NVOLUME);
for i := 0 to NVOLUME - 1 do
self.color[i] := c[i];
self.pos := p;
end;
destructor TVial.Destroy;
begin
Setlength(self.color, 0);
inherited;
end;
function TVial.getTopInfo: TVialTopInfo;
var
i, cl: integer;
begin
Result.topcol := 0;
Result.empty := NVOLUME;
Result.topvol := 0;
if self.color[NVOLUME - 1] = EMPTY then
Exit(Result); //empty vial
for i := 0 to NVOLUME - 1 do
if self.color[i] <> EMPTY then
begin
cl := integer(self.color[i]);
Result.topcol := cl;
Result.empty := i;
Break;
end;
Result.topvol := 1;
for i := Result.empty + 1 to NVOLUME - 1 do
if cl = integer(self.color[i]) then
Inc(Result.topvol)
else
Break;
end;
function TVial.vialBlocks: integer;