益智游戏的算法
我正在开发一款游戏,并且正在努力获得一些通用功能。假设我们有一个像“puzzle game using group of Words”这样的短语,所以我从中生成可能的子集:
“puzzle”
,“game”
,“using”
,“group”
,“of”
,“words”
并添加更多乐趣我还添加两个连续单词的组(目前> 2个单词的组不允许):“益智游戏”
、“游戏使用”
、“使用组”
、“组”,
"of Words"
所以现在的主要思想是从形成原始句子的这些子集中形成所有可能的组合。请注意,在这种情况下,子集应该是一个分区。
示例:
"puzzle game", "using", "group", "of words"
"puzzle", "game", "using group", "of", "words"
...
不允许:
"puzzle game", "game using", .. (it's not a partition as "game" is repeated)
是否有任何已知的算法可以生成所有可能的组合?我认为对于较长的短语来说这可能会非常耗时,因此是否有其他替代方案可以尝试根据某些权重找到可能的最佳选项?
我不会假装获得代码(尽管那会很棒),但至少任何关于在哪里查看的提示或想法将非常感激!
I'm working on a game and I'm struggling to get some generic functionality. Suppose we have a phrase like "puzzle game using group of words"
so I generate possible subsets from this:
"puzzle"
, "game"
, "using"
, "group"
, "of"
, "words"
and to add more fun I also add group of two consecutive words (for now groups of > 2 words are not allowed): "puzzle game"
, "game using"
, "using group"
, "group of"
, "of words"
So now the main idea would be forming ALL possible combinations from these subsets that form the original sentence. Please note that in this case the subsets should be a partition.
Example:
"puzzle game", "using", "group", "of words"
"puzzle", "game", "using group", "of", "words"
...
Not allowed:
"puzzle game", "game using", .. (it's not a partition as "game" is repeated)
Is there any known algorithm that generates all possible combinations? I presume this can get very time consuming for longer phrases so are there alternatives that try to find possible best options based on some weight for example?
I don't pretend to get code (though that would be awesome) but at least any tip or ideas of where to look at would be really appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先将字符串解析为单词,让单词列表为 S。创建一个可能返回值的空结果列表(让它为 L)。
使用递归解决方案:设置当前解决方案(初始化为空),并在每一步中添加可能的下一个单词/双精度数。当你用完你的单词时,“当前”将是一个分区,并将其添加到列表中。
伪代码:
编辑:注意:此解决方案假设每个分区只有 1 或 2 个单词是合法的。如果不是这种情况,则必须迭代 1,...,S.size() 第一个单词,而不是通过硬编码的递归调用将 S 减少 1/2 个单词。
非递归解决方案(使用堆栈和列表 ADT):
first parse your string into words, let the list of words be S. create an empty result list (let it be L) of possible return values.
use a recursive solution: set a current solution (initialized to empty), and at each step - add the possible next word/double to it. when you used up your words, the 'current' will be a partition, and add it to the list.
pseudocode:
EDIT: note: this solution assumes only 1 or 2 words are legal per partition. if it is not the case, instead of the hard-coded recursive call which decreases S by 1/2 words, you will have to iterate over the 1,...,S.size() first words.
None recursive solution (using Stack and List ADTs):
如果您认为每个单词之间都存在小的看不见的“障碍”,那么这很简单。
例如,“使用单词组的益智游戏”变为“益智 | 游戏 | 使用 | 单词组 | 单词”。如果你有 N 个单词,你就有 N-1 个障碍。
现在,对于每个“障碍”,您可以选择障碍是向上还是向下。如果它向上,它就充当分离器。如果不存在,则认为它不存在。
示例:
“拼图 | 游戏 | 使用 | 一组 | 单词” -> “拼图”、“游戏”、“使用”、“一组”、“单词”
“使用|组|单词的益智游戏”-> “益智游戏使用”,“组”,“的”,“单词”
对于每个“障碍”,你可以决定它是向上还是向下,所以只有 2 个选择。由于您有 N-1 个“障碍”,因此您总共有 2^(N-1) 个这样的分区
编辑:Argl =/
这些组是否仅限于一两个单词?
Very simple if you consider that there are small invisible "barriers" between each word.
For example, "puzzle game using group of words" becomes "puzzle | game | using | group | of | words". If you have N words, you have N-1 barriers.
Now, for every "barrier", you can choose whether the barrier is up or down. If it's up, it acts as a splitter. If not, considers that it doesn't exist.
Examples :
"puzzle | game | using | group of | words" -> "puzzle", "game", "using" , "group of", "words"
"puzzle game using | group | of | words" -> "puzzle game using", "group", "of", "words"
For each "barrier", you can decide whether it's up or down, so there are only 2 choices. As you have N-1 "barriers" , you have a total of 2^(N-1) such partitions
Edit : Argl =/
Are the groups only limited to one or two words?
查看星星和条形。
如果您有
N
个字符串(又名星星),现在在它们之间放置
N-1
个条。只有一种方法可以做到这一点这是一种可能性。现在在它们之间放置
N-2
条。等等。如果您用字符串替换星星,这些将定义您的分区。要生成将
x
条放在N
颗星之间的所有可能方法,您只需要一种生成组合的方法。Take a look at Stars and bars.
If you have
N
strings (aka stars)Now place
N-1
bars between them. There's only one way to do thisThis is one possibility. Now place
N-2
bars between them.etc. These define your partitions if you replace the stars with your strings. To generate all possible ways to put the
x
bars betweenN
stars, you'll just need a way to generate combinations.