質問編集履歴
3
情報が足りなかった
test
CHANGED
File without changes
|
test
CHANGED
@@ -152,6 +152,16 @@
|
|
152
152
|
|
153
153
|
|
154
154
|
|
155
|
+
char init_state[SIZE1] = {'8', '6', '7', '2', '5', '4', '3', '0', '1'};
|
156
|
+
|
157
|
+
char final_state[SIZE] = {'1', '2', '3', '4', '5', '6', '7', '8', '0'};
|
158
|
+
|
159
|
+
//最初のノードは親ノードを持たないのでこのあり得ないNULL_stateを親とする
|
160
|
+
|
161
|
+
char NULL_state[SIZE] = {'9', '9', '9' ,'9', '9', '9', '9', '9', '9'};
|
162
|
+
|
163
|
+
|
164
|
+
|
155
165
|
typedef struct List{
|
156
166
|
|
157
167
|
int cost;
|
@@ -194,6 +204,20 @@
|
|
194
204
|
|
195
205
|
|
196
206
|
|
207
|
+
//test
|
208
|
+
|
209
|
+
printf("node state\n");
|
210
|
+
|
211
|
+
for(int i = 0; i < SIZE; i++){
|
212
|
+
|
213
|
+
printf("%c ",node->state[i]);
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
printf("\n");
|
218
|
+
|
219
|
+
|
220
|
+
|
197
221
|
/*データをリストに追加する*/
|
198
222
|
|
199
223
|
if(ls == NULL)
|
@@ -300,13 +324,13 @@
|
|
300
324
|
|
301
325
|
|
302
326
|
|
303
|
-
int List_search(List* list, char
|
327
|
+
int List_search(List* list, char state[]){
|
304
328
|
|
305
329
|
|
306
330
|
|
307
331
|
for(List* node = list; node != NULL; node = node->next){
|
308
332
|
|
309
|
-
if(node->state
|
333
|
+
if(memcmp(node->state, state, SIZE) == 0){
|
310
334
|
|
311
335
|
same_state_cost = node->cost;
|
312
336
|
|
@@ -322,13 +346,31 @@
|
|
322
346
|
|
323
347
|
|
324
348
|
|
349
|
+
//char -> int
|
350
|
+
|
351
|
+
int ctoi(char c) {
|
352
|
+
|
353
|
+
if (c >= '0' && c <= '9') {
|
354
|
+
|
355
|
+
return c - '0';
|
356
|
+
|
357
|
+
}
|
358
|
+
|
359
|
+
return -1;
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
|
364
|
+
|
325
365
|
//空きマス(0)の取得
|
326
366
|
|
327
|
-
int get_space(char
|
367
|
+
int get_space(char state[]){
|
328
368
|
|
329
369
|
for(int i = 0;i < 9;i++){
|
330
370
|
|
331
|
-
if(state[i] ==
|
371
|
+
if(ctoi(state[i]) == 0){
|
372
|
+
|
373
|
+
printf("space:%d\n",i);
|
332
374
|
|
333
375
|
return i;
|
334
376
|
|
@@ -340,27 +382,57 @@
|
|
340
382
|
|
341
383
|
|
342
384
|
|
343
|
-
void generate_node(
|
385
|
+
void generate_node(char state[], int p_cost){
|
344
386
|
|
345
387
|
int space = get_space(state);
|
346
388
|
|
389
|
+
|
390
|
+
|
391
|
+
//test
|
392
|
+
|
393
|
+
printf("(g_n)space:%d\n",space);
|
394
|
+
|
347
395
|
|
348
396
|
|
349
397
|
int n;
|
350
398
|
|
351
399
|
memcpy(pre_state, state, SIZE); //親となるstateの保存
|
352
400
|
|
401
|
+
|
402
|
+
|
403
|
+
//test
|
404
|
+
|
405
|
+
printf("p_node:%s\n",pre_state); //ok
|
406
|
+
|
407
|
+
|
408
|
+
|
353
409
|
for(int i = 0; (n = adjacent[space][i]) != -1; i++ ){
|
354
410
|
|
411
|
+
//test
|
412
|
+
|
413
|
+
printf("n:%d\n",n); //OK
|
414
|
+
|
415
|
+
|
416
|
+
|
355
417
|
/* 移動 */
|
356
418
|
|
357
419
|
state[space] = state[n]; //スペースがあった場所にnにあったものを移動
|
358
420
|
|
359
|
-
state[n] = 0; //nをスペースにする
|
421
|
+
state[n] = '0'; //nをスペースにする
|
422
|
+
|
423
|
+
|
424
|
+
|
360
|
-
|
425
|
+
//test
|
426
|
+
|
361
|
-
|
427
|
+
printf("state[space]:%c\n", state[space]);
|
428
|
+
|
362
|
-
|
429
|
+
printf("state[n]:%c\n", state[n]);
|
430
|
+
|
431
|
+
printf("after move state:%s\n",state);
|
432
|
+
|
433
|
+
|
434
|
+
|
363
|
-
if(state
|
435
|
+
if(memcmp(state, final_state, SIZE) == 0){
|
364
436
|
|
365
437
|
printf("succeed\n");
|
366
438
|
|
@@ -390,7 +462,7 @@
|
|
390
462
|
|
391
463
|
for(List* node = olist; node != NULL; node = node->next){
|
392
464
|
|
393
|
-
if(node->next->state
|
465
|
+
if(memcmp(node->next->state, state, SIZE) == 0){
|
394
466
|
|
395
467
|
node->next = (node->next)->next; //同じstateのnodeを削除
|
396
468
|
|
@@ -402,113 +474,77 @@
|
|
402
474
|
|
403
475
|
}
|
404
476
|
|
405
|
-
|
406
|
-
|
407
477
|
}
|
408
478
|
|
409
479
|
}
|
410
480
|
|
411
|
-
|
412
|
-
|
413
481
|
}
|
414
482
|
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
}
|
483
|
+
}
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
|
488
|
+
|
420
|
-
|
489
|
+
//ここで探索を行う.
|
490
|
+
|
421
|
-
|
491
|
+
void search(){
|
492
|
+
|
422
|
-
|
493
|
+
int count = 0;
|
494
|
+
|
495
|
+
while(count < MAX_STATE){
|
496
|
+
|
497
|
+
for(List* node = olist; node != NULL; node = node->next){
|
498
|
+
|
499
|
+
if(node->cost < min_cost){
|
500
|
+
|
501
|
+
min_cost = node->cost;
|
502
|
+
|
503
|
+
|
504
|
+
|
423
|
-
/
|
505
|
+
//test
|
506
|
+
|
424
|
-
|
507
|
+
printf("min_cost:%d\n");
|
508
|
+
|
509
|
+
}
|
510
|
+
|
511
|
+
}
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
for(List* node = olist; node != NULL; node = node->next){
|
516
|
+
|
517
|
+
if(node->cost == min_cost){
|
518
|
+
|
425
|
-
|
519
|
+
generate_node(node->state, node->cost);
|
426
|
-
|
520
|
+
|
427
|
-
|
521
|
+
break;
|
522
|
+
|
428
|
-
|
523
|
+
}
|
524
|
+
|
525
|
+
}
|
526
|
+
|
527
|
+
|
528
|
+
|
529
|
+
//test
|
530
|
+
|
531
|
+
//printf("%d ",count); しっかり回っている
|
532
|
+
|
429
|
-
|
533
|
+
min_cost = MAX_STATE;
|
430
|
-
|
534
|
+
|
431
|
-
if(s
|
535
|
+
if(succeed_flag > 0) break;
|
432
|
-
|
536
|
+
|
433
|
-
|
537
|
+
count++; //無限ループを防ぐ
|
434
|
-
|
435
|
-
|
436
538
|
|
437
539
|
}
|
438
540
|
|
439
|
-
|
541
|
+
|
440
|
-
|
441
|
-
|
542
|
+
|
442
|
-
|
443
|
-
int p = state[x];
|
444
|
-
|
445
|
-
if (move_piece[n] == p) continue;
|
446
|
-
|
447
|
-
move_piece[n + 1] = p;
|
448
|
-
|
449
|
-
state[space] = p;
|
450
|
-
|
451
|
-
state[x] = S;
|
452
|
-
|
453
|
-
}
|
454
|
-
|
455
|
-
return state;
|
456
|
-
|
457
|
-
}
|
458
|
-
|
459
|
-
*/
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
char init_state[SIZE] = {8, 6, 7, 2, 5, 4, 3, 0, 1};
|
464
|
-
|
465
|
-
char final_state[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
|
466
|
-
|
467
|
-
//最初のノードは親ノードを持たないのでこのあり得ないNULL_stateを親とする
|
468
|
-
|
469
|
-
char NULL_state[SIZE] = {9, 9, 9 ,9, 9, 9, 9, 9, 9};
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
//
|
543
|
+
//test
|
474
|
-
|
475
|
-
|
544
|
+
|
476
|
-
|
477
|
-
int count = 0;
|
478
|
-
|
479
|
-
while(count < MAX_STATE){
|
480
|
-
|
481
|
-
|
545
|
+
//for(List* node = olist; node != NULL; node = node->next) printf("%s\n", node->state);
|
482
|
-
|
483
|
-
|
546
|
+
|
484
|
-
|
485
|
-
min_cost = node->cost;
|
486
|
-
|
487
|
-
}
|
488
|
-
|
489
|
-
}
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
547
|
+
//for(List* node = clist; node != NULL; node = node->next) printf("%s\n", node->state);
|
494
|
-
|
495
|
-
if(node->cost == min_cost){
|
496
|
-
|
497
|
-
generate_node(olist, clist, node->state, node->cost);
|
498
|
-
|
499
|
-
break;
|
500
|
-
|
501
|
-
}
|
502
|
-
|
503
|
-
}
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
if(succeed_flag > 0) break;
|
508
|
-
|
509
|
-
count++; //無限ループを防ぐ
|
510
|
-
|
511
|
-
}
|
512
548
|
|
513
549
|
|
514
550
|
|
@@ -526,6 +562,14 @@
|
|
526
562
|
|
527
563
|
min_cost = h1(init_state);
|
528
564
|
|
565
|
+
|
566
|
+
|
567
|
+
//test
|
568
|
+
|
569
|
+
//printf("init min_cost:%d\n",min_cost); OK
|
570
|
+
|
571
|
+
|
572
|
+
|
529
573
|
//headを入れる
|
530
574
|
|
531
575
|
olist = ListAdd(olist, MAX_STATE, NULL_state, NULL_state);
|
@@ -538,6 +582,8 @@
|
|
538
582
|
|
539
583
|
|
540
584
|
|
585
|
+
printf("Search start\n");
|
586
|
+
|
541
587
|
clock_t time = clock();
|
542
588
|
|
543
589
|
search();
|
@@ -559,165 +605,3 @@
|
|
559
605
|
|
560
606
|
|
561
607
|
```
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
####組みこもうとしているもの
|
566
|
-
|
567
|
-
search関数,まだ修正しきれていません,申し訳ございません.
|
568
|
-
|
569
|
-
```C
|
570
|
-
|
571
|
-
char space_postion[MAX_STATE];
|
572
|
-
|
573
|
-
char pre_state[SIZE];
|
574
|
-
|
575
|
-
int same_state_cost;
|
576
|
-
|
577
|
-
int succeed_flag = 0;
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
int adjacent[SIZE][5] = {
|
582
|
-
|
583
|
-
{1, 3, -1}, // 0
|
584
|
-
|
585
|
-
{0, 2, 4, -1}, // 1
|
586
|
-
|
587
|
-
{1, 5, -1}, // 2
|
588
|
-
|
589
|
-
{0, 4, 6, -1}, // 3
|
590
|
-
|
591
|
-
{1, 3, 5, 7, -1}, // 4
|
592
|
-
|
593
|
-
{2, 4, 8, -1}, // 5
|
594
|
-
|
595
|
-
{3, 7, -1}, // 6
|
596
|
-
|
597
|
-
{4, 6, 8, -1}, // 7
|
598
|
-
|
599
|
-
{5, 7, -1} // 8
|
600
|
-
|
601
|
-
};
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
int List_search(List* list, char* state){
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
for(List* node = list; node != NULL; node = node->next){
|
610
|
-
|
611
|
-
if(node->state == state){ //ここは少し違いそう
|
612
|
-
|
613
|
-
same_state_cost = node->cost;
|
614
|
-
|
615
|
-
return EXIST;
|
616
|
-
|
617
|
-
}
|
618
|
-
|
619
|
-
}
|
620
|
-
|
621
|
-
return NOEXIST;
|
622
|
-
|
623
|
-
}
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
//空きマス(0)の取得
|
628
|
-
|
629
|
-
int get_space(char* state){
|
630
|
-
|
631
|
-
for(int i = 0;i < 9;i++){
|
632
|
-
|
633
|
-
if(state[i] == '0'){
|
634
|
-
|
635
|
-
return i;
|
636
|
-
|
637
|
-
}
|
638
|
-
|
639
|
-
}
|
640
|
-
|
641
|
-
}
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
void generate_node(List* olist, List* clist, char* state, int p_cost){
|
646
|
-
|
647
|
-
int space = get_space(state);
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
int n;
|
652
|
-
|
653
|
-
memcpy(pre_state, state, SIZE); //親となるstateの保存
|
654
|
-
|
655
|
-
for(int i = 0; (n = adjacent[space][i]) != -1; i++ ){
|
656
|
-
|
657
|
-
/* 移動 */
|
658
|
-
|
659
|
-
state[space] = state[n]; //スペースがあった場所にnにあったものを移動
|
660
|
-
|
661
|
-
state[n] = 0; //nをスペースにする
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
if(state == final_state){
|
666
|
-
|
667
|
-
printf("succeed\n");
|
668
|
-
|
669
|
-
succeed_flag++;
|
670
|
-
|
671
|
-
return ;
|
672
|
-
|
673
|
-
}
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
if( List_search(clist, state) == NOEXIST ){
|
678
|
-
|
679
|
-
if( List_search(olist, state) == NOEXIST ){
|
680
|
-
|
681
|
-
//olistにもclistにも存在しないstateなら即olistに追加
|
682
|
-
|
683
|
-
ListAdd(olist, h1(state)+p_cost, state, pre_state);
|
684
|
-
|
685
|
-
}else{
|
686
|
-
|
687
|
-
//olistにのみ存在するときは,同じstateよりもコストが小さいなら追加
|
688
|
-
|
689
|
-
if(h1(state)+p_cost < same_state_cost){
|
690
|
-
|
691
|
-
//同じstateのnodeを削除するためにolistを最初から見ていく
|
692
|
-
|
693
|
-
for(List* node = olist; node != NULL; node = node->next){
|
694
|
-
|
695
|
-
if(node->next->state == state){ //ここもすこし違いそう
|
696
|
-
|
697
|
-
node->next = (node->next)->next; //同じstateのnodeを削除
|
698
|
-
|
699
|
-
ListAdd(olist, h1(state)+p_cost, state, pre_state); //olistに追加
|
700
|
-
|
701
|
-
}
|
702
|
-
|
703
|
-
}
|
704
|
-
|
705
|
-
}
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
}
|
710
|
-
|
711
|
-
}
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
}
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
}
|
722
|
-
|
723
|
-
```
|
2
内容の充実化
test
CHANGED
File without changes
|
test
CHANGED
@@ -168,189 +168,485 @@
|
|
168
168
|
|
169
169
|
|
170
170
|
|
171
|
-
/*
|
172
|
-
|
173
|
-
* リストを追加する為の関数
|
171
|
+
/* リストを追加する為の関数*/
|
172
|
+
|
174
|
-
|
173
|
+
List *ListAdd(List* ls, int cost, char state[], char parent_state[]){
|
174
|
+
|
175
|
-
*
|
175
|
+
/*メモリの確保*/
|
176
|
+
|
176
|
-
|
177
|
+
List* node = (List*)malloc(sizeof(List));
|
178
|
+
|
179
|
+
if(node == NULL)
|
180
|
+
|
181
|
+
return NULL;
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
/*データの追加*/
|
186
|
+
|
187
|
+
node->cost = cost;
|
188
|
+
|
189
|
+
memcpy(node->state,state, SIZE);
|
190
|
+
|
191
|
+
memcpy(node->parent_state,parent_state,SIZE);
|
192
|
+
|
193
|
+
node->next = NULL;
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
/*データをリストに追加する*/
|
198
|
+
|
199
|
+
if(ls == NULL)
|
200
|
+
|
201
|
+
return node;
|
202
|
+
|
203
|
+
List* search = ls;
|
204
|
+
|
205
|
+
while (search->next != NULL)
|
206
|
+
|
207
|
+
search = search->next;
|
208
|
+
|
177
|
-
|
209
|
+
search->next = node;
|
210
|
+
|
211
|
+
return ls;
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
/* リストを削除する為の関数*/
|
218
|
+
|
219
|
+
void ListAllDelete(List* ls){
|
220
|
+
|
221
|
+
List *tmp = ls;
|
222
|
+
|
223
|
+
while (ls->next != NULL){
|
224
|
+
|
225
|
+
tmp = ls->next;
|
226
|
+
|
227
|
+
ls->next = tmp->next;
|
228
|
+
|
229
|
+
free(tmp);
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
free(ls);
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
int h1(char state[]){
|
240
|
+
|
241
|
+
int count = 0;
|
242
|
+
|
243
|
+
for(int i = 0;i < 9;i++){
|
244
|
+
|
245
|
+
if(state[i] != final_state[i]) count++;
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
return count;
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
int min_cost;
|
256
|
+
|
257
|
+
List* olist = NULL;
|
258
|
+
|
259
|
+
List* clist = NULL;
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
//動かした数字
|
264
|
+
|
265
|
+
//int move_piece[MAX_MOVE + 1];
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
char space_postion[MAX_STATE];
|
270
|
+
|
271
|
+
char pre_state[SIZE];
|
272
|
+
|
273
|
+
int same_state_cost;
|
274
|
+
|
275
|
+
int succeed_flag = 0;
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
int adjacent[SIZE][5] = {
|
280
|
+
|
281
|
+
{1, 3, -1}, // 0
|
282
|
+
|
283
|
+
{0, 2, 4, -1}, // 1
|
284
|
+
|
285
|
+
{1, 5, -1}, // 2
|
286
|
+
|
287
|
+
{0, 4, 6, -1}, // 3
|
288
|
+
|
289
|
+
{1, 3, 5, 7, -1}, // 4
|
290
|
+
|
291
|
+
{2, 4, 8, -1}, // 5
|
292
|
+
|
293
|
+
{3, 7, -1}, // 6
|
294
|
+
|
295
|
+
{4, 6, 8, -1}, // 7
|
296
|
+
|
297
|
+
{5, 7, -1} // 8
|
298
|
+
|
299
|
+
};
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
int List_search(List* list, char* state){
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
for(List* node = list; node != NULL; node = node->next){
|
308
|
+
|
309
|
+
if(node->state == state){ //ここは少し違いそう
|
310
|
+
|
311
|
+
same_state_cost = node->cost;
|
312
|
+
|
313
|
+
return EXIST;
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
}
|
318
|
+
|
319
|
+
return NOEXIST;
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
//空きマス(0)の取得
|
326
|
+
|
327
|
+
int get_space(char* state){
|
328
|
+
|
329
|
+
for(int i = 0;i < 9;i++){
|
330
|
+
|
331
|
+
if(state[i] == '0'){
|
332
|
+
|
333
|
+
return i;
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
void generate_node(List* olist, List* clist, char* state, int p_cost){
|
344
|
+
|
345
|
+
int space = get_space(state);
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
int n;
|
350
|
+
|
351
|
+
memcpy(pre_state, state, SIZE); //親となるstateの保存
|
352
|
+
|
353
|
+
for(int i = 0; (n = adjacent[space][i]) != -1; i++ ){
|
354
|
+
|
355
|
+
/* 移動 */
|
356
|
+
|
357
|
+
state[space] = state[n]; //スペースがあった場所にnにあったものを移動
|
358
|
+
|
359
|
+
state[n] = 0; //nをスペースにする
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
if(state == final_state){
|
364
|
+
|
365
|
+
printf("succeed\n");
|
366
|
+
|
367
|
+
succeed_flag++;
|
368
|
+
|
369
|
+
return ;
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
if( List_search(clist, state) == NOEXIST ){
|
376
|
+
|
377
|
+
if( List_search(olist, state) == NOEXIST ){
|
378
|
+
|
379
|
+
//olistにもclistにも存在しないstateなら即olistに追加
|
380
|
+
|
381
|
+
ListAdd(olist, h1(state)+p_cost, state, pre_state);
|
382
|
+
|
383
|
+
}else{
|
384
|
+
|
385
|
+
//olistにのみ存在するときは,同じstateよりもコストが小さいなら追加
|
386
|
+
|
387
|
+
if(h1(state)+p_cost < same_state_cost){
|
388
|
+
|
389
|
+
//同じstateのnodeを削除するためにolistを最初から見ていく
|
390
|
+
|
391
|
+
for(List* node = olist; node != NULL; node = node->next){
|
392
|
+
|
393
|
+
if(node->next->state == state){ //ここもすこし違いそう
|
394
|
+
|
395
|
+
node->next = (node->next)->next; //同じstateのnodeを削除
|
396
|
+
|
397
|
+
ListAdd(olist, h1(state)+p_cost, state, pre_state); //olistに追加
|
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
|
+
/* IDSのやり方
|
424
|
+
|
425
|
+
char* generate_node(char* state){
|
426
|
+
|
427
|
+
int space;
|
428
|
+
|
429
|
+
for(int i = 0;i < 9;i++){
|
430
|
+
|
431
|
+
if(state[i] == '0')
|
432
|
+
|
433
|
+
space = i;
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
}
|
438
|
+
|
439
|
+
for(int i = 0; adjacent[space][i] != -1; i++){
|
440
|
+
|
441
|
+
int x = adjacent[space][i];
|
442
|
+
|
443
|
+
int p = state[x];
|
444
|
+
|
445
|
+
if (move_piece[n] == p) continue;
|
446
|
+
|
447
|
+
move_piece[n + 1] = p;
|
448
|
+
|
449
|
+
state[space] = p;
|
450
|
+
|
451
|
+
state[x] = S;
|
452
|
+
|
453
|
+
}
|
454
|
+
|
455
|
+
return state;
|
456
|
+
|
457
|
+
}
|
178
458
|
|
179
459
|
*/
|
180
460
|
|
461
|
+
|
462
|
+
|
181
|
-
|
463
|
+
char init_state[SIZE] = {8, 6, 7, 2, 5, 4, 3, 0, 1};
|
464
|
+
|
182
|
-
|
465
|
+
char final_state[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
|
466
|
+
|
467
|
+
//最初のノードは親ノードを持たないのでこのあり得ないNULL_stateを親とする
|
468
|
+
|
469
|
+
char NULL_state[SIZE] = {9, 9, 9 ,9, 9, 9, 9, 9, 9};
|
470
|
+
|
471
|
+
|
472
|
+
|
183
|
-
|
473
|
+
//ここで探索を行う.
|
184
|
-
|
185
|
-
|
474
|
+
|
186
|
-
|
187
|
-
if(node == NULL)
|
188
|
-
|
189
|
-
return NULL;
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
/*データの追加*/
|
194
|
-
|
195
|
-
node->cost = cost;
|
196
|
-
|
197
|
-
memcpy(node->state,state, SIZE);
|
198
|
-
|
199
|
-
memcpy(node->parent_state,parent_state,SIZE);
|
200
|
-
|
201
|
-
node->next = NULL;
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
/*データをリストに追加する*/
|
206
|
-
|
207
|
-
if(ls == NULL)
|
208
|
-
|
209
|
-
return node;
|
210
|
-
|
211
|
-
|
475
|
+
void search(){
|
212
|
-
|
213
|
-
while (search->next != NULL)
|
214
|
-
|
215
|
-
search = search->next;
|
216
|
-
|
217
|
-
search->next = node;
|
218
|
-
|
219
|
-
return ls;
|
220
|
-
|
221
|
-
}
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
/*
|
226
|
-
|
227
|
-
* リストを削除する為の関数
|
228
|
-
|
229
|
-
* 第一引数:削除するリストのポインタ
|
230
|
-
|
231
|
-
* 戻り値:なし
|
232
|
-
|
233
|
-
*/
|
234
|
-
|
235
|
-
void ListAllDelete(List* ls){
|
236
|
-
|
237
|
-
List *tmp = ls;
|
238
|
-
|
239
|
-
while (ls->next != NULL){
|
240
|
-
|
241
|
-
tmp = ls->next;
|
242
|
-
|
243
|
-
ls->next = tmp->next;
|
244
|
-
|
245
|
-
free(tmp);
|
246
|
-
|
247
|
-
}
|
248
|
-
|
249
|
-
free(ls);
|
250
|
-
|
251
|
-
}
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
int h1(char state[]){
|
256
476
|
|
257
477
|
int count = 0;
|
258
478
|
|
479
|
+
while(count < MAX_STATE){
|
480
|
+
|
481
|
+
for(List* node = olist; node != NULL; node = node->next){
|
482
|
+
|
483
|
+
if(node->cost < min_cost){
|
484
|
+
|
485
|
+
min_cost = node->cost;
|
486
|
+
|
487
|
+
}
|
488
|
+
|
489
|
+
}
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
for(List* node = olist; node != NULL; node = node->next){
|
494
|
+
|
495
|
+
if(node->cost == min_cost){
|
496
|
+
|
497
|
+
generate_node(olist, clist, node->state, node->cost);
|
498
|
+
|
499
|
+
break;
|
500
|
+
|
501
|
+
}
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
if(succeed_flag > 0) break;
|
508
|
+
|
509
|
+
count++; //無限ループを防ぐ
|
510
|
+
|
511
|
+
}
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
printf("search failed\n");
|
516
|
+
|
517
|
+
|
518
|
+
|
519
|
+
}
|
520
|
+
|
521
|
+
|
522
|
+
|
523
|
+
void main(){
|
524
|
+
|
525
|
+
|
526
|
+
|
527
|
+
min_cost = h1(init_state);
|
528
|
+
|
529
|
+
//headを入れる
|
530
|
+
|
531
|
+
olist = ListAdd(olist, MAX_STATE, NULL_state, NULL_state);
|
532
|
+
|
533
|
+
clist = ListAdd(clist, MAX_STATE, NULL_state, NULL_state);
|
534
|
+
|
535
|
+
|
536
|
+
|
537
|
+
olist = ListAdd(olist, h1(init_state), init_state, NULL_state); //最初の親はNULLにしたいが怒られている
|
538
|
+
|
539
|
+
|
540
|
+
|
541
|
+
clock_t time = clock();
|
542
|
+
|
543
|
+
search();
|
544
|
+
|
545
|
+
double result = (double)(clock() - time)/CLOCKS_PER_SEC;
|
546
|
+
|
547
|
+
printf("%.3fsec\n", result); //探索にかかった時間を表示
|
548
|
+
|
549
|
+
|
550
|
+
|
551
|
+
ListAllDelete(olist);
|
552
|
+
|
553
|
+
ListAllDelete(clist);
|
554
|
+
|
555
|
+
return;
|
556
|
+
|
557
|
+
}
|
558
|
+
|
559
|
+
|
560
|
+
|
561
|
+
```
|
562
|
+
|
563
|
+
|
564
|
+
|
565
|
+
####組みこもうとしているもの
|
566
|
+
|
567
|
+
search関数,まだ修正しきれていません,申し訳ございません.
|
568
|
+
|
569
|
+
```C
|
570
|
+
|
571
|
+
char space_postion[MAX_STATE];
|
572
|
+
|
573
|
+
char pre_state[SIZE];
|
574
|
+
|
575
|
+
int same_state_cost;
|
576
|
+
|
577
|
+
int succeed_flag = 0;
|
578
|
+
|
579
|
+
|
580
|
+
|
581
|
+
int adjacent[SIZE][5] = {
|
582
|
+
|
583
|
+
{1, 3, -1}, // 0
|
584
|
+
|
585
|
+
{0, 2, 4, -1}, // 1
|
586
|
+
|
587
|
+
{1, 5, -1}, // 2
|
588
|
+
|
589
|
+
{0, 4, 6, -1}, // 3
|
590
|
+
|
591
|
+
{1, 3, 5, 7, -1}, // 4
|
592
|
+
|
593
|
+
{2, 4, 8, -1}, // 5
|
594
|
+
|
595
|
+
{3, 7, -1}, // 6
|
596
|
+
|
597
|
+
{4, 6, 8, -1}, // 7
|
598
|
+
|
599
|
+
{5, 7, -1} // 8
|
600
|
+
|
601
|
+
};
|
602
|
+
|
603
|
+
|
604
|
+
|
605
|
+
int List_search(List* list, char* state){
|
606
|
+
|
607
|
+
|
608
|
+
|
609
|
+
for(List* node = list; node != NULL; node = node->next){
|
610
|
+
|
611
|
+
if(node->state == state){ //ここは少し違いそう
|
612
|
+
|
613
|
+
same_state_cost = node->cost;
|
614
|
+
|
615
|
+
return EXIST;
|
616
|
+
|
617
|
+
}
|
618
|
+
|
619
|
+
}
|
620
|
+
|
621
|
+
return NOEXIST;
|
622
|
+
|
623
|
+
}
|
624
|
+
|
625
|
+
|
626
|
+
|
627
|
+
//空きマス(0)の取得
|
628
|
+
|
629
|
+
int get_space(char* state){
|
630
|
+
|
259
631
|
for(int i = 0;i < 9;i++){
|
260
632
|
|
261
|
-
if(state[i]
|
633
|
+
if(state[i] == '0'){
|
262
|
-
|
263
|
-
|
634
|
+
|
264
|
-
|
265
|
-
return
|
635
|
+
return i;
|
266
|
-
|
267
|
-
}
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
int min_cost;
|
272
|
-
|
273
|
-
List* olist = NULL;
|
274
|
-
|
275
|
-
List* clist = NULL;
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
//動かした数字
|
280
|
-
|
281
|
-
int move_piece[MAX_MOVE + 1];
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
char space_postion[MAX_STATE];
|
286
|
-
|
287
|
-
char pre_state[SIZE];
|
288
|
-
|
289
|
-
int same_state_cost;
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
int adjacent[SIZE][5] = {
|
294
|
-
|
295
|
-
{1, 3, -1}, // 0
|
296
|
-
|
297
|
-
{0, 2, 4, -1}, // 1
|
298
|
-
|
299
|
-
{1, 5, -1}, // 2
|
300
|
-
|
301
|
-
{0, 4, 6, -1}, // 3
|
302
|
-
|
303
|
-
{1, 3, 5, 7, -1}, // 4
|
304
|
-
|
305
|
-
{2, 4, 8, -1}, // 5
|
306
|
-
|
307
|
-
{3, 7, -1}, // 6
|
308
|
-
|
309
|
-
{4, 6, 8, -1}, // 7
|
310
|
-
|
311
|
-
{5, 7, -1} // 8
|
312
|
-
|
313
|
-
};
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
int List_search(List* list, char* state){
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
for(List* node = list; node != NULL; node = node->next){
|
322
|
-
|
323
|
-
if(node->state == state){ //ここは少し違いそう
|
324
|
-
|
325
|
-
same_state_cost = node->cost;
|
326
|
-
|
327
|
-
return EXIST;
|
328
636
|
|
329
637
|
}
|
330
638
|
|
331
639
|
}
|
332
640
|
|
333
|
-
return NOEXIST;
|
334
|
-
|
335
641
|
}
|
336
642
|
|
337
643
|
|
338
644
|
|
339
645
|
void generate_node(List* olist, List* clist, char* state, int p_cost){
|
340
646
|
|
341
|
-
int space;
|
342
|
-
|
343
|
-
for(int i = 0;i < 9;i++){
|
344
|
-
|
345
|
-
|
647
|
+
int space = get_space(state);
|
346
|
-
|
347
|
-
|
648
|
+
|
348
|
-
|
349
|
-
|
649
|
+
|
350
|
-
|
351
|
-
}
|
352
|
-
|
353
|
-
}
|
354
650
|
|
355
651
|
int n;
|
356
652
|
|
@@ -364,6 +660,20 @@
|
|
364
660
|
|
365
661
|
state[n] = 0; //nをスペースにする
|
366
662
|
|
663
|
+
|
664
|
+
|
665
|
+
if(state == final_state){
|
666
|
+
|
667
|
+
printf("succeed\n");
|
668
|
+
|
669
|
+
succeed_flag++;
|
670
|
+
|
671
|
+
return ;
|
672
|
+
|
673
|
+
}
|
674
|
+
|
675
|
+
|
676
|
+
|
367
677
|
if( List_search(clist, state) == NOEXIST ){
|
368
678
|
|
369
679
|
if( List_search(olist, state) == NOEXIST ){
|
@@ -378,6 +688,8 @@
|
|
378
688
|
|
379
689
|
if(h1(state)+p_cost < same_state_cost){
|
380
690
|
|
691
|
+
//同じstateのnodeを削除するためにolistを最初から見ていく
|
692
|
+
|
381
693
|
for(List* node = olist; node != NULL; node = node->next){
|
382
694
|
|
383
695
|
if(node->next->state == state){ //ここもすこし違いそう
|
@@ -408,322 +720,4 @@
|
|
408
720
|
|
409
721
|
}
|
410
722
|
|
411
|
-
|
412
|
-
|
413
|
-
/* IDSのやり方
|
414
|
-
|
415
|
-
char* generate_node(char* state){
|
416
|
-
|
417
|
-
int space;
|
418
|
-
|
419
|
-
for(int i = 0;i < 9;i++){
|
420
|
-
|
421
|
-
if(state[i] == '0')
|
422
|
-
|
423
|
-
space = i;
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
}
|
428
|
-
|
429
|
-
for(int i = 0; adjacent[space][i] != -1; i++){
|
430
|
-
|
431
|
-
int x = adjacent[space][i];
|
432
|
-
|
433
|
-
int p = state[x];
|
434
|
-
|
435
|
-
if (move_piece[n] == p) continue;
|
436
|
-
|
437
|
-
move_piece[n + 1] = p;
|
438
|
-
|
439
|
-
state[space] = p;
|
440
|
-
|
441
|
-
state[x] = S;
|
442
|
-
|
443
|
-
}
|
444
|
-
|
445
|
-
return state;
|
446
|
-
|
447
|
-
}
|
448
|
-
|
449
|
-
*/
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
char init_state[SIZE] = {8, 6, 7, 2, 5, 4, 3, 0, 1};
|
454
|
-
|
455
|
-
char final_state[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
|
456
|
-
|
457
|
-
//最初のノードは親ノードを持たないのでこのあり得ないNULL_stateを親とする
|
458
|
-
|
459
|
-
char NULL_state[SIZE] = {9, 9, 9 ,9, 9, 9, 9, 9, 9};
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
//ここで探索を行う.
|
464
|
-
|
465
|
-
void search(){
|
466
|
-
|
467
|
-
int count = 0;
|
468
|
-
|
469
|
-
while(count < MAX_STATE){
|
470
|
-
|
471
|
-
for(List* node = olist; node != NULL; node = node->next){
|
472
|
-
|
473
|
-
if(node->cost < min_cost){
|
474
|
-
|
475
|
-
min_cost = node->cost;
|
476
|
-
|
477
|
-
}
|
478
|
-
|
479
|
-
}
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
for(List* node = olist; node != NULL; node = node->next){
|
484
|
-
|
485
|
-
if((node->next)->cost == min_cost){
|
486
|
-
|
487
|
-
//子ノードの生成
|
488
|
-
|
489
|
-
node->next = (node->next)->next; //nodeの削除
|
490
|
-
|
491
|
-
//clistに削除したノードを追加
|
492
|
-
|
493
|
-
clist = ListAdd(clist, (node->next)->cost, (node->next)->state, node->state);
|
494
|
-
|
495
|
-
if(/*子のstateがcloseリストにない*/1){
|
496
|
-
|
497
|
-
//子ノード->cost = h1(子ノードのstate) + 親のcost;
|
498
|
-
|
499
|
-
if(/*子のstateがopenにある*/1){
|
500
|
-
|
501
|
-
if(/*今の見てるノードのコスト < openに保存してあるコスト*/1){
|
502
|
-
|
503
|
-
//ノードの貼り換え
|
504
|
-
|
505
|
-
}
|
506
|
-
|
507
|
-
}
|
508
|
-
|
509
|
-
else if(/*子のstateがopenにない*/1){
|
510
|
-
|
511
|
-
if(/*h1(子node->state) == 0*/){
|
512
|
-
|
513
|
-
printf("search finished\n");
|
514
|
-
|
515
|
-
goto FINISH;
|
516
|
-
|
517
|
-
}
|
518
|
-
|
519
|
-
else {
|
520
|
-
|
521
|
-
//子ノードをopenリストに入れる.
|
522
|
-
|
523
|
-
//olist = ListAdd(olist, cost, NULL_state, NULL_state);
|
524
|
-
|
525
|
-
}
|
526
|
-
|
527
|
-
//olist = OlistAdd(olist, h1(子のstate)+親のcost, 親);
|
528
|
-
|
529
|
-
}
|
530
|
-
|
531
|
-
}
|
532
|
-
|
533
|
-
}
|
534
|
-
|
535
|
-
}
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
count++; //無限ループを防ぐ
|
540
|
-
|
541
|
-
}
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
printf("search failed\n");
|
546
|
-
|
547
|
-
FINISH:
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
}
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
void main(){
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
min_cost = h1(init_state);
|
560
|
-
|
561
|
-
//headを入れる
|
562
|
-
|
563
|
-
olist = ListAdd(olist, MAX_STATE, NULL_state, NULL_state);
|
564
|
-
|
565
|
-
clist = ListAdd(clist, MAX_STATE, NULL_state, NULL_state);
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
olist = ListAdd(olist, h1(init_state), init_state, NULL_state); //最初の親はNULLにしたいが怒られている
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
clock_t time = clock();
|
574
|
-
|
575
|
-
search();
|
576
|
-
|
577
|
-
double result = (double)(clock() - time)/CLOCKS_PER_SEC;
|
578
|
-
|
579
|
-
printf("%.3fsec\n", result); //探索にかかった時間を表示
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
ListAllDelete(olist);
|
584
|
-
|
585
|
-
ListAllDelete(clist);
|
586
|
-
|
587
|
-
return;
|
588
|
-
|
589
|
-
}
|
590
|
-
|
591
|
-
|
592
|
-
|
593
723
|
```
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
####組みこもうとしているもの
|
598
|
-
|
599
|
-
node-generateとsearch関数で同じ条件分岐がありますが,まだ修正しきれていません,申し訳ございません.
|
600
|
-
|
601
|
-
```C
|
602
|
-
|
603
|
-
har space_postion[MAX_STATE];
|
604
|
-
|
605
|
-
char pre_state[SIZE];
|
606
|
-
|
607
|
-
int same_state_cost;
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
int adjacent[SIZE][5] = {
|
612
|
-
|
613
|
-
{1, 3, -1}, // 0
|
614
|
-
|
615
|
-
{0, 2, 4, -1}, // 1
|
616
|
-
|
617
|
-
{1, 5, -1}, // 2
|
618
|
-
|
619
|
-
{0, 4, 6, -1}, // 3
|
620
|
-
|
621
|
-
{1, 3, 5, 7, -1}, // 4
|
622
|
-
|
623
|
-
{2, 4, 8, -1}, // 5
|
624
|
-
|
625
|
-
{3, 7, -1}, // 6
|
626
|
-
|
627
|
-
{4, 6, 8, -1}, // 7
|
628
|
-
|
629
|
-
{5, 7, -1} // 8
|
630
|
-
|
631
|
-
};
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
int List_search(List* list, char* state){
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
for(List* node = list; node != NULL; node = node->next){
|
640
|
-
|
641
|
-
if(node->state == state){ //ここは少し違いそう
|
642
|
-
|
643
|
-
same_state_cost = node->cost;
|
644
|
-
|
645
|
-
return EXIST;
|
646
|
-
|
647
|
-
}
|
648
|
-
|
649
|
-
}
|
650
|
-
|
651
|
-
return NOEXIST;
|
652
|
-
|
653
|
-
}
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
void generate_node(List* olist, List* clist, char* state, int p_cost){
|
658
|
-
|
659
|
-
int space;
|
660
|
-
|
661
|
-
for(int i = 0;i < 9;i++){
|
662
|
-
|
663
|
-
if(state[i] == '0'){
|
664
|
-
|
665
|
-
space = i;
|
666
|
-
|
667
|
-
break;
|
668
|
-
|
669
|
-
}
|
670
|
-
|
671
|
-
}
|
672
|
-
|
673
|
-
int n;
|
674
|
-
|
675
|
-
memcpy(pre_state, state, SIZE); //親となるstateの保存
|
676
|
-
|
677
|
-
for(int i = 0; (n = adjacent[space][i]) != -1; i++ ){
|
678
|
-
|
679
|
-
/* 移動 */
|
680
|
-
|
681
|
-
state[space] = state[n]; //スペースがあった場所にnにあったものを移動
|
682
|
-
|
683
|
-
state[n] = 0; //nをスペースにする
|
684
|
-
|
685
|
-
if( List_search(clist, state) == NOEXIST ){
|
686
|
-
|
687
|
-
if( List_search(olist, state) == NOEXIST ){
|
688
|
-
|
689
|
-
//olistにもclistにも存在しないstateなら即olistに追加
|
690
|
-
|
691
|
-
ListAdd(olist, h1(state)+p_cost, state, pre_state);
|
692
|
-
|
693
|
-
}else{
|
694
|
-
|
695
|
-
//olistにのみ存在するときは,同じstateよりもコストが小さいなら追加
|
696
|
-
|
697
|
-
if(h1(state)+p_cost < same_state_cost){
|
698
|
-
|
699
|
-
for(List* node = olist; node != NULL; node = node->next){
|
700
|
-
|
701
|
-
if(node->next->state == state){ //ここもすこし違いそう
|
702
|
-
|
703
|
-
node->next = (node->next)->next; //同じstateのnodeを削除
|
704
|
-
|
705
|
-
ListAdd(olist, h1(state)+p_cost, state, pre_state); //olistに追加
|
706
|
-
|
707
|
-
}
|
708
|
-
|
709
|
-
}
|
710
|
-
|
711
|
-
}
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
}
|
716
|
-
|
717
|
-
}
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
}
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
}
|
728
|
-
|
729
|
-
```
|
1
内容の充実化
test
CHANGED
File without changes
|
test
CHANGED
@@ -52,6 +52,74 @@
|
|
52
52
|
|
53
53
|
|
54
54
|
|
55
|
+
コードを編集いたしましたので追記にて記します.
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
###質問内容
|
64
|
+
|
65
|
+
・~~次にあげる部分を現状のコードに組み込んで子ノードを生成させたいのですが,私が考えている組み込み方の直す点を教えていただきたいです.反復深化という方法で子ノードを生成するときに用いています.~~
|
66
|
+
|
67
|
+
[反復深化](http://www.nct9.ne.jp/m_hiroi/linux/clang23.html)
|
68
|
+
|
69
|
+
~~説明を加えます.
|
70
|
+
|
71
|
+
adjacentは
|
72
|
+
|
73
|
+
0|1|2
|
74
|
+
|
75
|
+
3|4|5
|
76
|
+
|
77
|
+
6|7|8
|
78
|
+
|
79
|
+
と配置したときの隣り合う数字を示しています.例えば0ならば1, 3とのみ隣り合っています.隣り合う数字がもう出尽くしたという意味で-1を用いています.~~
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
~~print_answerは下位にたどり着くまでに動かした数字を格納しています~~.
|
84
|
+
|
85
|
+
深さ優先では厳しいので幅優先にします,内容派追記にて書かせていただきます.
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
####組みこもうとしているもの
|
96
|
+
|
97
|
+
~~上のコードでのnの扱いが上手くできていません.
|
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
|
+
|
55
123
|
```C
|
56
124
|
|
57
125
|
#include <stdlib.h>
|
@@ -64,12 +132,24 @@
|
|
64
132
|
|
65
133
|
|
66
134
|
|
67
|
-
|
135
|
+
#define TRUE 1
|
136
|
+
|
137
|
+
#define FALSE 0
|
138
|
+
|
139
|
+
#define GOAL 2
|
68
140
|
|
69
141
|
#define SIZE 9
|
70
142
|
|
143
|
+
#define S 0
|
144
|
+
|
71
145
|
#define MAX_STATE 181440
|
72
146
|
|
147
|
+
#define MAX_MOVE 31
|
148
|
+
|
149
|
+
#define EXIST 1
|
150
|
+
|
151
|
+
#define NOEXIST 0
|
152
|
+
|
73
153
|
|
74
154
|
|
75
155
|
typedef struct List{
|
@@ -196,6 +276,180 @@
|
|
196
276
|
|
197
277
|
|
198
278
|
|
279
|
+
//動かした数字
|
280
|
+
|
281
|
+
int move_piece[MAX_MOVE + 1];
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
char space_postion[MAX_STATE];
|
286
|
+
|
287
|
+
char pre_state[SIZE];
|
288
|
+
|
289
|
+
int same_state_cost;
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
int adjacent[SIZE][5] = {
|
294
|
+
|
295
|
+
{1, 3, -1}, // 0
|
296
|
+
|
297
|
+
{0, 2, 4, -1}, // 1
|
298
|
+
|
299
|
+
{1, 5, -1}, // 2
|
300
|
+
|
301
|
+
{0, 4, 6, -1}, // 3
|
302
|
+
|
303
|
+
{1, 3, 5, 7, -1}, // 4
|
304
|
+
|
305
|
+
{2, 4, 8, -1}, // 5
|
306
|
+
|
307
|
+
{3, 7, -1}, // 6
|
308
|
+
|
309
|
+
{4, 6, 8, -1}, // 7
|
310
|
+
|
311
|
+
{5, 7, -1} // 8
|
312
|
+
|
313
|
+
};
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
int List_search(List* list, char* state){
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
for(List* node = list; node != NULL; node = node->next){
|
322
|
+
|
323
|
+
if(node->state == state){ //ここは少し違いそう
|
324
|
+
|
325
|
+
same_state_cost = node->cost;
|
326
|
+
|
327
|
+
return EXIST;
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
return NOEXIST;
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
void generate_node(List* olist, List* clist, char* state, int p_cost){
|
340
|
+
|
341
|
+
int space;
|
342
|
+
|
343
|
+
for(int i = 0;i < 9;i++){
|
344
|
+
|
345
|
+
if(state[i] == '0'){
|
346
|
+
|
347
|
+
space = i;
|
348
|
+
|
349
|
+
break;
|
350
|
+
|
351
|
+
}
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
int n;
|
356
|
+
|
357
|
+
memcpy(pre_state, state, SIZE); //親となるstateの保存
|
358
|
+
|
359
|
+
for(int i = 0; (n = adjacent[space][i]) != -1; i++ ){
|
360
|
+
|
361
|
+
/* 移動 */
|
362
|
+
|
363
|
+
state[space] = state[n]; //スペースがあった場所にnにあったものを移動
|
364
|
+
|
365
|
+
state[n] = 0; //nをスペースにする
|
366
|
+
|
367
|
+
if( List_search(clist, state) == NOEXIST ){
|
368
|
+
|
369
|
+
if( List_search(olist, state) == NOEXIST ){
|
370
|
+
|
371
|
+
//olistにもclistにも存在しないstateなら即olistに追加
|
372
|
+
|
373
|
+
ListAdd(olist, h1(state)+p_cost, state, pre_state);
|
374
|
+
|
375
|
+
}else{
|
376
|
+
|
377
|
+
//olistにのみ存在するときは,同じstateよりもコストが小さいなら追加
|
378
|
+
|
379
|
+
if(h1(state)+p_cost < same_state_cost){
|
380
|
+
|
381
|
+
for(List* node = olist; node != NULL; node = node->next){
|
382
|
+
|
383
|
+
if(node->next->state == state){ //ここもすこし違いそう
|
384
|
+
|
385
|
+
node->next = (node->next)->next; //同じstateのnodeを削除
|
386
|
+
|
387
|
+
ListAdd(olist, h1(state)+p_cost, state, pre_state); //olistに追加
|
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
|
+
/* IDSのやり方
|
414
|
+
|
415
|
+
char* generate_node(char* state){
|
416
|
+
|
417
|
+
int space;
|
418
|
+
|
419
|
+
for(int i = 0;i < 9;i++){
|
420
|
+
|
421
|
+
if(state[i] == '0')
|
422
|
+
|
423
|
+
space = i;
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
}
|
428
|
+
|
429
|
+
for(int i = 0; adjacent[space][i] != -1; i++){
|
430
|
+
|
431
|
+
int x = adjacent[space][i];
|
432
|
+
|
433
|
+
int p = state[x];
|
434
|
+
|
435
|
+
if (move_piece[n] == p) continue;
|
436
|
+
|
437
|
+
move_piece[n + 1] = p;
|
438
|
+
|
439
|
+
state[space] = p;
|
440
|
+
|
441
|
+
state[x] = S;
|
442
|
+
|
443
|
+
}
|
444
|
+
|
445
|
+
return state;
|
446
|
+
|
447
|
+
}
|
448
|
+
|
449
|
+
*/
|
450
|
+
|
451
|
+
|
452
|
+
|
199
453
|
char init_state[SIZE] = {8, 6, 7, 2, 5, 4, 3, 0, 1};
|
200
454
|
|
201
455
|
char final_state[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
|
@@ -340,37 +594,21 @@
|
|
340
594
|
|
341
595
|
|
342
596
|
|
343
|
-
###
|
597
|
+
####組みこもうとしているもの
|
344
|
-
|
345
|
-
|
598
|
+
|
346
|
-
|
347
|
-
[反復深化](http://www.nct9.ne.jp/m_hiroi/linux/clang23.html)
|
348
|
-
|
349
|
-
説明を加えます.
|
350
|
-
|
351
|
-
adjacentは
|
352
|
-
|
353
|
-
0|1|2
|
354
|
-
|
355
|
-
3|4|5
|
356
|
-
|
357
|
-
6|7|8
|
358
|
-
|
359
|
-
と配置したときの隣り合う数字を示しています.例えば0ならば1, 3とのみ隣り合っています.隣り合う数字がもう出尽くしたという意味で-1を用いています.
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
599
|
+
node-generateとsearch関数で同じ条件分岐がありますが,まだ修正しきれていません,申し訳ございません.
|
364
|
-
|
365
|
-
|
366
600
|
|
367
601
|
```C
|
368
602
|
|
603
|
+
har space_postion[MAX_STATE];
|
604
|
+
|
369
|
-
|
605
|
+
char pre_state[SIZE];
|
606
|
+
|
370
|
-
|
607
|
+
int same_state_cost;
|
371
|
-
|
372
|
-
|
608
|
+
|
609
|
+
|
610
|
+
|
373
|
-
int adjacent[
|
611
|
+
int adjacent[SIZE][5] = {
|
374
612
|
|
375
613
|
{1, 3, -1}, // 0
|
376
614
|
|
@@ -394,130 +632,98 @@
|
|
394
632
|
|
395
633
|
|
396
634
|
|
397
|
-
char
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
in
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
if
|
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
|
-
|
635
|
+
int List_search(List* list, char* state){
|
636
|
+
|
637
|
+
|
638
|
+
|
639
|
+
for(List* node = list; node != NULL; node = node->next){
|
640
|
+
|
641
|
+
if(node->state == state){ //ここは少し違いそう
|
642
|
+
|
643
|
+
same_state_cost = node->cost;
|
644
|
+
|
645
|
+
return EXIST;
|
646
|
+
|
647
|
+
}
|
648
|
+
|
649
|
+
}
|
650
|
+
|
651
|
+
return NOEXIST;
|
652
|
+
|
653
|
+
}
|
654
|
+
|
655
|
+
|
656
|
+
|
657
|
+
void generate_node(List* olist, List* clist, char* state, int p_cost){
|
658
|
+
|
659
|
+
int space;
|
660
|
+
|
661
|
+
for(int i = 0;i < 9;i++){
|
662
|
+
|
663
|
+
if(state[i] == '0'){
|
664
|
+
|
665
|
+
space = i;
|
666
|
+
|
667
|
+
break;
|
668
|
+
|
669
|
+
}
|
670
|
+
|
671
|
+
}
|
672
|
+
|
673
|
+
int n;
|
674
|
+
|
675
|
+
memcpy(pre_state, state, SIZE); //親となるstateの保存
|
676
|
+
|
677
|
+
for(int i = 0; (n = adjacent[space][i]) != -1; i++ ){
|
678
|
+
|
679
|
+
/* 移動 */
|
680
|
+
|
681
|
+
state[space] = state[n]; //スペースがあった場所にnにあったものを移動
|
682
|
+
|
683
|
+
state[n] = 0; //nをスペースにする
|
684
|
+
|
685
|
+
if( List_search(clist, state) == NOEXIST ){
|
686
|
+
|
687
|
+
if( List_search(olist, state) == NOEXIST ){
|
688
|
+
|
689
|
+
//olistにもclistにも存在しないstateなら即olistに追加
|
690
|
+
|
691
|
+
ListAdd(olist, h1(state)+p_cost, state, pre_state);
|
692
|
+
|
693
|
+
}else{
|
694
|
+
|
695
|
+
//olistにのみ存在するときは,同じstateよりもコストが小さいなら追加
|
696
|
+
|
697
|
+
if(h1(state)+p_cost < same_state_cost){
|
698
|
+
|
699
|
+
for(List* node = olist; node != NULL; node = node->next){
|
700
|
+
|
701
|
+
if(node->next->state == state){ //ここもすこし違いそう
|
702
|
+
|
703
|
+
node->next = (node->next)->next; //同じstateのnodeを削除
|
704
|
+
|
705
|
+
ListAdd(olist, h1(state)+p_cost, state, pre_state); //olistに追加
|
706
|
+
|
707
|
+
}
|
708
|
+
|
709
|
+
}
|
710
|
+
|
711
|
+
}
|
712
|
+
|
713
|
+
|
714
|
+
|
715
|
+
}
|
716
|
+
|
717
|
+
}
|
718
|
+
|
719
|
+
|
720
|
+
|
721
|
+
}
|
722
|
+
|
723
|
+
|
724
|
+
|
725
|
+
|
462
726
|
|
463
727
|
}
|
464
728
|
|
465
729
|
```
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
####組みこもうとしているもの
|
470
|
-
|
471
|
-
上のコードでのnの扱いが上手くできていません.
|
472
|
-
|
473
|
-
これでは一パターンの子ノードしか作れない.
|
474
|
-
|
475
|
-
と私は考えています.
|
476
|
-
|
477
|
-
```C
|
478
|
-
|
479
|
-
char* generate_node(char* state){
|
480
|
-
|
481
|
-
int space;
|
482
|
-
|
483
|
-
for(int i = 0;i < 9;i++){
|
484
|
-
|
485
|
-
if(state[i] == '0')
|
486
|
-
|
487
|
-
space = i;
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
}
|
492
|
-
|
493
|
-
for(int i = 0; adjacent[space][i] != -1; i++){
|
494
|
-
|
495
|
-
int x = adjacent[space][i];
|
496
|
-
|
497
|
-
int p = state[x];
|
498
|
-
|
499
|
-
if (move_piece[n] == p) continue;
|
500
|
-
|
501
|
-
move_piece[n + 1] = p;
|
502
|
-
|
503
|
-
state[space] = p;
|
504
|
-
|
505
|
-
state[x] = S;
|
506
|
-
|
507
|
-
}
|
508
|
-
|
509
|
-
return state;
|
510
|
-
|
511
|
-
}
|
512
|
-
|
513
|
-
```
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
####残っているやるべきこと
|
520
|
-
|
521
|
-
・子ノードの生成
|
522
|
-
|
523
|
-
・木の生成
|