Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java N-Queens程序使用LinkedStack而不是使用递归_Java_Data Structures - Fatal编程技术网

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

Java N-Queens程序使用LinkedStack而不是使用递归,java,data-structures,Java,Data Structures,我已经做了很多研究,但所有的研究要么是递归,要么不是我目前正在寻找的。我正在尝试使用LinkedStack而不是递归创建一个N-Queens程序,LinkedStack将接受对象NQueen,而不仅仅是一组整数。这是我第一次做这件事,虽然我理解这个算法,但我就是不知道如何实现它。比如,我如何将一个皇后与堆栈中的最后一个皇后进行比较,以及它们如何存储适合两个皇后不相互攻击的每个位置。我太迷茫了,如果可能的话,一些代码如何实现它会很好 public class NQueen { private

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

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;
   }     
   
}
public class-nquien{
私有静态整数解;
私人国际学院;
私人int row;
公共int getCol()
{
返回列;
}
public int getRow()
{
返回行;
}
公共无效设置列(整数){
col=num;
}
公共void setRow(int num){
行=num;
}
公共NQueen(int newRow,int newColumn){
this.row=newRow;
this.col=newColumn;
}
公共无效解决方案(NQueen Queen,int n){
int电流=0;
LinkedStack堆栈=新LinkedStack();
堆栈。推(皇后);
while(true){
而(电流
这是我在LinkedStack中实现的返回项(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();
  } 
/**
*
*@前提条件

*从你的代码中,我真的不明白你的问题是什么。我通过使用不同的
爬山搜索
算法解决了n-queen问题。从这段代码中,您可能会了解如何存储和

如果要使用基于堆栈的递归解决此问题,请遵循以下过程:

- 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)
这只是解决问题需要遵循的步骤。如果您发现很难遵循此过程,请发表评论