431 {
432 MapOfPuzzleInfoWithPuzzleInfo
433 parent_of;
434 MapOfPuzzleInfoWithInteger g_score;
435 SetOfPuzzleInfo open_list;
436 SetOfPuzzleInfo closed_list;
437
438
439 open_list.emplace(Initial);
440 parent_of[Initial] = nullptr;
441 g_score[Initial] = 0;
442
443 while (!open_list.empty()) {
444
445 typename SetOfPuzzleInfo::iterator it_low_f_score;
446 uint32_t min_f_score = 1e9;
447 for (auto iter = open_list.begin(); iter != open_list.end();
448 ++iter) {
449
450
451 uint32_t f_score = (*iter)->heuristic_value + (*iter)->depth;
452 if (f_score < min_f_score) {
453 min_f_score = f_score;
454 it_low_f_score = iter;
455 }
456 }
457
458
459 std::shared_ptr<Info> current_state = *it_low_f_score;
460
461
462 if (*(current_state->state) == *(Final->state)) {
463 return Solution(current_state, parent_of);
464 }
465
466 open_list.erase(it_low_f_score);
467
468
469 if (current_state->depth >= permissible_depth) {
470 continue;
471 }
472
473
474 std::vector<Puzzle> total_possible_moves =
475 current_state->state->generate_possible_moves();
476
477 for (Puzzle &neighbor : total_possible_moves) {
478
479
480 std::shared_ptr<Info> Neighbor = std::make_shared<Info>(
481 neighbor, dist(neighbor, *(Final->state)),
482 current_state->depth + 1U);
483 uint32_t temp_g_score = Neighbor->depth;
484
485
486
487
488 auto closed_list_iter = closed_list.find(Neighbor);
489 if (closed_list_iter != closed_list.end()) {
490
491
492
493 if (Neighbor->depth < (*closed_list_iter)->depth) {
494 closed_list.erase(closed_list_iter);
495 } else {
496 continue;
497 }
498 }
499 auto neighbor_g_score_iter = g_score.find(Neighbor);
500
501
502 if (neighbor_g_score_iter != g_score.end()) {
503 if (neighbor_g_score_iter->second > temp_g_score) {
504 neighbor_g_score_iter->second = temp_g_score;
505 parent_of[Neighbor] = current_state;
506 }
507 } else {
508 g_score[Neighbor] = temp_g_score;
509 parent_of[Neighbor] = current_state;
510 }
511
512
513
514 auto iter = open_list.find(Neighbor);
515 if (iter == open_list.end()) {
516 open_list.emplace(Neighbor);
517 } else if ((*iter)->depth > Neighbor->depth) {
518 (*iter)->depth = Neighbor->depth;
519 }
520 }
521 closed_list.emplace(current_state);
522 }
523
524 return std::vector<Puzzle>(0);
525 }
std::vector< Puzzle > Solution(std::shared_ptr< Info > FinalState, const MapOfPuzzleInfoWithPuzzleInfo &parent_of)
A helper solution: launches when a solution for AyStarSearch is found.