首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用LinkedStack而不是递归的N-Queens程序

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

Stack Overflow用户
提问于 2020-11-15 06:59:08
回答 1查看 99关注 0票数 4

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

代码语言:javascript
复制
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 )。谢谢你的帮助。

代码语言:javascript
复制
/**
   *
   * @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();
  } 
EN

回答 1

Stack Overflow用户

发布于 2021-02-06 06:03:56

从你的代码中,我真的不明白你的问题是什么。通过使用hill-climbing search算法的不同变体,我已经解决了n皇后问题。从这段代码中,您可能了解了如何存储board statequeen state

当你想使用基于堆栈的递归来解决这个问题时,下面是你应该遵循的过程:

代码语言:javascript
复制
- 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)

这只是您解决问题所需遵循的步骤。如果您发现很难遵循此流程,请发表意见。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64839511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档