我已经写了两个版本的nqueens问题,我认为他们应该有类似的效率,但事实并非如此。我认为这是由于Haskell的懒惰评估行为造成的。有没有人可以解释一下它是如何工作的?
nqueens1 n 1 = [[i] | i <- [1..n]]
nqueens1 n k = [ i:q | i <- [1..n], q <- (nqueens1 n (k - 1)), isSafe i q k]
isSafe i q n = isSafeHelper i (zip q [(n-1),(n-2)..1])
where isSafeHelper i [] =
我试图用回溯法解决Haskell的N皇后问题。以下是我的做法:
type Solution = [Int]
extend :: Solution -> Int -> Maybe Solution
extend b new = go 0 b
where
n = length b
go _ [] = Just (b ++ [new])
go i (x:xs)
| x == new = Nothing
| abs (i - n) == abs (x - new) = Nothing
我正在努力理解 monad是如何工作的。显然,它是的表亲,可以用于回溯搜索。
我有一个基于列表的n皇后问题的解决方案:
-- All the ways of extracting an element from a list.
oneOf :: [Int] -> [(Int,[Int])]
oneOf [] = []
oneOf (x:xs) = (x,xs) : map (\(y,ys) -> (y,x:ys)) (oneOf xs)
-- Adding a new queen at col x, is it threathened diagonally by any of
所以我正在解决N个皇后的问题,你必须找到在NxN棋盘上有N个皇后而不能互相攻击的可能的解决方案的数量。我使用递归回溯来做这件事。我已经在代码中实现了我的解决方案,但由于某种原因,我没有得到正确的解决方案数量,我不知道为什么。
下面是我的代码:
#include <iostream>
#include <vector>
using namespace std;
int numSolutions=0;
//Uses recursion to count how many solutions there are for
//placing n queens on an
Algorithm NQueens ( k, n) //Prints all Solution to the n-queens problem
{
for i := 1 to n do
{
if Place (k, i) then
{
x[k] := i;
if ( k = n) then write ( x [1 : n]
else NQueens ( k+1, n);
}
}
}
Algorithm Place (k, i)
{
for
我试图在OSX10.11.3上构建一个示例
当我跑步时:
g++ -I../include/ -L../lib -lortools -Wno-c++11-extensions nqueens2.cc
In file included from nqueens2.cc:25:
In file included from ../include/constraint_solver/constraint_solver.h:65:
In file included from ../include/base/hash.h:19:
/Applications/Xcode.app/Contents/Dev
我试图找到所有可能的解决方案的N-皇后谜题。我必须在每一行上打印一个皇后,没有两个皇后应该彼此相邻,就像没有一个或多个皇后对角,没有一个或多个皇后在同一排,没有一个或多个皇后在同一列。
我已经编写了算法,并认为它的大部分是正确的,但解决方案没有打印出来。我不明白为什么。我花了很多时间在这件事上。
任何帮助都将不胜感激。以下是我的代码:
public class NQueens {
int[] x;
public NQueens(int n) {
x = new int[n];
} // CONSTRUCTOR
public void
我是JAVA新手。下面是我的NQueens问题的代码。结果是[[Ljava.lang.String;@123a439b,Ljava.lang.String;@7de26db8。
有人能帮帮忙吗?非常感谢!我从别人的博客上抄袭了代码。代码应该是正确的。我只是不知道怎么把结果打印出来。我认为这应该不是很难。我的背景不是计算机科学,这可能是我遇到麻烦的原因。谢谢!
import java.util.ArrayList;
public class Solution {
public ArrayList<String[]> solveNQueens(int n) {
ArrayL
我尝试使用javascript实现N- Queens问题,并使用节点REPL js解释器执行它。下面是我使用过的两个主要功能:
function isSafe( board, row, column){
//To check for a queen in the same row to the left
for (i=0;i<column;i++)
if(board[row][i])
return false;
//To check for a queen diagonally below to the left
for ( i=0 , j=0 ; i<row
我正在创建一个nQueen问题解决程序,但是每次我运行该程序时,它都会启动以下错误:
"tuple“没有属性"row”。
我想知道函数位置是否结构良好,因为是抛出错误的那个。
import sys #use of argv
#Create the object queen with the row and column
class Queen:
def __init__(self, row, col):
self.row = row
self.col = col
def __repr__(self):
retu
执行代码时有此错误。我查看了其他有关这方面的帖子,但所有这些帖子都提到了不应该使用()或[]的地方。但是,在我的例子中,我并不认为问题是我唯一要做的事情,就是用另一个列表的另一项覆盖一个列表的索引的值。这是我试图调用的代码:
def reproduce(boardx, boardy, nqueens):
boardChild = [-1] * nqueens
n = random.randint(0, nqueens - 1) #percentage of how much of one parent is reproduced and how much of the othe
我是C#中的新手(我来自C++),我面临一个简单的模式,在C++中,我会使用模板进行解析,但是使用C#泛型也不能使用相同的方法。
下一段代码( C#与C++模板的混合)显示了我的问题。
class A { /* ... */ }
class B { /* ... */ }
// C, D, ...
class W
{
public void Update(A a) { /* ... */ }
public void Update(B b) { /* ... */ }
// C, D, ...
}
class X
{
template <typename
我正在创建一个带有菜单的WPF应用程序,当点击menuItem时,我想在我的窗口(插件)中显示NQueens (另一个项目)。我已经在我的CurrentDirectory中放置了一个NQueens.dll来使用。为此,我创建了一个程序集对象来装入类,创建了一个Nqueens.Nqueen实例并调用了这些方法。
NQueens项目由一个类NQueen.cs和一个MainWindow组成。
namespace NQueens
{
public class NQueen
{
public static bool berekenQueens(int Row, int N, bool[,] bor
我正在阅读C++ 4e中与图有关的数据结构和算法(由Adam编写)。在图宽度优先搜索的实现中,psuedo代码如下所示:
BFS():
for all vertices u
num(u) = 0
edges = null
i = 1
while there is a vertex v such that num(v) is 0
num(v)++
enqueue(v)
while queue is not empty
v = dequeue()
if n
几个小时以来,我一直在用头撞墙。我不知道该怎么办。我已经对我的函数进行了多次重构,但仍然没有让它正常工作。
这是针对我的c++类中的一个编程任务。它必须有一个特定的形式,如数据类型、参数等,就像讲师给出的那样,所以我不能改变任何类似的东西。我们必须使用字符数组,因此strcmp()。如果我们找到了person,就必须返回它,如果没有找到,则返回NULL。
到目前为止,我使用的是以下内容:
person *classname::getPersonByName(char *name, person *rt)
{
if (rt != NULL)
{
if (strcm
我想做一个解决3*3 sudoku难题的程序。我已经做了一个程序,但它只工作50%的问题,其余的给出60%正确的解决方案。我不知道如何用有限的步骤来解决每一个可能的问题。我使用的技术是搜索数组中的每个单独元素,并检查在同一行和同一列中不存在的任何元素,然后将其放入该单元并移到下一个单元。但这并不是解决所有问题的办法。接下来想到的是,我们必须为一个单元写出每一个可能的数字,然后再继续。但是,我们将如何决定我们最终应该把哪个数字放进这个单位。我只想知道如何写一个能解决每个问题的解决方案。我希望你明白我的意思。我写的代码是
#include<iostream>
using namesp
我试图解决这个nQueens问题,我的代码如下所示:
class Solution {
public:
vector<vector<string>> ans;
bool canPlace(vector<string> &board, int row, int col, int n){
//upper left diagonal
int rowIndex = row;
int colIndex = col;
while(rowIndex >= 0 and
我使用递归调用编写了fizzbuzz代码,它引用了互联网上的一些代码。下面的代码可以正常工作,但我不能完全理解执行这些代码时发生了什么。
更确切地说,我不明白为什么一开始会显示1。在main()方法中,recurrence()接收100,因此它感觉Buzz (100的结果)首先被显示出来。
我在IntelliJ上进行了调试,将断点放在recurrence(i - 1)上,该方法首先调用100次,其余的代码随后执行。我想知道recuurence的结果存储在哪里。
public class FizzBuzz {
public static void main(String[] args)