【问题标题】:N-Queens program using LinkedStack instead of using recursionN-Queens 程序使用 LinkedStack 而不是使用递归
【发布时间】:2021-02-26 13:25:14
【问题描述】:

我做了很多研究,但所有这些要么是递归,要么不是我目前正在寻找的。我正在尝试使用 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();
  } 

【问题讨论】:

    标签: java data-structures


    【解决方案1】:

    从您的代码中,我真的不明白您的问题是什么。 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)
    

    这只是解决问题所需遵循的步骤。如果您发现难以遵循此过程,请发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      相关资源
      最近更新 更多