java - N-Queens 程序使用 LinkedStack 而不是使用递归

标签 java data-structures

我做了很多研究,但所有这些要么是递归,要么不是我目前正在寻找的。我正在尝试使用 LinkedStack 而不是递归创建 N-Queens 程序,LinkedStack 将采用对象 NQueen,而不仅仅是一堆整数。这是我第一次这样做,尽管我了解算法但我不知道如何实现它。就像我如何将一个皇后与堆栈中的最后一个皇后进行比较,以及他们如何存储适合 2 个皇后不会相互攻击的每个位置。我很迷茫,如果可能的话,一些代码如何实现它会很棒。

public class NQueen {
   private static int numSolutions;
   private int col;
   private int row;
   
   public int getCol()
   {
      return col;
   }
   
   public int getRow()
   {
      return row;
   }
   
   public void setCol(int num){
      col= num;
   }
   
   public void setRow(int num) {
      row= num;
   }
   
   public NQueen(int newRow, int newColumn) {
      this.row = newRow;
      this.col = newColumn;
   
   }
   
   public void solve(NQueen Queen, int n ) {
      int current =0;
      LinkedStack<Object> stack = new LinkedStack<>();
      stack.push(Queen);
      while(true) {
         while(current < n) {
                     
         }
      
      }
      
   }
   public boolean conflict(NQueen Queen) {
      for(int i= 0; i < stack.size(); i++) {
         
      }
       
         //Check if same column or same diagonal
         
      return true;
   }     
   
}
这是我在 LinkedStack 中实现的返回 itemAt(int n)。感谢您的帮助。
/**
   *
   * @precondition 
   *   0 <= n and n < size( ).
   * @postcondition
   *   The return value is the item that is n from the top (with the top at
   *   n = 0, the next at n = 1, and so on). The stack is not changed
   *  
   **/
   
   public Object itemAt(int n) {
      int index = n;  
      if ((n<0) && (n >= size())) { 
         throw new EmptyStackException();
      }
      int i = 0; 
      while (i < n) {
         this.pop();
         i++;
      }
      this.peek();
      return peek();
  } 

最佳答案

从您的代码中,我真的不明白您的问题是什么。 Here我已经通过使用 hill-climbing search 的不同变体解决了 n-queen 问题算法。从这段代码中,您可能会了解如何存储 board statequeen state .
由于您想使用基于堆栈的递归来解决问题,因此您应该遵循以下过程:

- initiate empty stack: st = {}
- insert initial_board_state into stack: st.insert(initial_board_state)
- initiate empty map to track the visited state: visited_map = {}
- insert initial_board_state into the visited_map: visited_map.insert(initial_board_state)
- while stack is not empty:
    - remove top element from the stack: current_board_state = stack.top()
    - if current_board_state is the goal_state: return found
    - generate all the next states from the current_board_state and loop over it:
        - if next_board_state is not in the visited_map:
            - insert next_board_state in the stack: st.insert(next_board_state)
            - insert next_board_state in the visited_map: visited_map.insert(next_board_state)
这只是解决问题所需遵循的步骤。如果您发现难以遵循此过程,请发表评论。

关于java - N-Queens 程序使用 LinkedStack 而不是使用递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64839511/

相关文章:

c - 二叉树传输

python - 如何在 Python 中获取类属性的定义顺序?

c - 尝试创建字符串的 avl 树时出现段错误

c++ - std::vector 以外的排名保持数据结构?

java - 如果我不指定,我的类(class)放在哪个默认包中?

Java 泛型方法重载

java - 连续executeUpdate() SQL语句的执行时间

javascript - 如何在 JavaScript 中检查两个 map 是否具有相同的键集

java - 如何让 Telegram Bot 从提及中获取用户信息

java - 没有类名的 NoClassDefFoundError