- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我必须编写这个算法(这个版本不需要在骑士开始的同一位置结束),并且我让它工作,但它太慢了。它适用于起始位置 x=0 和 y=0(大小=8),但如果我尝试将 x 和 y 操纵为例如 2 和 4,它不会在几分钟后结束。有人可以帮忙吗?
#include <stdio.h>
#define size 8
int ruch_x[]={ -1, 1, -2, 2, -2, 2, -1, 1}; //arrays of possible knight moves, for example 2nd move is [1,2]//
int ruch_y[]={ 2, 2, 1, 1, -1, -1, -2, -2};
int done(int szach[][size])
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(szach[i][j]==0)
return 0;
}
}
return 1;
}
//licz - variable for counting knights moves//
void konik(int szach[][size], int x, int y, int licz)
{
szach[x][y]=licz;
if(licz<size*size){
for(int i=0;i<=7;i++){ //loop that checks every possible knight move//
if(szach[x+ruch_x[i]][y+ruch_y[i]]==0&&x+ruch_x[i]>=0&&x+ruch_x[i]<size&&y+ruch_y[i]>=0&&y+ruch_y[i]<size)
// checking if candidat was already visited and if it's not outside of the board//
{
konik(szach, x+ruch_x[i], y+ruch_y[i], licz+1);}}}
if(done(szach)==1) return; //checking if whole board is filled with numbers, if yes then skip zeroing current move//
szach[x][y]=0;
}
int main()
{
int i, j, x,y;
printf("set x\n");
scanf("%d", &x);
printf("set y\n");
scanf("%d", &y);
int szach[size][size];
for(i=0;i<size;i++) { //zeroing array//
for(j=0;j<size; j++) {
szach[i][j]=0; }}
konik(szach, x, y, 1);
for(int i=0;i<size;i++) {
for(int j=0;j<size; j++) {
printf("%d\t",szach[j][i]); }
printf("\n\n");}
printf("\n");
return 0;
}
最佳答案
简单地暴力破解可能不是一个好主意,因为对于 8x8 板来说,可能的序列数量可能超过 10^50。
Knight's tour维基百科上的文章有关于这个问题的不错的信息,我建议从那里开始。
查找哈密顿路径的最著名的启发式之一如下:从任何节点 u
中,按相邻节点在图中的度数以非降序排列。假设从 u
骑士可以移动到 p
和 q
(即 u
有两个邻居),然后如果递归的可用邻居少于 q
,则首先考虑 p
。这通常会导致显着的优化,特别是如果图形具有大量汉密尔顿路径(如本例所示)。
关于你的代码。你不需要每次都调用done()。如果在任何时候 licz >= size*size
,那么您就知道您找到了答案。例如,您可以存储全局 bool 值完成
。这应该会有一点帮助,但并不是在所有情况下都有帮助。
附注如果您的项目需要任何单一解决方案,我建议存储单个汉密尔顿循环,并且对于任何x,y
只需简单地移动获得有效解决方案的顺序。
关于c - 骑士之旅算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20409930/
我是docker的新手,我正在尝试将Angular示例项目“Tour of heros”进行docker化,您可以在这里找到: https://angular.io/generated/zips/to
当我点击“结束游览”按钮时,我无法再次开始 Bootstrap 游览。我搜索了所有 documentation ,但我还没有找到解决方案。 最佳答案 也试试 tour.restart()。我刚刚问了同
我正在浏览Tour C++中的Aliases模板。我不明白下面的代码以及如何使用它? template class Vector { public: using value_type = T;
我已经实现了intro.js到我的网站。但我只想在第一次访问时开始旅行。可能是通过使用cookie..网站是用html而不是php制作的.. 最佳答案 JavaScript cookie 是一种解决方
我想使用Knockout.js与 Bootstrap Tour 。特别是,我想将一些数据绑定(bind)点击处理程序附加到游览步骤中的按钮。 我创建了一个像这样的简单游览: var tour = ne
我正在学习着名的 Angular 教程,英雄之旅,但我正在努力迈出一步。 关于 sixth course, HTTP ,我们正在使用一种工具来模拟对 API 的 API 调用。 我想我遵循了所有步骤,
我正在做 Go 之旅,这部分是“slice 就像对数组的引用”。我根本没有更改此代码,所以我很好奇为什么它会耗尽内存。 最佳答案 不应该崩溃,可能是旅游网站有一段时间出了问题......目前任何方式,
A Tour of Go #23 : package main import ( "fmt" "math" ) func pow(x, n, lim float64) float64
无法将其分解为更小的示例...所以我使用 std::multimap 来存储一些值...这是一个简单的多项式类。 问题是最后一个函数,它将两个多项式相乘。当它与它们相乘时,它会产生一个具有多项式的多项
环境:Angular 5+ 来源:https://angular.io/tutorial 在 heroes.component.ts 类中有一个带冒号的赋值,如下所示: export class He
代码 let interestingNumbers = [ "Prime": [2, 3, 5, 7, 11, 13], "Fibonacci": [1, 1, 2, 3, 5, 8]
在 Bootstrap 演练中,我们有下一个和上一个按钮。在此点击下一步按钮,我们进入下一步。 但我想添加条件,如果该输入字段为空,则演练不应移至下一步,它应仅停留在当前步骤。 引用代码如下: rec
我是一名优秀的程序员,十分优秀!