我在试着安排一套井字游戏板子。所以我有以下代码:
// 5 turns for x if x goes first
std::string moves = "xxxxxoooo";
do {
std::cout << moves << std::endl;
} while ( std::next_permutation(moves.begin(), moves.end()) );但它只输出原始字符串一次。我假设每个字符都必须是唯一的。有什么方法可以做到这一点呢?
发布于 2013-05-22 06:14:10
std::next_permutation按字典顺序返回下一个排列,如果生成了第一个排列(按该顺序),则返回false。
因为您以("xxxxxoooo")开头的字符串实际上是该字符串的字符按字典顺序排列的最后一个排列,所以您的循环立即停止。
因此,在开始循环调用next_permutation()之前,您可以尝试对moves进行排序:
std::string moves = "xxxxxoooo";
sort(begin(moves), end(moves));
while (std::next_permutation(begin(moves), end(moves)))
{
std::cout << moves << std::endl;
}这是一个。
https://stackoverflow.com/questions/16680322
复制相似问题