- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我做了很多研究,但所有这些要么是递归,要么不是我目前正在寻找的。我正在尝试使用 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 state和 queen 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/
我在这些问题上遇到了困难。我有 4 个问题要让它工作。我的其中两个是正确的,但另外两个是错误的。它是 java 的 toString(): LLNode node = top; Str
所以,我开始自己在 LinkedStack 上编码,我最终缩小了错误发生的范围,但我不知道如何修复它。我会在这里发布我的代码和错误,看看是否有人能弄清楚。谢谢 LinkedNode.h #pragma
所以我有这个链接节点堆栈(简称链接堆栈)的代码,它让我非常困惑!我明天要考试,这让我很困惑!所以看看: class Node { T item;
我正在制作一个游戏,每当我要按下新游戏按钮时,我的 Canvas 不会更新,但我的 LinkedStack 值已经更新。据说它已经打乱了 LinkedStack 值。 [编辑] -在我的代码中添加了一
我做了很多研究,但所有这些要么是递归,要么不是我目前正在寻找的。我正在尝试使用 LinkedStack 而不是递归创建 N-Queens 程序,LinkedStack 将采用对象 NQueen,而不仅
我对这项作业有一个小问题:我打算完成 LinkedStack 实现并确保 peek、isEmpty 和 size 方法按照 Stack 接口(interface)中的定义实现。我相信我的大部分工作都正
我有一个 LinkedStack.h 并且我已经定义了一些方法。我的问题是如何将该文件包含在 Qt creator 中以及如何编写它以便我可以在我的 GUI 程序中定义一个 LinkedStack 类
如何在 O(1) 中为 LinkedStack 编写一个 pop() 方法?我的 LinkedStack 类中有两个私有(private)数据成员:ListNode* head 和 ListNode*
我是一名优秀的程序员,十分优秀!