簡體   English   中英

如何使用next_permutation

[英]how to use next_permutation

我正在嘗試安排tic tac toe board。 所以我有以下代碼:

// 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()) );

但它只輸出一次原始字符串。 我假設每個角色都必須是唯一的。 我能做到這一點的方式是什么?

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;
}

這是一個實例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM