/*前の記事の続き*/
/*おわり*/
void heuristic(struct node *cube) { char i,j,k,p=1,q; unsigned int color[6], dcube=0, buff=0, hash; struct node *node; if(cube->surface[0][0][0]!=goal.surface[0][0][0] || /*コーナーキューブの位置,向きが違ってる個数を数える*/ cube->surface[2][1][0]!=goal.surface[2][1][0] || cube->surface[1][1][1]!=goal.surface[1][1][1]) dcube++; if(cube->surface[0][0][1]!=goal.surface[0][0][1] || cube->surface[2][1][1]!=goal.surface[2][1][1] || cube->surface[4][1][0]!=goal.surface[4][1][0]) dcube++; if(cube->surface[0][1][0]!=goal.surface[0][1][0] || cube->surface[1][1][0]!=goal.surface[1][1][0] || cube->surface[3][0][0]!=goal.surface[3][0][0]) dcube++; if(cube->surface[0][1][1]!=goal.surface[0][1][1] || cube->surface[4][1][1]!=goal.surface[4][1][1] || cube->surface[3][0][1]!=goal.surface[3][0][1]) dcube++; if(cube->surface[5][0][0]!=goal.surface[5][0][0] || cube->surface[3][1][0]!=goal.surface[3][1][0] || cube->surface[1][0][0]!=goal.surface[1][0][0]) dcube++; if(cube->surface[5][0][1]!=goal.surface[5][0][1] || cube->surface[3][1][1]!=goal.surface[3][1][1] || cube->surface[4][0][1]!=goal.surface[4][0][1]) dcube++; if(cube->surface[5][1][0]!=goal.surface[5][1][0] || cube->surface[2][0][0]!=goal.surface[2][0][0] || cube->surface[1][0][1]!=goal.surface[1][0][1]) dcube++; if(cube->surface[5][1][1]!=goal.surface[5][1][1] || cube->surface[2][0][1]!=goal.surface[2][0][1] || cube->surface[4][0][0]!=goal.surface[4][0][0]) dcube++; for(i=0;i<6;i++)
surface[k][i][j]) color[k]++; /*色が違ってる数を数える*/ for(i=0;i<6;i++)
heuristic=0; else if(hash==12) cube->heuristic=1; else if(hash==884 || hash==402) cube->heuristic=2; else if(hash==1317 || hash==78 || hash==3426 || hash==1156 || hash==1750 || hash==788 || hash==160 || hash==5128 || hash==2291 || hash==6830 || hash==588) cube->heuristic=3; else if(hash==1966 || hash==378 || hash==3048 || hash==4561 || hash==2615 || hash==559 || hash==257 || hash==28 || hash==2039 || hash==452 || hash==307 || hash==9099 || hash==1558 || hash==106 || hash==2939 || hash==18 || hash==62 || hash==2040 || hash==3049 || hash==4057 || hash==1535 || hash==1030 || hash==778 || hash==2292 || hash==1534 || hash==234 || hash==506 || hash==210) cube->heuristic=4; else if(hash==739 || hash==1173 || hash==451 || hash==6074 || hash==3480 || hash==2327 || hash==197 || hash==124 || hash==337 || hash==343 || hash==136 || hash==668 || hash==1318 || hash==3912 || hash==1366 || hash==1031 || hash==1100 || hash==12124 || hash==8091 || hash==272 || hash==694 || hash==1029 || hash==143 || hash==595 || hash==208 || hash==498 || hash==4398 || hash==404 || hash==111 || hash==525 || hash==14 || hash==469) cube->heuristic=5; else if(hash==993 || hash==95 || hash==306 || hash==1724 || hash==23 || hash==403 || hash==885 || hash==596 || hash==2712 || hash==1174 || hash==789 || hash==1367 || hash==3609 || hash==176 || hash==597 || hash==338 || hash==177 || hash==258 || hash==37 || hash==669 || hash==288 || hash==180 || hash==2713 || hash==1751 || hash==16 || hash==5402 || hash==4058 || hash==45 || hash==693 || hash==740 || hash==1815 || hash==499 || hash==1967 || hash==918 || hash==379 || hash==1480 || hash==21 || hash==777 || hash==24 || hash==886 || hash==22) cube->heuristic=6; else if(hash==11 || hash==152 || hash==15 || hash==20 || hash==36 || hash==162 || hash==198 || hash==137 || hash==1368 || hash==17 || hash==50 || hash==13 || hash==75 || hash==1816 || hash==87 || hash==25 || hash==96 || hash==917) cube->heuristic=7; else if(hash==10 || hash==19 || hash==919) cube->heuristic=8; else cube->heuristic=9; cube->expectation=cube->heuristic+cube->cost; cube->hash=1.0; for(i=0;i<6;i++)
hash*=sin(color[i]+i*0.5); /*前と同じノードを展開しないためのハッシュ値を計算*/ cube->hash*=dcube; node=root.nodelist; /*ノードリストをたどる*/ while(node!=cube && p){ if(node->hash==cube->hash){ /*同じハッシュ値をもつノードがあったら*/ q=0; for(k=0;k<5;k++)
surface[k][i][j]!=node->surface[k][i][j]) /*状態を詳しく比較して*/ q++; if(!q){ p=0; cube->revleaf->nextleaf=cube->nextleaf; /*全く同じなら,このノードをリーフリストからはずす*/ cube->nextleaf->revleaf=cube->revleaf; } } node=node->nodelist; } } void exception(void) /*ルート展開のあと1回だけ呼び出す。状態遷移を網羅するためあと3つ展開する*/ { struct node *leaf; char i,j,k,l; leaf=(struct node *)malloc(sizeof(struct node)*3); if(!leaf){ printf("メモリ割り当てエラー\a\n"); exit(1); } endleaf.revleaf->nextleaf=&leaf[0]; leaf[0].revleaf=endleaf.revleaf; leaf[0].nextleaf=&leaf[1]; leaf[1].revleaf=&leaf[0]; leaf[1].nextleaf=&leaf[2]; leaf[2].revleaf=&leaf[1]; leaf[2].nextleaf=&endleaf; endleaf.revleaf=&leaf[2]; for(i=0;i<3;i++)
nodelist=&leaf[0]; leaf[0].nodelist=&leaf[1]; leaf[1].nodelist=&leaf[2]; leaf[2].nodelist=&nodeend; nodeend.revnode=&leaf[2]; for(i=0;i<3;i++){
nextleaf){ if(min>leaf->expectation){ min=leaf->expectation; /*リーフリストをたどってheuristic+costが最小のものを探す*/ minG=leaf; } leaf=leaf->nextleaf; } } void output(void) /*解を表示*/ { unsigned int i; struct node *p; p=minG; for(i=0;i<min;i++){
reverse->revnode=p; p=p->reverse; } printf("\n\n %d 手で完成\n\n\a",min); p=p->revnode; for(i=min;i;i--){ switch(p->ope){ /*解法を表示する*/ case 0: printf(" %c %c\n %c %c の面を: 180°回転\n\n" ,p->reverse->surface[p->opecolor][0][0],p->reverse->surface[p->opecolor][0][1] ,p->reverse->surface[p->opecolor][1][0],p->reverse->surface[p->opecolor][1][1]); break; case 1: printf(" %c %c\n %c %c の面を: 90°右に回転\n\n" ,p->reverse->surface[p->opecolor][0][0],p->reverse->surface[p->opecolor][0][1] ,p->reverse->surface[p->opecolor][1][0],p->reverse->surface[p->opecolor][1][1]); break; case -1: printf(" %c %c\n %c %c の面を: 左に 90°回転\n\n" ,p->reverse->surface[p->opecolor][0][0],p->reverse->surface[p->opecolor][0][1] ,p->reverse->surface[p->opecolor][1][0],p->reverse->surface[p->opecolor][1][1]); }; p=p->revnode; } }
/*おわり*/
※コメント投稿者のブログIDはブログ作成者のみに通知されます