- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的任务是修改 8 Queens 程序以使用一维数组并使用蛮力(已经进行了回溯)。我想出了以下代码:
#include <cmath>
#include <iostream>
using namespace std;
bool ok(int board[8]){
for(int j = 0; j <= 7; j++){ //check for repeating digits
cout << "ok loop 1"<<endl;
for (int k = 0; k <= 7; k++)
{
cout << "ok loop 2"<<endl;
if (board[k] = board[j]){ return false; }
}
}
for(int c = 7; c >= 0; c--){ //check if position is safe
cout << "ok loop 3"<<endl;
//int r = 0;
for(int i = 1; i <= c; i++){
cout << "ok loop 4"<<endl;
if(board[c-i] == c)
return false;
else if ((board[c]-i)>0 && board[c-i]-i == 1)
return false;
else if ((board[c]+i)<=7 && board[c-i]+i == 1)
return false;
} // for loop
} // for loop
return true;
} // ok
void print(int board[8], int c){
cout << "Solution " << c << ": " << endl;
for(int i = 0; i < 8; i++){
{
cout << board[i] <<" ";
}
}
cout << endl;
}
int main ()
{
int b[8]={0}; //initialize the array
int count = 0;
for(b[0]=0; b[0]<8; b[0]++)
for(b[1]=0; b[1]<8; b[1]++)
for(b[2]=0; b[2]<8; b[2]++)
for(b[3]=0 ; b[3]<8; b[3]++)
for(b[4]=0; b[4]<8; b[4]++)
for(b[5]=0; b[5]<8; b[5]++)
for(b[6]=0; b[6]<8; b[6]++)
for(b[7]=0; b[7]<8; b[7]++)
if(ok(b))
{
count++;
print(b, count);
}
system("PAUSE");
return 0;
}
它一直在循环,我不确定为什么。有人介意帮助我吗?
最佳答案
有几点可以改进:
ok()
,而不仅仅是指向非常量整数的指针,编译器可能会告诉您其中一个问题。关于c++ - 如何用蛮力解决 8 皇后一维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15163016/
我正在尝试编写一个程序,以表格的形式计算我的结果。我有一个液压升降机,可以显示作用在直径 (D1) 的活塞上的小力(我称之为 F1)可以乘以作用在直径 (D2) 的活塞上的大力 (F2) ,电梯的运行
我有一个 fiddle :https://jsfiddle.net/mvLf579a/284/ 此 fiddle 允许您使用 D3 径向布局来布置力网络,并将布局重置为默认力布局。但是,重置时链接强度
我想实现表格 View 单元格重叠,因为我想实现这种效果: 所以基本上细胞会一个接一个地堆叠。我的目标是 iOS7+(目前正在测试 iOS8)。 我目前正在做一个 CAGradientLayer,它被
尽管在 box2d 前花了几个小时,但我仍然不明白 applyforce 和 applyimpulse 是如何工作的。我尝试使用一些视觉效果来更好地理解正在发生的事情(通过在 body 位置和应用点之
首先,我是 D3 的新手。我正在尝试使用这些示例在单个 D3 图中实现不同的行为: Drag + Zoom 力导向图 但我的图表在几秒钟后卡住,我不明白为什么... 这是我的代码:http://jsf
所以,我在 Xcode 上用 Swift 玩了这个游戏,我有一个跳跃的 SKShapeNode。不过,我希望这种跳跃与玩家的触感相称。因此,小水龙头会跳得小,而轻快的水龙头会跳得更高。怎么可能做到这一
在 ARKit/SceneKit 中,当用户点击按钮时,我想对我的节点施加一个脉冲。我希望冲动来自当前用户的角度。这意味着节点将远离用户的视角。多亏了这段代码,我能够获得当前的方向/方向: func
我正在使用 Angular2 并拥有父子组件。我的子组件正在使用 DatePipe 提供程序,因为父单元测试失败并给出以下错误, 错误:非法状态:无法加载管道 DatePipe 的摘要。 如何解决这个
我是一名优秀的程序员,十分优秀!