使用线程解决 N 个皇后难题

Solving N queens puzzle using pthreads

本文关键字:难题 线程 解决      更新时间:2023-10-16

我知道如何使用回溯和递归来解决n个皇后难题。

我在想如何使用多线程来优化它。

我正在尝试使用 p - 线程。

基本上我无法理解顶部应用线程的位置以及在哪里加入线程。

由于这是递归,我也无法理解线程在这里如何工作。

--

谢谢

阿洛克·

一种方法是使用队列将每个扩展放入队列中,而不是执行递归。有一个线程池来弹出扩展并对其进行处理:

create a state with an empty board and put it into the queue
create N threads with the following function

螺纹功能:

while not done:
  1) pop a state S from the queue (use locks), if queue is empty,
     wait on a semaphore until there is an S
  2) expand state S
  2a) if S has feasible children then put them into the queue 
      except for one state SS, call it S and goto 2 
     (also signal the semaphore)
  2b) if S has no feasible children goto 1
end while

您可以针对不同的算法修改此设置