Lov*_*ech 5 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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在 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();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
75 次 |
最近记录: |