为什么我编的八皇后问题只有82个解 C++
以下我的程序:#include<iostream>#include<iomanip>usingnamespacestd;//LineNum[i]表示第i列的皇后要放的行位...
以下 我的程序:
#include <iostream>
#include <iomanip>
using namespace std;
//LineNum[i]表示第i列的皇后要放的行位置(只用其中的列号1到8);
// a[i]为true(i =1,2,…,8)表示第i行上尚未放皇后;
// b[i]为true(i =0,1,2,…,14)表示第i条斜对角线上尚未放皇后(斜对角线指的是"/"状对角线,该对角线上各点的行列号之和i+j为一个常数);
// c[i]为true(i=0,1,2,…,14)表示第i条反斜对角线上尚未放皇后(反斜对角线指的是"\"状对角线,该对角线上各点的行列号之差i-j为一个常数)。
int LineNum[9];
bool b[9],a[15],c[15];
void solve(int i,int& ok);
void main()
{
for(int i=0;i<15;i++)
{
if(i>=1&&i<9) a[i]=b[i]=c[i]=true;
else b[i]=c[i]=true;
}
int ok=0;
solve(1,ok);
cout<<"The total number is "<<ok<<" !"<<endl;
}
void solve(int i,int& ok)//从第i列开始放
{
if(i==9){//i!=8
ok++;
for(int m=1;m<9;m++)//行
{ for(int n=1;n<9;n++)//列
{
if(LineNum[n]==m) cout<<'Q'<<' ';
else cout<<'+'<<' ';
}
cout<<endl;
}
cout<<"这是第"<<ok<<"次!"<<"haha"<<endl;
}
else{
for(int j=1;j<9;j++)
{
if(a[j]&&b[i+j-2]&&c[i-j+7]) {LineNum[i]=j;//cout<<'<'<<j<<'>'<<i<<'<'<<i<<'>'<<endl;
a[j]=false;
b[i+j-2]=false;
c[i-j+7]=false;
solve(i+1,ok);
a[j]=true;
b[i+j-2]=true;
c[i-j+7]=true;}
}
}
}
我不需要别人的程序,我想有高手帮我改下!
不要发这种东西上来捣乱 展开
#include <iostream>
#include <iomanip>
using namespace std;
//LineNum[i]表示第i列的皇后要放的行位置(只用其中的列号1到8);
// a[i]为true(i =1,2,…,8)表示第i行上尚未放皇后;
// b[i]为true(i =0,1,2,…,14)表示第i条斜对角线上尚未放皇后(斜对角线指的是"/"状对角线,该对角线上各点的行列号之和i+j为一个常数);
// c[i]为true(i=0,1,2,…,14)表示第i条反斜对角线上尚未放皇后(反斜对角线指的是"\"状对角线,该对角线上各点的行列号之差i-j为一个常数)。
int LineNum[9];
bool b[9],a[15],c[15];
void solve(int i,int& ok);
void main()
{
for(int i=0;i<15;i++)
{
if(i>=1&&i<9) a[i]=b[i]=c[i]=true;
else b[i]=c[i]=true;
}
int ok=0;
solve(1,ok);
cout<<"The total number is "<<ok<<" !"<<endl;
}
void solve(int i,int& ok)//从第i列开始放
{
if(i==9){//i!=8
ok++;
for(int m=1;m<9;m++)//行
{ for(int n=1;n<9;n++)//列
{
if(LineNum[n]==m) cout<<'Q'<<' ';
else cout<<'+'<<' ';
}
cout<<endl;
}
cout<<"这是第"<<ok<<"次!"<<"haha"<<endl;
}
else{
for(int j=1;j<9;j++)
{
if(a[j]&&b[i+j-2]&&c[i-j+7]) {LineNum[i]=j;//cout<<'<'<<j<<'>'<<i<<'<'<<i<<'>'<<endl;
a[j]=false;
b[i+j-2]=false;
c[i-j+7]=false;
solve(i+1,ok);
a[j]=true;
b[i+j-2]=true;
c[i-j+7]=true;}
}
}
}
我不需要别人的程序,我想有高手帮我改下!
不要发这种东西上来捣乱 展开
3个回答
展开全部
七古·古意
唐·李颀
男儿事长征,少小幽燕客。赌胜马蹄下,由来轻七尺。杀人莫敢前,须如猬毛磔。黄云陇底白云飞,未得报恩不得归。辽东小妇年十五,惯弹琵琶解歌舞。今为羌笛出塞声,使我三军泪如雨。
乐府·古从军行
白日登山望烽火,黄昏饮马傍交河。行人刁斗风沙暗,公主琵琶幽怨多。野营万里无城郭,雨雪纷纷连大漠。胡雁哀鸣夜夜飞,胡儿眼泪双双落。闻道玉门犹被遮,应将性命逐轻车。年年战骨埋荒处,空见蒲桃入汉家。
七律·咏怀古迹五首
唐·杜甫
支离东北风尘际,漂泊西南天地间。三峡楼台淹日月,五溪衣服共云山。羯胡事主终无赖,词客哀时且未还。庾信平生最萧瑟,暮年诗赋动江关。摇落深知宋玉悲,风流儒雅亦吾师。怅望千秋一洒泪,萧条异代不同时。江山故宅空文藻,云雨荒台岂梦思。最是楚宫俱泯灭,舟人指点到今疑。群山万壑赴荆门,生长明妃尚有村。一去紫台连朔漠,独留青冢向黄昏。画图省识春风面,环佩空归月夜魂。千载琵琶作胡语,分明怨恨曲中论。蜀主窥吴幸三峡,崩年亦在永安宫。翠华想像空山里,玉殿虚无野寺中。古庙杉松巢水鹤,岁时伏腊走村翁。武侯祠屋常邻近,一体君臣祭祀同。诸葛大名垂宇宙,宗臣遗像肃清高。三分割据纡筹策,万古云霄一羽毛。伯仲之间见伊吕,指挥若定失萧曹。福移汉祚难恢复,志决身歼军务劳。
唐·李颀
男儿事长征,少小幽燕客。赌胜马蹄下,由来轻七尺。杀人莫敢前,须如猬毛磔。黄云陇底白云飞,未得报恩不得归。辽东小妇年十五,惯弹琵琶解歌舞。今为羌笛出塞声,使我三军泪如雨。
乐府·古从军行
白日登山望烽火,黄昏饮马傍交河。行人刁斗风沙暗,公主琵琶幽怨多。野营万里无城郭,雨雪纷纷连大漠。胡雁哀鸣夜夜飞,胡儿眼泪双双落。闻道玉门犹被遮,应将性命逐轻车。年年战骨埋荒处,空见蒲桃入汉家。
七律·咏怀古迹五首
唐·杜甫
支离东北风尘际,漂泊西南天地间。三峡楼台淹日月,五溪衣服共云山。羯胡事主终无赖,词客哀时且未还。庾信平生最萧瑟,暮年诗赋动江关。摇落深知宋玉悲,风流儒雅亦吾师。怅望千秋一洒泪,萧条异代不同时。江山故宅空文藻,云雨荒台岂梦思。最是楚宫俱泯灭,舟人指点到今疑。群山万壑赴荆门,生长明妃尚有村。一去紫台连朔漠,独留青冢向黄昏。画图省识春风面,环佩空归月夜魂。千载琵琶作胡语,分明怨恨曲中论。蜀主窥吴幸三峡,崩年亦在永安宫。翠华想像空山里,玉殿虚无野寺中。古庙杉松巢水鹤,岁时伏腊走村翁。武侯祠屋常邻近,一体君臣祭祀同。诸葛大名垂宇宙,宗臣遗像肃清高。三分割据纡筹策,万古云霄一羽毛。伯仲之间见伊吕,指挥若定失萧曹。福移汉祚难恢复,志决身歼军务劳。
展开全部
#include<stdio.h>
int num0=0;
int a[8],b[15],c[15],i;
int rs[8];
void try(int i)
{int k,j;
for (j=1;j<=8;j++)
if (a[j-1]+b[i+j-2]+c[i-j+7]==3)/*如果第i行,第j列可用,占用(i,j) */
{a[j-1]=0; b[i+j-2]=0; c[i-j+7]=0;
rs[i-1]=j;
if (i<8) try(i+1);/*继续找第i+1个皇后*/
else /*输出一组解*/
{num0++;
printf("rezult#%3d:",num0);
for(k=0;k<=7;k++)
printf("%2d",rs[k]);
printf("\n");
if (num0 %20==0)
{ printf("Press <Enter> to continue...");
while (getchar()!='\n') {};
}
}
a[j-1]=1; b[i+j-2]=1; c[i-j+7]=1;/*重置数组*/
}
}
main()
{ int i;
for(i=0;i<=7;i++) a[i]=1;
for(i=0;i<=14;i++) b[i]=1;
for(i=0;i<=14;i++) c[i]=1;
try(1);
printf("######################");
printf("\n");
printf("There are %d methods",num0);
getchar();
}
int num0=0;
int a[8],b[15],c[15],i;
int rs[8];
void try(int i)
{int k,j;
for (j=1;j<=8;j++)
if (a[j-1]+b[i+j-2]+c[i-j+7]==3)/*如果第i行,第j列可用,占用(i,j) */
{a[j-1]=0; b[i+j-2]=0; c[i-j+7]=0;
rs[i-1]=j;
if (i<8) try(i+1);/*继续找第i+1个皇后*/
else /*输出一组解*/
{num0++;
printf("rezult#%3d:",num0);
for(k=0;k<=7;k++)
printf("%2d",rs[k]);
printf("\n");
if (num0 %20==0)
{ printf("Press <Enter> to continue...");
while (getchar()!='\n') {};
}
}
a[j-1]=1; b[i+j-2]=1; c[i-j+7]=1;/*重置数组*/
}
}
main()
{ int i;
for(i=0;i<=7;i++) a[i]=1;
for(i=0;i<=14;i++) b[i]=1;
for(i=0;i<=14;i++) c[i]=1;
try(1);
printf("######################");
printf("\n");
printf("There are %d methods",num0);
getchar();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#ifndef _QUEENBOARD_H_
#define _QUEENBOARD_H_
const int BOARDSIZE = 8;
using namespace std;
class Queenboard {
private:
bool board[BOARDSIZE][BOARDSIZE];
public:
Queenboard();
bool is_space_under_attack(int, int) const;
void occupy_space(int, int);
void clear_column(int);
friend ostream& operator<<(ostream& out, const Queenboard& cb);
};
Queenboard::Queenboard() {
// Initialize the board to contain zero queens.
for (int row = 0; row < BOARDSIZE; row++) {
for (int col = 0; col < BOARDSIZE; col++) {
board[row][col] = false;
}
}
}
ostream& operator<<(ostream& out, const Queenboard& cb) {
// output the board
for (int row = 0; row < BOARDSIZE; row++) {
out << "---------------------------------" << endl;
for (int col = 0; col < BOARDSIZE; col++) {
out << "|";
if ( cb.board[row][col]) {
out << " Q ";
}
else {
out << " ";
}
}
out << "|" << endl;
}
out << "---------------------------------" << endl;
return out;
}
void Queenboard::clear_column(int col) {
if (col >= BOARDSIZE || col < 0) {
throw out_of_range("Queenboard::clear_column()");
}
for (int row = 0; row < BOARDSIZE; row++) {
board[row][col] = false;
}
}
void Queenboard::occupy_space(int row, int col) {
if (col >= BOARDSIZE || col < 0 ||
row >= BOARDSIZE || row < 0) {
throw out_of_range("Queenboard::occupy_space()");
}
// places a queen on the board
board[row][col] = true;
}
bool Queenboard::is_space_under_attack(int row, int col) const {
if (col >= BOARDSIZE || col < 0 ||
row >= BOARDSIZE || row < 0) {
throw out_of_range("Queenboard::is_space_under_attack()");
}
// check to the left
int i = col - 1;
while (i >= 0) {
if (board[row][i]) return true;
i--;
}
// check diagonal up and left
int j = row - 1;
int k = col - 1;
while (j >= 0 && k >= 0) {
if (board[j][k]) return true;
j--; k--;
}
// check diagonal down and left
j = row + 1;
k = col - 1;
while (j < BOARDSIZE && k >= 0) {
if (board[j][k]) return true;
j++; k--;
}
return false;
}
#endif
#include <iostream>
#include <cstdlib>
#include <stdexcept>
#include "Queenboard.h"
using namespace std;
bool place_queens(Queenboard& qb, int col);
int main(int argc, char* argv[]) {
try {
Queenboard qb;
if (! place_queens(qb, 0)) {
cout << "No solution found.\n";
}
return EXIT_SUCCESS;
}
catch (exception& e) {
cerr << e.what() << "\n";
}
catch (...) {
cerr << "Unknown exception caught.\n";
}
return EXIT_FAILURE;
}
bool place_queens(Queenboard& qb, int col) {
bool inserted = false;
for (int row = 0; row < BOARDSIZE; row++) {
if (! qb.is_space_under_attack(row, col)) {
// insert a queen
qb.occupy_space(row, col);
inserted = true;
if (col == BOARDSIZE - 1) {
// solution found!
cout << qb << "\n";
return true;
}
else {
// place a queen in the next column
if (place_queens(qb, col + 1)) {
return true;
}
else {
inserted = false;
}
}
}
}
if (! inserted) {
// backtrack to previous column
qb.clear_column(col - 1);
return false;
}
}
#define _QUEENBOARD_H_
const int BOARDSIZE = 8;
using namespace std;
class Queenboard {
private:
bool board[BOARDSIZE][BOARDSIZE];
public:
Queenboard();
bool is_space_under_attack(int, int) const;
void occupy_space(int, int);
void clear_column(int);
friend ostream& operator<<(ostream& out, const Queenboard& cb);
};
Queenboard::Queenboard() {
// Initialize the board to contain zero queens.
for (int row = 0; row < BOARDSIZE; row++) {
for (int col = 0; col < BOARDSIZE; col++) {
board[row][col] = false;
}
}
}
ostream& operator<<(ostream& out, const Queenboard& cb) {
// output the board
for (int row = 0; row < BOARDSIZE; row++) {
out << "---------------------------------" << endl;
for (int col = 0; col < BOARDSIZE; col++) {
out << "|";
if ( cb.board[row][col]) {
out << " Q ";
}
else {
out << " ";
}
}
out << "|" << endl;
}
out << "---------------------------------" << endl;
return out;
}
void Queenboard::clear_column(int col) {
if (col >= BOARDSIZE || col < 0) {
throw out_of_range("Queenboard::clear_column()");
}
for (int row = 0; row < BOARDSIZE; row++) {
board[row][col] = false;
}
}
void Queenboard::occupy_space(int row, int col) {
if (col >= BOARDSIZE || col < 0 ||
row >= BOARDSIZE || row < 0) {
throw out_of_range("Queenboard::occupy_space()");
}
// places a queen on the board
board[row][col] = true;
}
bool Queenboard::is_space_under_attack(int row, int col) const {
if (col >= BOARDSIZE || col < 0 ||
row >= BOARDSIZE || row < 0) {
throw out_of_range("Queenboard::is_space_under_attack()");
}
// check to the left
int i = col - 1;
while (i >= 0) {
if (board[row][i]) return true;
i--;
}
// check diagonal up and left
int j = row - 1;
int k = col - 1;
while (j >= 0 && k >= 0) {
if (board[j][k]) return true;
j--; k--;
}
// check diagonal down and left
j = row + 1;
k = col - 1;
while (j < BOARDSIZE && k >= 0) {
if (board[j][k]) return true;
j++; k--;
}
return false;
}
#endif
#include <iostream>
#include <cstdlib>
#include <stdexcept>
#include "Queenboard.h"
using namespace std;
bool place_queens(Queenboard& qb, int col);
int main(int argc, char* argv[]) {
try {
Queenboard qb;
if (! place_queens(qb, 0)) {
cout << "No solution found.\n";
}
return EXIT_SUCCESS;
}
catch (exception& e) {
cerr << e.what() << "\n";
}
catch (...) {
cerr << "Unknown exception caught.\n";
}
return EXIT_FAILURE;
}
bool place_queens(Queenboard& qb, int col) {
bool inserted = false;
for (int row = 0; row < BOARDSIZE; row++) {
if (! qb.is_space_under_attack(row, col)) {
// insert a queen
qb.occupy_space(row, col);
inserted = true;
if (col == BOARDSIZE - 1) {
// solution found!
cout << qb << "\n";
return true;
}
else {
// place a queen in the next column
if (place_queens(qb, col + 1)) {
return true;
}
else {
inserted = false;
}
}
}
}
if (! inserted) {
// backtrack to previous column
qb.clear_column(col - 1);
return false;
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询