-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpruntables.cpp
1929 lines (1765 loc) · 57.9 KB
/
pruntables.cpp
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
//---------------------------------------------------------------------------
#pragma hdrstop
#include <iostream>
//#include <cstdlib>
//#include <cstring>
//#include <utility>
//#include <time.h>
#include "pruntables.h"
#include "fifteen.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#define NWD 24964 //Number of wd-matrices
#define WEIGHT(piece,a,b) BNK[15+a-piece][b-piece]
//order of elements in puz array INTERN
//00 01 02 03
//07 06 05 04
//08 09 10 11
//15 14 13 12
//order of elements in puz array ZEROBASED
//00 01 02 03
//04 05 06 07
//08 09 10 11
//12 13 14 15
//order of elements in puz array ZEROTERMINATED
//01 02 03 04
//05 06 07 08
//09 10 11 12
//13 14 15 00
//wd elements
//x 0 0 0
//1 1 1 1
//2 2 2 2
//3 3 3 3
//describes the number of wd-elements per row
//for example 1 1 0 2 means 1 times 0, 2 times 1, 1 times 2 und 0 times 3 in this row
typedef unsigned char wdmatrix[4][4];
struct node{
char pz[16];
char pz_r[16];
};
//WD663 performance: 93 ms/random instance
//ONLY78 performance: 3 ms/random instance
__int64 nodecount=0,sumdepth=0,timeStart,timeStop;
int solutionFound=0,abortIDA=0;
char solution[85];
int lastSolution[85];//80 is enough
int CurrentSearchLength=0;//aktuelle Länge der Lösung
//++++++++++++some tables+++++++++++++++++++++++++++++++++++++++++++++++
int indextowdhash[NWD];//map 0<=index<NWD of wd-position tohashvalue 0<=x<=20*35*35*35
short int wdmoveup[NWD][4];//wd-movetable for move of blank up
short int wdmovedown[NWD][4];
unsigned char A[4][4][4][4],BCD[5][5][5][5];//for computing the wd-hash
//newidx[blank][dir] gives the new INTERNAL position index of the blank when moving in direction dir
//dir: 0 left, 1 right, 2 up, 3 down
char newidx[16][4]={{-1,1,-1,7},{0,2,-1,6},{1,3,-1,5},{2,-1,-1,4},{5,-1,3,11},{6,4,2,10},{7,5,1,9},{-1,6,0,8},
{-1,9,7,15},{8,10,6,14},{9,11,5,13},{10,-1,4,12},{13,-1,11,-1},{14,12,10,-1},{15,13,9,-1},{-1,14,8,-1}};
//die Richtung relativ zum puz-Array.0: benachbart, 1 nicht benachtbart, Permutation ändert sich
int mirr[16]={0,7,8,15,14,9,6,1,2,5,10,13,12,11,4,3};
int fromzerobased[16]={0,1,2,3,7,6,5,4,8,9,10,11,15,14,13,12};
char masksum[256][8];
char manhattdist[16][16];
int BNK[16][8];
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char *prunwd;//pointers to the pruningtable
char *prun17,*prun815;
char *prun16,*prun79,*prun1015;
char *prun15,*prun610,*prun1115;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
unsigned int Bnk(int n, int k)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
int s;
int i;
if (n<k) return 0;
for (s=1,i=n; i!=n-k; i--) s *= i;
return s;
}
char msum(char mask, int piece){//piece from 0 to 7
char s=0;
for (int i=0;i<=piece;i++) if ((1<<i)&mask) s++;
return s;
}
void buildmasksum(){
for (int i=0;i<256;i++){
for (int piece=0;piece<8;piece++)
masksum[i][piece]=msum(i,piece);
}
}
void buildBNK(){
for (int i=0;i<16;i++)
for (int j=0;j<8;j++)
BNK[i][j]=Bnk(i,j);
}
int weight(char piece, int a, int b){
return BNK[15+a-piece][b-piece];
}
int index(char*pz, int a, int b){//halb so schnell wie die andere Methode!
char invp[16];
char msk=0, mask[16];
for (int i=a;i<=b;i++)mask[i]=0;
for (int i=0;i<16;i++){
if ((pz[i]>=a)&&pz[i]<=b){
mask[pz[i]]|=msk;
msk|=(1<<(pz[i]-a));
invp[pz[i]]=i;
}
}
int s=0;
for (int i=a,j=16;i<=b;i++){
s*=j--;
s+=invp[i]-masksum[(unsigned char)mask[i]][i-a];
}
return s;
}
void inv_index(char*pz, int a, int b, int idx){
int n=b-a;
int offset[16];
for (int i=16-n;i<=16;i++){
int c=idx%i;idx/=i;
offset[a+16-i]=c;
}
for (int i=0;i<16;i++) pz[i]=0;
for (int i=a;i<=b;i++){
int v=0,j=0;
while (v!=offset[i]){
if (pz[j++]==0) v++;
}
//jetzt nächsten freien Platz suchen
while (pz[j]!=0) j++;
pz[j]=i;
}
}
///Hashes for the columns of a wd-matrix
//first column A, 2.,3.,4. dolumn BCD
void inithashABCD() {
int hash = 0;
for (int i = 0; i <= 3; i++)
for (int j = 0; j <= 3 - i; j++)
for (int k = 0; k <= 3 - i - j; k++)
if (3 - i - j - k >= 0)
A[i][j][k][3 - i - j - k] = hash++; // <20
hash = 0;
for (int i = 0; i <= 4; i++)
for (int j = 0; j <= 4 - i; j++)
for (int k = 0; k <= 4 - i - j; k++)
if (4 - i - j - k >= 0)
BCD[i][j][k][4 - i - j - k] = hash++; // <35
}
//Hash value of a wd-Matrix
//0<=hash<20*35*35*35=857500
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int wdhash(wdmatrix m){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int a= A[m[0][0]][m[1][0]][m[2][0]][m[3][0]];
int b= BCD[m[0][1]][m[1][1]][m[2][1]][m[3][1]];
int c= BCD[m[0][2]][m[1][2]][m[2][2]][m[3][2]];
int d= BCD[m[0][3]][m[1][3]][m[2][3]][m[3][3]];
return a+20*b+20*35*c+20*35*35*d;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int getblankrow(wdmatrix m){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
for (int i=0;i<4;i++) if (m[i][0]+m[i][1]+m[i][2]+m[i][3]==3) return i;
return 0;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void copywdmatrix(wdmatrix src, wdmatrix dst){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
dst[i][j]=src[i][j];
}
//Compute wd-matrix from permutation
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void puztowdmatrix(char *pz, wdmatrix m){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
for (int i=0;i<16;i++)m[i/4][i%4]=0;
for (int i=0;i<16;i++){
if (pz[i]!=0){
m[i/4][pz[i]/4]++;
}
}
}
//Compute the index of a wd-matrix
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
unsigned short wdindex(char *pz){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
wdmatrix m;
puztowdmatrix(pz,m);
int n = wdhash(m);
for (int j = 0; j < NWD; j++)
if (indextowdhash[j] == n)
return j;
return 0;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void buildprunWD( char *prn){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char *hashprun= new char[20*35*35*35];
wdmatrix *wd = new wdmatrix[NWD+1];
int left,right,depth,prevdepth,head,n;
for (int i=0;i<20*35*35*35;i++) hashprun[i]=-1;
unsigned char v[16]={3,0,0,0,0,4,0,0,0,0,4,0,0,0,0,4};
for (int i=0;i<16;i++) wd[0][i/4][i%4]=v[i];
for (int i=0;i<NWD;i++)
for (int k=0;k<4;k++){
wdmovedown[i][k]=-1;
wdmoveup[i][k]=-1;
}
//build the indextowdhash table
// Form1->Memo->Lines->Add("create WD-Pruning Table: ");
hashprun[wdhash(wd[0])]=0;right=0;depth=0;head=1;
indextowdhash[0]=wdhash(wd[0]);
prn[0]=0;
do{
left=right;right=head;
prevdepth=depth++;
for (int i=left;i<right;i++){
Application->ProcessMessages();
if (hashprun[wdhash(wd[i])]==prevdepth){//should always be true
int j= getblankrow(wd[i]);
if (j-1>=0){
for (int k = 0; k < 4; k++) {
if (wd[i][j - 1][k] > 0) {
copywdmatrix(wd[i], wd[head]);
wd[head][j - 1][k]--;
wd[head][j][k]++;
n = wdhash(wd[head]);
if (hashprun[n] == -1) { // new entry
hashprun[n] = depth;
indextowdhash[head] = n;
prn[head] = depth;
wdmoveup[i][k] = head;
head++;
}
else { // now search entry in table
for (int m = 0; m <= head; m++) {
if (indextowdhash[m] == n) {
wdmoveup[i][k] = m;
break;
}
}
}
}
}
}
if (j + 1 < 4) {
for (int k = 0; k < 4; k++) {
if (wd[i][j + 1][k] > 0) {
copywdmatrix(wd[i], wd[head]);
wd[head][j+1][k]--;wd[head][j][k]++;
n=wdhash(wd[head]);
if (hashprun[n]==-1){//new entry
hashprun[n]=depth;
indextowdhash[head]=n;
prn[head]=depth;
wdmovedown[i][k] = head;
head++;
}
else { // Dann den Eintrag in der Tabelle suchen
for (int m = 0; m <= head; m++) {
if (indextowdhash[m] == n) {
wdmovedown[i][k] = m;
break;
}
}
}
}
}
}
}
}
// Form1->Memo->Lines->Add(IntToStr(depth)+AnsiString(" ")+IntToStr(head-right));
}while (head>right);
delete[] hashprun;
delete[] wd;
}
//reflection at main diagonal
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void reflect(char *src,char *dst){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
for (int i=0;i<16;i++) dst[i]=mirr[src[mirr[i]]];
}
//solvable positions have even parity
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int parity(char* pz){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int s=0;
for (int i=0;i<15;i++){
if (pz[i]==0) continue;
for (int j=i+1;j<16;j++){
if (pz[j]==0) continue;
if (pz[i]>pz[j]) s++;
}
}
return s%2;
}
//generate reandom puzzle in INTERNAL Format
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char * randompuz(char* pz,puztype type){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
for (int i = 0; i < 16; i++)
pz[i] = i;
for (int i = 16; i > 1; i--)
swap(pz[i - 1], pz[rand() % i]);
char p[16],p_r[16];
char *pp;
switch (type) {
case ZEROBASED:
for (int i = 0; i < 16; i++)
p[i] = fromzerobased[pz[fromzerobased[i]]];
pp = p;
break;
case ZEROTERMINATED:
for (int i = 0; i < 16; i++)
p_r[i] = (16 - pz[15 - i]) % 16;
for (int i = 0; i < 16; i++)
p[i] = fromzerobased[p_r[fromzerobased[i]]];
pp = p;
break;
case INTERNAL:
pp = pz;
break;
}
if (parity(pp)!=0){//fix parity
if (pz[0]==0 || pz[1]==0) swap(pz[2],pz[3]);
else swap(pz[0],pz[1]);
}
return pz;
}
//get position of blank
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char blankpos(char* pz){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
for (int i=0;i<16;i++) if (pz[i]==0) return i;
return 0;
}
//from current blank position fill all positions with -1 which can be reached
//without moving tiles not belonging to the pruning tile set
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void fill_(char *p, char blankpos){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (p[blankpos]!=0) return;//occupied with pruning tile (>0) oder filled (-1)
else{
p[blankpos]=-1;//fill
for (int d=0;d<4;d++){//fill neighbours if possible
int newblankpos = newidx[blankpos][d];
if (newblankpos==-1)continue;//outside of puzzle
else fill_(p,newblankpos);
}
}
}
//return a bitvector with all other possible blank positions for a given
//pruning tile configuration
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
short int blankcluster_(char* pz, char blankpos){//p
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char p[16];
for (int i=0;i<16;i++) p[i]=pz[i];
fill_(p,blankpos);
//it is sufficient to take only those possible blank positions
//which have a neighbour of the pruning set
for (int i=0;i<16;i++){
if (p[i]!=-1) continue;//no blank position
for (int j=0;j<4;j++){
int n = newidx[i][j];
if (n<0) continue;//outside
if (p[n]>0) {p[i]=-2;break;}//has neighbour
}
}
short int r=0;
for (int i=0;i<16;i++)
if (p[i]==-2) r|= (1<<i);
return r;
}
//build the pruning table for pruning tiles set a,...b
//smaller memory chunks
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int buildprun_32(char* prn, int a, int b, bool loadonly){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
FILE *fprun;
char filename[20],idx[20];
filename[0]='p';filename[1]=0;
itoa(a,idx,10);
strcat(filename,idx);
itoa(b,idx,10);
strcat(filename,idx);
unsigned int B=Bnk(16,b-a+1);
fprun = fopen(filename,"r+b");
if (fprun!=NULL){
Form1->Memo->Lines->Add(AnsiString("reading from file ")+filename);
//cout<<"\nreading from file "<<filename<<endl;
fread(prn,1,B,fprun);
fclose(fprun);
Form1->Memo->Lines->Add(" done");
return 0;
}
if (loadonly) return -1;
short int *blanks1[8], *blanks1tmp[8];
int B8=B/8;
try{
for (int i = 0; i < 8; i++) {
blanks1[i] = new short int[B8];
blanks1tmp[i] = new short int[B8];
}
}
catch (...) {
for (int i = 0; i < 8; i++) {
delete[]blanks1[i];
delete[]blanks1tmp[i];
}
Application->MessageBox
(L"You do not have enough memory to use this heuristics.\n"
L"You must have at least Windows 64 bit with 6 GB of RAM.", L"",
MB_OK | MB_ICONERROR);
return -1;
}
Form1->Memo->Lines->Add(AnsiString("generating table ")+filename);
for (unsigned int i = 0; i < B; i++) {
blanks1[i/B8][(i%B8)] = 0;
blanks1tmp[i/B8][(i%B8)] = 0;
}
char pz[16];
for (int i = 0; i < 16; i++)
pz[i] = i; // starting configuration
for (unsigned int i = 0; i < B; i++) {
if (i % 100000 == 0) {
Application->ProcessMessages();
}
prn[i] = 127; // set all entries to empty
}
int j=index(pz,a,b);//
prn[j]=0;
inv_index(pz,a,b,j);
blanks1[j/B8][(j%B8)]|= blankcluster_(pz,0);//starting blankposition is 0 (INTERNAL format)
unsigned int done=1,olddone=done;unsigned char depth=0,prevdepth;
do{
prevdepth=depth++;
for (unsigned int i=0;i<B;i++){
if (i%100000==0) {
Application->ProcessMessages();
}
if (prn[i]<=prevdepth){//prn[i]==prevdepth is wrong!
inv_index(pz,a,b,i);
int bl = blanks1[i/B8][(i%B8)];//possible blank positions
for (int blpos=0;blpos<16;blpos++){//<16!!!
if ((bl & 1) == 0) {
bl >>= 1;
continue;
}
if (pz[blpos] != 0) {
Form1->Memo->Lines->Add
("Error while generating table. Aborted");
return -1;
}
for (int dir = 0; dir < 4; dir++) {
int newblpos = newidx[blpos][dir];
if (newblpos < 0)
continue; // outside puzzle
int tmp = pz[newblpos];
if (tmp==0) continue;//exchanging blank with non pruning tile does not count
pz[newblpos]=0;pz[blpos]=tmp;
j=index(pz,a,b);//beibehalten!!
if(prn[j]==127){
prn[j]=depth; done++;
}
if ((blanks1[j/B8][(j%B8)]&(1<<newblpos))==0){//new blankcluster
char ps[16];
for (int k=0;k<16;k++) ps[k]=pz[k];
blanks1tmp[j/B8][(j%B8)]|= blankcluster_(ps,newblpos);
}
pz[blpos]=0;pz[newblpos]=tmp;//restore position
}
bl>>=1;
}
}
}
Form1->Memo->Lines->Add(L"depth: "+IntToStr(depth)+L" count: "+IntToStr((int)(done-olddone)));
olddone=done;
for (unsigned int k=0;k<B;k++){
if (k%100000==0) {
Application->ProcessMessages();
}
blanks1[k/B8][(k%B8)]|= blanks1tmp[k/B8][(k%B8)] ;
}
} while (done<B);
for (int i=0; i < 8; i++) {
delete[] blanks1[i];
delete[] blanks1tmp[i];
}
Form1->Memo->Lines->Add("saving file "+AnsiString(filename));
fprun = fopen(filename,"w+b");
fwrite(prn,1,B,fprun);
fclose(fprun);
Form1->Memo->Lines->Add("done ");
return 0;
}
//build the pruning table for pruning tiles set a,...b
//if loadonly==true the tables will not be created if the files
//are not present yet
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void buildprun_(char* prn, int a, int b, bool loadonly){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
FILE *fprun;
char filename[20],idx[20];
filename[0]='p';filename[1]=0;
itoa(a,idx,10);
strcat(filename,idx);
itoa(b,idx,10);
strcat(filename,idx);
unsigned int B=Bnk(16,b-a+1);
fprun = fopen(filename,"r+b");
if (fprun!=NULL){
Form1->Memo->Lines->Add(AnsiString("reading from file ")+filename);
Application->ProcessMessages();
fread(prn,1,B,fprun);
fclose(fprun);
Form1->Memo->Lines->Add(" done");
return;
}
if (loadonly) return;
Form1->Memo->Lines->Add(AnsiString("generating table ")+filename);
short int *blanks = new short int[B];
short int *blankstmp = new short int[B];
for (unsigned int i=0;i<B;i++) {blanks[i]=0;blankstmp[i]=0;}
char pz[16];
for (int i=0;i<16;i++) pz[i]=i;//starting configuration
for (unsigned int i=0;i<B;i++) prn[i]=127;//set all entries to empty
int j=index(pz,a,b);//
prn[j]=0;
inv_index(pz,a,b,j);
blanks[j]|= blankcluster_(pz,0);//starting blankposition is 0 (INTERNAL format)
unsigned int done=1,olddone=done;unsigned char depth=0,prevdepth;
do{
prevdepth=depth++;
for (unsigned int i=0;i<B;i++){
if (i%100000==0) {
Application->ProcessMessages();
}
if (prn[i]<=prevdepth){//prn[i]==prevdepth is wrong!
inv_index(pz,a,b,i);
int bl = blanks[i];//possible blank positions
for (int blpos=0;blpos<16;blpos++){//<16!!!
if ((bl&1)==0){bl>>=1;continue;}
if (pz[blpos]!=0) Form1->Memo->Lines->Add("FEHLER");//darf nicht passieren!
for (int dir=0;dir<4;dir++){
int newblpos=newidx[blpos][dir];
if (newblpos<0) continue;//outside puzzle
int tmp=pz[newblpos];
if (tmp==0) continue;//exchanging blank with non pruning tile does not count
pz[newblpos]=0;pz[blpos]=tmp;
j=index(pz,a,b);//beibehalten!!
if(prn[j]==127){
prn[j]=depth; done++;
}
if ((blanks[j]&(1<<newblpos))==0){//new blankcluster
char ps[16];
for (int k=0;k<16;k++) ps[k]=pz[k];
blankstmp[j]|= blankcluster_(ps,newblpos);
}
pz[blpos]=0;pz[newblpos]=tmp;//restore position
}
bl>>=1;
}
}
}
Form1->Memo->Lines->Add(L"depth: "+IntToStr(depth)+L" count: "+IntToStr((int)(done-olddone)));
olddone=done;
for (unsigned int k=0;k<B;k++){blanks[k]|=blankstmp[k];}
} while (done<B);
delete[] blanks;
delete[] blankstmp;
Form1->Memo->Lines->Add("saving file "+AnsiString(filename));
fprun = fopen(filename,"w+b");
fwrite(prn,1,B,fprun);
fclose(fprun);
Form1->Memo->Lines->Add("done ");
// cout<<" done"<<endl;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void treesearchMANHATTAN_(node n, char blnkpos,char lastdir,int togo){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (solutionFound||abortIDA) return;
if (togo == 0) {
solution[togo] = blnkpos;
if (solution[84] == 0) { // Ort für Lösungslänge
QueryPerformanceCounter((LARGE_INTEGER *)&timeStop);
printSolutionPieces(Form1->puz,CurrentSearchLength, (puztype)(Form1->Type->ItemIndex));
solution[84] = CurrentSearchLength;
if (Form1->SolNum->ItemIndex==0) { //only one solution to compute
solutionFound=1;
return;
}
}
else {
if (solution[84] < CurrentSearchLength) {
QueryPerformanceCounter((LARGE_INTEGER *)&timeStop);
solutionFound = 1;
return;
}
printSolutionPieces(Form1->puz, CurrentSearchLength,
(puztype)(Form1->Type->ItemIndex));
Application->ProcessMessages();
}
}
else {
for (char dir = 0; dir < 4; dir++) {
if (lastdir == -1 || (dir + lastdir) % 4 != 1) {
int newblankpos = newidx[blnkpos][dir];
if (newblankpos == -1)
continue;
n.pz[blnkpos] = n.pz[newblankpos];
n.pz[newblankpos] = 0;
int mandi=0;
for (int i=0;i<16;i++) {
if (i != newblankpos)
mandi += manhattdist[i][n.pz[i]];
}
if (mandi < togo) {
nodecount++;
if (nodecount % 1000000 == 0) {
Application->ProcessMessages();
}
solution[togo] = blnkpos;
treesearchMANHATTAN_(n, newblankpos, dir, togo - 1);
}
n.pz[newblankpos] = n.pz[blnkpos];
n.pz[blnkpos] = 0;
}
}
}
}
//only Ken'ichiro Takahashi's wd-heuristic
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void treesearchWD(node n, char blnkpos, unsigned short wdidx, unsigned short wdidx_r,char lastdir,int togo){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (solutionFound||abortIDA) return;
// char pd[16];
// for (int i=0;i<16;i++) pd[i]=n.pz[i];
if (togo == 0) {
solution[togo] = blnkpos;
if (solution[84] == 0) { // Ort für Lösungslänge
QueryPerformanceCounter((LARGE_INTEGER *)&timeStop);
printSolutionPieces(Form1->puz,CurrentSearchLength, (puztype)(Form1->Type->ItemIndex));
solution[84] = CurrentSearchLength;
if (Form1->SolNum->ItemIndex==0) { //only one solution to compute
solutionFound=1;
return;
}
}
else {
if (solution[84] < CurrentSearchLength) {
QueryPerformanceCounter((LARGE_INTEGER *)&timeStop);
solutionFound = 1;
return;
}
printSolutionPieces(Form1->puz, CurrentSearchLength, (puztype)(Form1->Type->ItemIndex));
Application->ProcessMessages();
}
}
else{
char blnkpos_r=mirr[blnkpos];
for (char dir=0;dir<4;dir++){
if (lastdir!=-1 && (dir+lastdir)%4==1) continue; //so 10% schneller
int pos = newidx[blnkpos][dir];
if (pos==-1)continue;
int pos_r = mirr[pos];
unsigned short wdidxnew=wdidx,wdidx_rnew=wdidx_r;
switch (dir){
case 0: //move blank left
wdidx_rnew=wdmoveup[wdidx_r][n.pz_r[pos_r]/4];break;
case 1: //move blank right
wdidx_rnew=wdmovedown[wdidx_r][n.pz_r[pos_r]/4];break;
case 2: //move blank up
wdidxnew=wdmoveup[wdidx][n.pz[pos]/4];break;
case 3: //move blank down
wdidxnew=wdmovedown[wdidx][n.pz[pos]/4];break;
}
if ((prunwd[wdidxnew]+prunwd[wdidx_rnew]<=togo-1) /*&& (lastdir==-1 ||(dir+lastdir)%4!=1)*/){
n.pz[blnkpos]=n.pz[pos];n.pz[pos]=0;
n.pz_r[blnkpos_r]=n.pz_r[pos_r];n.pz_r[pos_r]=0;
nodecount++;
if (nodecount%1000000==0) {
Application->ProcessMessages();
}
solution[togo]=blnkpos;
treesearchWD(n,pos,wdidxnew,wdidx_rnew,dir,togo-1);
n.pz[pos]=n.pz[blnkpos];n.pz[blnkpos]=0;
n.pz_r[pos_r]=n.pz_r[blnkpos_r];n.pz_r[blnkpos_r]=0;
}
}
}
}
//additional pruning sets 1..5,6..10,11..15
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void treesearchWD555_(node n, char blnkpos, unsigned short wdidx, unsigned short wdidx_r,
int index15, int index610, int index1115,
int index15_r, int index610_r, int index1115_r,
char lastdir,int togo){
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (solutionFound||abortIDA) return;
// if (togo==0){solutionFound=1;solution[togo]=blnkpos;return;}
if (togo == 0) {
solution[togo] = blnkpos;
if (solution[84] == 0) { // Ort für Lösungslänge
QueryPerformanceCounter((LARGE_INTEGER *)&timeStop);
printSolutionPieces(Form1->puz,CurrentSearchLength, (puztype)(Form1->Type->ItemIndex));
solution[84] = CurrentSearchLength;
if (Form1->SolNum->ItemIndex==0) { //only one solution to compute
solutionFound=1;
return;
}
}
else {
if (solution[84] < CurrentSearchLength) {
QueryPerformanceCounter((LARGE_INTEGER *)&timeStop);
solutionFound = 1;
return;
}
printSolutionPieces(Form1->puz, CurrentSearchLength, (puztype)(Form1->Type->ItemIndex));
Application->ProcessMessages();
}
}
else{
char blnkpos_r=mirr[blnkpos];
for (char dir=0;dir<4;dir++){
int newblnkpos = newidx[blnkpos][dir];
if (newblnkpos==-1)continue;
int newblnkpos_r = mirr[newblnkpos];
unsigned short wdidxnew=wdidx,wdidx_rnew=wdidx_r;
switch (dir){
case 0:
wdidx_rnew=wdmoveup[wdidx_r][n.pz_r[newblnkpos_r]/4];break;
case 1:
wdidx_rnew=wdmovedown[wdidx_r][n.pz_r[newblnkpos_r]/4];break;
case 2:
wdidxnew=wdmoveup[wdidx][n.pz[newblnkpos]/4];break;
case 3:
wdidxnew=wdmovedown[wdidx][n.pz[newblnkpos]/4];break;
}
if ((prunwd[wdidxnew]+prunwd[wdidx_rnew]<=togo-1) && (lastdir==-1 ||(dir+lastdir)%4!=1)){
int index15new=index15; int index610new=index610; int index1115new=index1115;
int index15new_r=index15_r; int index610new_r=index610_r; int index1115new_r=index1115_r;
char piece=n.pz[newblnkpos];
int deltaweight=0,deltapos=0;
switch (blnkpos-newblnkpos){
case 1://go ->
if (piece<6)index15new+= WEIGHT(piece,1,5);
else if (piece<11)index610new+= WEIGHT(piece,6,10);
else if (piece<16)index1115new+= WEIGHT(piece,11,15);
break;
case -1://go <-
if (piece<6)index15new-= WEIGHT(piece,1,5);
else if (piece<11)index610new-= WEIGHT(piece,6,10);
else if (piece<16)index1115new-= WEIGHT(piece,11,15);
break;
case 3:
case 5:
case 7:
if (piece<6){
for (int i=newblnkpos+1;i<blnkpos;i++){
int piece1=n.pz[i];
if (piece1<6){
if (piece1>piece) {deltapos++;deltaweight+=WEIGHT(piece1,1,5);}//wenn piece<piece1 passiert eigentlich nicht
}
else deltapos++;
}
deltapos++;//nach newblankpos rutschen
index15new+=deltaweight+deltapos*WEIGHT(piece,1,5);
}
else if (piece<11){
for (int i=newblnkpos+1;i<blnkpos;i++){
int piece1=n.pz[i];
if (piece1>5&&piece1<11){
if (piece1>piece) {deltapos++;deltaweight+=WEIGHT(piece1,6,10);}//wenn piece<piece1 passiert eigentlich nicht
}
else deltapos++;
}
deltapos++;//nach newblankpos rutschen
index610new+=deltaweight+deltapos*WEIGHT(piece,6,10);
}
else if (piece<16){
for (int i=newblnkpos+1;i<blnkpos;i++){
int piece1=n.pz[i];
if (piece1>10&&piece1<16){
if (piece1>piece) {deltapos++;deltaweight+=WEIGHT(piece1,11,15);}//wenn piece<piece1 passiert eigentlich nicht
}
else deltapos++;
}
deltapos++;//nach newblankpos rutschen
index1115new+=deltaweight+deltapos*WEIGHT(piece,11,15);
};
break;
case -3:
case -5:
case -7:
if (piece<6){
for (int i=newblnkpos-1;i>blnkpos;i--){
int piece1=n.pz[i];
if (piece1<6){
if (piece1>piece) {deltapos--;deltaweight-=WEIGHT(piece1,1,5);}//wenn piece<piece1 passiert eigentlich nicht
}
else deltapos--;
}
deltapos--;//nach newblankpos rutschen
index15new+=deltaweight+deltapos*WEIGHT(piece,1,5);
}
else if (piece<11){
for (int i=newblnkpos-1;i>blnkpos;i--){
int piece1=n.pz[i];
if (piece1>5&&piece1<11){
if (piece1>piece) {deltapos--;deltaweight-=WEIGHT(piece1,6,10);}//wenn piece<piece1 passiert eigentlich nicht
}
else deltapos--;
}
deltapos--;//nach newblankpos rutschen
index610new+=deltaweight+deltapos*WEIGHT(piece,6,10);
}
else if (piece<16){
for (int i=newblnkpos-1;i>blnkpos;i--){
int piece1=n.pz[i];
if (piece1>10&&piece1<16){
if (piece1>piece) {deltapos--;deltaweight-=WEIGHT(piece1,11,15);}//wenn piece<piece1 passiert eigentlich nicht
}
else deltapos--;
}
deltapos--;//nach newblankpos rutschen
index1115new+=deltaweight+deltapos*WEIGHT(piece,11,15);
};
break;
}
piece=n.pz_r[newblnkpos_r];
switch (blnkpos_r-newblnkpos_r){
case 1://go ->
if (piece<6)index15new_r+= WEIGHT(piece,1,5);
else if (piece<11)index610new_r+= WEIGHT(piece,6,10);
else if (piece<16)index1115new_r+= WEIGHT(piece,11,15);
break;
case -1://go <-
if (piece<6)index15new_r-= WEIGHT(piece,1,5);
else if (piece<11)index610new_r-= WEIGHT(piece,6,10);
else if (piece<16)index1115new_r-= WEIGHT(piece,11,15);
break;
case 3:
case 5:
case 7:
if (piece<6){
for (int i=newblnkpos_r+1;i<blnkpos_r;i++){
int piece1=n.pz_r[i];
if (piece1<6){
if (piece1>piece) {deltapos++;deltaweight+=WEIGHT(piece1,1,5);}//wenn piece<piece1 passiert eigentlich nicht
}
else deltapos++;
}
deltapos++;//nach newblankpos rutschen
index15new_r+=deltaweight+deltapos*WEIGHT(piece,1,5);
}
else if (piece<11){
for (int i=newblnkpos_r+1;i<blnkpos_r;i++){
int piece1=n.pz_r[i];
if (piece1>5&&piece1<11){
if (piece1>piece) {deltapos++;deltaweight+=WEIGHT(piece1,6,10);}//wenn piece<piece1 passiert eigentlich nicht
}
else deltapos++;
}
deltapos++;//nach newblankpos rutschen
index610new_r+=deltaweight+deltapos*WEIGHT(piece,6,10);
}
else if (piece<16){
for (int i=newblnkpos_r+1;i<blnkpos_r;i++){
int piece1=n.pz_r[i];
if (piece1>10&&piece1<16){
if (piece1>piece) {deltapos++;deltaweight+=WEIGHT(piece1,11,15);}//wenn piece<piece1 passiert eigentlich nicht
}
else deltapos++;
}
deltapos++;//nach newblankpos rutschen
index1115new_r+=deltaweight+deltapos*WEIGHT(piece,11,15);
};
break;
case -3:
case -5:
case -7:
if (piece<6){
for (int i=newblnkpos_r-1;i>blnkpos_r;i--){
int piece1=n.pz_r[i];
if (piece1<6){
if (piece1>piece) {deltapos--;deltaweight-=WEIGHT(piece1,1,5);}//wenn piece<piece1 passiert eigentlich nicht