如何获得填字游戏的多个解决方案?
我已经看过有关此的论坛和不同的问题。
但我想问一些不同的事情。 我有两个不同单词的单词列表和一个由 0 和 1 指定的网格。 我必须从单词列表 1 中选择单词作为行,从单词列表 2 中选择单词作为列。
主要问题是我必须在给定的时间限制内找到多个解决方案。有人可以建议我一些好的算法吗?我不知道我应该采取什么样的算法方法。
另一件事, 我有两种语言选择。 c++或者java哪个比较好实现。
谢谢
I have already seen the forums and different questions on this.
But i want to ask something different.
I have two wordlist of different words and one grid specified by 0 and 1.
i will have to select word from wordlist 1 for rows and 2 for columns.
The main problem is i have to find multiple solution within given time constraint. Can someone suggest me some good algorithm for that. I am not getting what kind of algorithmic approach should i take.
Another thing,
I have two language options. Either c++ or java which would be better to implement.
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了一个可以满足您需求的解决方案。遗憾的是我不能把它归功于:)
这是一个例子。您向其提供一个模式文件,例如
pattern1
:您在其上调用程序,例如:
输出是
或者,再运行一次:
当然,对于较大的模式或较小的单词列表,您的里程可能会有所不同(疯狂地)。我能够在 Q9550 处理器上在 26.5 秒内完成 1000 代,使用
输出确认这些实际上是 1000 个独特解决方案。如果你问我的话,还不错。 (时间包括计算每个解决方案的 md5sums 的时间)
I found a solution that does what you want. Sadly I can't take credit for it :)
Here is an example. You feed it a pattern file, like
pattern1
:You invoke the program on it, like e.g. so:
The output is
Or, run another time:
Of course, with larger patterns or smaller wordlists your mileage may vary (wildly). I was able to do 1000 generations in 26.5 seconds on a Q9550 processor, using
The output confirmed that these were in fact 1000 unique solutions. Not bad, if you ask me. (The timing included the time to calculate the md5sums per solution)
您也许可以使用称为Dancing Links 或DLX 算法的算法。这是解决精确覆盖问题的极其有效的算法。
有几个程序使用它来解决数独难题。
老实说,我对确切的封面问题了解不够,不能说这肯定会满足您的需求,但值得一看。
You may be able to use something called the Dancing Links or DLX algorithm. This is an extremely efficient algorithm for solving exact cover problems.
There are several programs out there that use this to solve Sudoku puzzles.
I honestly don't know enough about exact cover problems to say that this will definitely work for your needs but it's worth taking a look.
在做填字游戏时,人们通常会发现自己正在寻找一个特定长度的单词,并且特定字母位于其中的特定位置。因此,您可能需要这样的函数:
它可能会使用一组预构建的 HashMap 来快速生成一组候选者。 (您可能还希望能够跟踪该单词当前是否已在填字游戏中使用......假设不允许重复)
人们做的另一件事是使用提示进行猜测。我认为你可能不是在寻找强大的人工智能,因此只剩下暴力算法......在这种情况下,尝试先从最大的单词开始填写填字游戏,因为那里的可能性通常较少。
骨架算法:
When doing a crossword, one usually finds oneself looking for a word of a certain length, with a certain letter at a certain position in it. So, you're probably going to want a function like this:
Which could probably use a set of pre-built HashMaps to produce a set of candidates quickly. (You'll probably also want to be able to track whether the word is currently used already in the crossword...assuming duplicates aren't allowed)
The other thing people do is guess using hints. I figure you're probably not looking for strong AI, so that leaves brute force algorithms...in which case, try to fill in the crossword starting with the biggest words first, since there are generally fewer possibilities there.
Skeleton algorithm: