/** * Iterative deepening. */ State ids(const State state){ if (state.is_complete()){ return state; } size_t max_depth = 0; while (1){ State state_ = dls(state, std::vector<State>(), MoveTracker(state), 0, 0, max_depth); max_depth++; } return state; }
/** * Depth limited search. */ State dls(const State state, std::vector<State> visited, MoveTracker mt, size_t explored_nodes, size_t depth, size_t max_depth){ if (depth > max_depth){ return state; } if (state.is_complete()){ return state; } visited.push_back(state); std::vector<Move> moves = state.possible_moves(); for (auto& move : moves){ State state_ = state.apply_move_cloning(move); MoveTracker mt_ = mt.addMoveCloning(move); if (state_.is_complete()){ for (auto& move : mt_.moves()){ std::cout << move << std::endl; } std::cout << "explored_nodes: " << explored_nodes << std::endl; return state_; } if (std::find(visited.begin(), visited.end(), state_) != visited.end()){ continue; } std::cout << state_ << std::endl; return dls(state_, visited, mt_, explored_nodes + 1, depth + 1, max_depth); } return state; }
V dlo(O o){ switch(o->t){ case TS:case TCB:DL(o->s.s);BK; case TA:while(len(o->a))dlo(pop(o->a));dls(o->a);BK; case TD:BK; }DL(o); } //delete object