C++ アルゴリズムとデータ構造のライブラリ
#include "library/search/permutation.hpp"$O(N!)$ Nは10〜12程度まで
// 関数でbreakしてたらtrue
bool is_loop_break = permutation(A, [&](){
print(A);
return true; // trueで探索をbreak
});
#pragma once
template <typename T, typename F> bool permutation(vector<T> &A, F f) {
sort(begin(A), end(A));
do {
if (f()) return true;
} while (next_permutation(begin(A), end(A)));
return false;
}#line 2 "library/search/permutation.hpp"
template <typename T, typename F> bool permutation(vector<T> &A, F f) {
sort(begin(A), end(A));
do {
if (f()) return true;
} while (next_permutation(begin(A), end(A)));
return false;
}