Each cell should be a single number or list (list, set, vector, bitset) of possible numbers (use variant type). For all but bitset the container already have iterators to traverse them. And for bitset you can do a simple loop over the 9 bits checking each.
For the overall guessing algorithm I would forget about backtracking. Instead do a depth first search and work stealing queues. What do I mean by that?
Each thread gets a queue of boards to solve. At the start all queues are empty and you insert the starting board into one queue. You also should have one condition variable to signal that new boards are in some queue.
Each thread removes the head of the queue, if possible.
Find the first field that isn't a solved number. Then iterate over the list of possible values to generate all boards with that field filled in. Filling in the number should remove the number from all lists of fields in the same row, column, square and fail if it causes some list to become empty (no possible solution). If any list is reduced to 1 element remember it and also fill it in. If at the end of all that you still have an unsolved board add it to the threads queue and trigger the condition variable to signal that new boards are in some queue.
If the queue if a thread is empty then first find the queue with the most entries. Remove half (or some other fraction) of it and put it in the threads queue. That's the work stealing part.
If all queues are empty wait for the condition variable to signal that new boards are in some queue. Notes: this is actually the starting place for all threads.
发布评论
评论(1)
每个单元格应该是可能数字的单个数字或列表(列表,set,vector,bitset)(使用变体类型)。除了小块外,容器已经有迭代器可以穿越它们。对于Bitset,您可以在9位检查每个位置进行简单的循环。
对于总体猜测算法,我会忘记回溯。相反,请先进行深度搜索并进行窃取队列。我的意思是什么?
每个线程都会有一个板的队列要解决。一开始,所有队列都是空的,然后将起始板插入一个队列。您还应该有一个条件变量,以表明新板在某些队列中。
如果可能的话,每个线程将卸下队列的头部。
找到不是解决号码的第一个字段。然后在可能的值列表上迭代以生成所有板的列表。没有可能的解决方案)。如果将任何列表简化为1个元素,请记住它并填写。
如果队列如果线程为空,则首先找到最多条目的队列。删除其中一半(或其他部分),然后将其放入螺纹队列中。那是窃取部分的工作。
如果所有队列都是空的,请等待条件变量,以表示新董事会在某些队列中。注意:这实际上是所有线程的起点。
Each cell should be a single number or list (list, set, vector, bitset) of possible numbers (use variant type). For all but bitset the container already have iterators to traverse them. And for bitset you can do a simple loop over the 9 bits checking each.
For the overall guessing algorithm I would forget about backtracking. Instead do a depth first search and work stealing queues. What do I mean by that?
Each thread gets a queue of boards to solve. At the start all queues are empty and you insert the starting board into one queue. You also should have one condition variable to signal that new boards are in some queue.
Each thread removes the head of the queue, if possible.
Find the first field that isn't a solved number. Then iterate over the list of possible values to generate all boards with that field filled in. Filling in the number should remove the number from all lists of fields in the same row, column, square and fail if it causes some list to become empty (no possible solution). If any list is reduced to 1 element remember it and also fill it in. If at the end of all that you still have an unsolved board add it to the threads queue and trigger the condition variable to signal that new boards are in some queue.
If the queue if a thread is empty then first find the queue with the most entries. Remove half (or some other fraction) of it and put it in the threads queue. That's the work stealing part.
If all queues are empty wait for the condition variable to signal that new boards are in some queue. Notes: this is actually the starting place for all threads.