- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我必须做 N 皇后问题的修改版本,其中我们给出了充满棋子的棋盘的初始配置,并且我们需要找到我们可以拥有的皇后的最大数量,这样他们就不会'不要互相攻击。输入由第一行中的一个整数组成,该整数给出了棋盘的尺寸(NxN),并且 n 行定义了棋盘的设置。字符将是“p”(意味着该位置已经有一个棋子)或“e”(表示该位置为空)。
例如,对于此输入,
5
epepe
ppppp
epepe
ppppp
epepe
输出将为 9。
这是我的代码,一切似乎都很清楚,但我不明白为什么它没有给出正确的输出
#include <stdio.h>
#include <malloc.h>
/* function headers */
void do_case(int);
int solve(char **,int,int);
int canPlace(char **,int,int,int);
/* Global vars */
int queens;
int main(void)
{
int n;
scanf("%d",&n);
getchar();
while( n != 0 )
{
do_case(n);
scanf("%d",&n);
getchar();
}
return 0;
}
void do_case(int n)
{
int i,j; //counters for input
//board configuration allocation
char **configuration = (char **)malloc(n*sizeof(char *));
for(i = 0 ; i < n ;i++ )
configuration[i] =(char *)malloc(n*sizeof(char));
queens = 0;
//get input
for( i = 0; i < n; i++ )
{
for( j = 0; j < n; j++ )
{
scanf("%c",&configuration[i][j]);
}
getchar();
}
//solve
solve(configuration,n,0);
printf("%d \n",queens);
}
//recursive solver
int solve(char **configuration,int N,int col)
{
int i,j;
//base case
if( col >= N )
return 1;
//consider this column
//try placing queen in non blocked spot in all rows
for(i = 0; i < N; i++)
{
if ( configuration[i][col] == 'e' && canPlace(configuration,N,i,col) )
{
//Place queen in configuration[i][col]
configuration[i][col] = 'q';
queens++;
//recursion on the rest
if( solve(configuration,N,col + 1) == 1 )
{
return 1;
}
//backtrack
configuration[i][col] = 'e';
queens--;
}
}
return 0;
}
//this function check if queen can be placed
int canPlace(char **configuration,int N, int row, int col)
{
int i, j;
/* Check this row on left side */
for (i = 0; i < col; i++)
{
if (configuration[row][i] == 'q')
{
return 0;
}
}
/* Check upper diagonal on left side */
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
{
if ( configuration[i][j] == 'q')
{
return 0;
}
}
/* Check lower diagonal on left side */
for (i = row, j = col; j >= 0 && i < N; i++, j--)
{
if (configuration[i][j] == 'q')
{
return 0;
}
}
return 1;
}
最佳答案
基本上,您的代码输出 0,因为它要求我们在每一列中放置一个皇后,但您的示例中并非如此。
也就是说,该算法存在多个问题(我并不认为该列表是完整的,尽管它可能是完整的):
代码不会考虑所有可能性:它只会找到第一个可能的排列,然后在单个“if( col >= N ) return 1;”后退出搜索。相反,它应该像“if( col >= N ) 在单独的变量中更新皇后的最佳可能值,然后返回 0 继续搜索”。
在“if(solve(configuration,N,col + 1) == 1)”行中,代码假定单个列中不能有两个皇后。该调用应使用 col
而不是 col + 1
,并以某种方式说明我们在当前列处停止的位置。
要允许没有皇后的列,应在求解函数中的某个位置放置对“solve(configuration,N,col + 1)”的无条件调用。
当我们允许第 2 项时,应修改函数 canPlace 以同时检查该列。
只要找到 pawn,canPlace 的循环就应该中断。
关于c - N Queen 的输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27337309/
有没有人从 perl journal 那里得到完整的程序? https://www.foo.be/docs/tpj/issues/vol1_4/tpj0104-0001.html 更具体地说,下面的链
我被困在扩展 exercise 28.2 of How to Design Programs .我使用真值或假值的向量来表示板,而不是使用列表。这是我所拥有的,但不起作用: #lang Scheme
为了练习我所学到的回溯算法,我正在尝试解决 N-Queen 问题。 我编写了一些函数来检查移动是否合法,但我不知道如何使用回溯来实现这些函数。 bool manger_ligne (int a[][4
所以我必须做 N 皇后问题的修改版本,其中我们给出了充满棋子的棋盘的初始配置,并且我们需要找到我们可以拥有的皇后的最大数量,这样他们就不会'不要互相攻击。输入由第一行中的一个整数组成,该整数给出了棋盘
下面是我的代码,我不知道哪里错了它编译得很好,但没有打印出正确的结果;;;如果 N 是 4 ,结果应该是2 4 1 3但是,它打印了1 3 0 0 我猜 for-loop 有问题,因为当我使用另一个值
我正在解决n-queen问题,但由于某种原因我遇到了一个问题,while循环不断循环而不迭代,tempx和tempy不会按i/j上升。结果一直输出0,0 public static boolean i
问题在这里:B. 8 Queens, Again!! 我认为我没有遇到最坏的情况或遗漏了什么。我的提交在测试 2 中失败。 我刚刚用下一个输入检查了每个输入的行、列和对角线。我认为这就足够了。还有其他
我有一个 - 字符列表列表,它充当一个网格。 我想将每个列和行的一个 - 更改为 Q。 这是我到目前为止所得到的: import pprint import random # I impo
我正在研究 N-Queens 以自行实现它,并且遇到了以下规则的实现: The n-queens puzzle is the problem of placing n queens on an n×n
我正在尝试编写一个程序,使用二维 boolean 数组解决 8 Queens 问题。我将使用回溯来找到解决方案。我知道这不是解决此问题的最佳方法,为了简单起见,我可能应该使用一维数组,但我想以这种方式
我是递归和回溯的新手。我正在尝试完成 N-Queen 问题以打印所有解决方案而不是仅 1 个解决方案并理解这些概念。 我认为我已经部分正确地实现了算法,因为我得到了一些解决方案但没有全部打印出来。我的
这是我第一次在这里发布问题,所以请放轻松。 我最近遇到了 n-queen/8 queen 问题,觉得很有趣就试了一下 我为这个问题编写了一个基本代码,但它没有给出任何输出。当我尝试调试它时,它表明流程
我想我理解 NQueens 算法的重点 - 您检查每一行是否存在可用空间,如果它们存在,则在那里放置一个皇后,然后递归地将算法调用到下一行。这是我的算法(N 大小为 3) from typing im
n-皇后拼图是将n 个皇后放在n x n 棋盘上的问题,这样就不会有两个皇后互相攻击。 给定一个整数 n,返回 n-queens 谜题的不同解的数量。 https://leetcode.com/pro
我在使用模拟退火算法来解决 n 皇后问题时遇到了一些麻烦。基本上,我让它寻找更好的更多,效果很好,但然后我运行一个公式来检查它是否应该采取“坏” Action 。据我了解,公式为e^(板状态计算变化)
我正在编写臭名昭著的 N-Queens 问题。但是我有一个问题。该程序正在执行,但没有按预期提供输出,因为我遇到了一个问题,因为矩阵 board 值未更改并且将第一个值分配给 board即,0 分配给
我一直在尝试编写一个java类来使用某种堆叠和递归来解决n皇后问题,答案存储在网格(二维数组)中,但是我遇到了一个死墙,即n=8时递归的堆栈溢出(最大递归深度达到2298)所以我一直想知道是否有某种方
在这个程序中,我将确定放置皇后是否会对棋盘造成威胁。这将使用位置 1 - 8(可以展开)作为行和列。如果一列中没有棋子,则该行的 y 将为 0(否则为相应的 y)。空板将如下所示:((1 0)(2 0
我编写了一个函数,可以生成 N 皇后问题的所有可能解决方案。它需要用户输入一个整数,即表格的尺寸。即,如果用户输入 4,那么它将列出 4x4 表的所有可能解决方案。当输入 n=4 时,输出如下所示:
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我是一名优秀的程序员,十分优秀!