#include <iostream> #include <cstdlib> using namespace std; void showQueens(int queenArr[],int nlen,int nSolution)//打印棋盘和皇后 { cout<<nSolution<<endl;//解法数量 for(int i = 0;i < nlen; ++i) { for(int j = 0; j < queenArr[i]; ++j)//打印皇后前面的格子 { cout<<"1";//空格子 } cout<<"Q";//皇后 for(int z = 0; z < nlen-queenArr[i]-1; ++z)//打印皇后后面的格子 { cout<<"1"; } cout<<endl; } cout<<" "<<endl; } bool rule(int queenArr[])//判断是否符合规则 { for(int i = 1; i <= 8; ++i) { for(int j = 0;j <= i-1; ++j) { if(queenArr[i] == queenArr[j])//判断皇后是否在同一列 { return true; } if(abs(queenArr[i]-queenArr[j]) == abs(i-j))//判断皇后是否在对角45° { return true; } } } return false; } void enumQueensPositon(int queenArr[],int &nSolution)//移动皇后 { for(queenArr[0] = 0; queenArr[0] < 8; ++queenArr[0]) for(queenArr[1] = 0;queenArr[1] < 8; ++queenArr[1]) for(queenArr[2] = 0;queenArr[2] < 8; ++queenArr[2]) for(queenArr[2]=0;queenArr[2] < 8; ++queenArr[2]) for(queenArr[4] = 0;queenArr[4] < 8; ++queenArr[4]) for(queenArr[5] = 0;queenArr[5] < 8; ++queenArr[5]) for(queenArr[6] = 0; queenArr[6] < 8; ++queenArr[6]) for(queenArr[7] = 0; queenArr[7] < 8; ++queenArr[7]) { if (rule(queenArr)) { continue; } else { ++nSolution; showQueens(queenArr ,8, nSolution); } } } int main() { int queenArr[8]; //每个数组表示一个皇后 int nSolution = 0;//解法数量 enumQueensPositon(queenArr,nSolution); return 0; }
这是我的代码,我的问题是不知道为什么代码运行后只有18种解法,想问问哪里出错了?
我成功运行的代码,自己对比对比。
#include <iostream> #include <iomanip> #include <cmath> using std::setw; using std::cout; using std::endl; //非递归算法解决八皇后问题。求出可能的92种。 // using std::abs; int main() { static int queen[9]; static int count=1; for (int A=1;A<=8;A++) { for (int B=1;B<=8;B++) { if (B==A) { continue; } queen[2]=B; if ((abs(B-A))==1) { continue; } queen[1]=A; for (int C=1;C<=8;C++) { if ((C==B) || (C==A)) { continue; } if ((abs(C-B)==1)||(abs(C-A)==2)) { continue; } queen[3]=C; for (int D=1;D<=8;D++) { if ((D==C)||(D==B)||(D==A)) { continue; } if ((abs(D-C)==1)||(abs(D-B)==2)||(abs(D-A)==3)) { continue; } queen[4]=D; for (int E=1;E<=8;E++) { if ((E==D)||(E==C)||(E==B)||(E==A)) { continue; } if ((abs(E-D)==1)||(abs(E-C)==2)||(abs(E-B)==3)||(abs(E-A)==4)) { continue; } queen[5]=E; for (int F=1;F<=8;F++) { if ((F==E)||(F==D)||(F==C)||(F==B)||(F==A)) { continue; } if ((abs(F-E)==1)||(abs(F-D)==2)||(abs(F-C)==3)||(abs(F-B)==4)||(abs(F-A)==5)) { continue; } queen[6]=F; for (int G=1;G<=8;G++) { if ((G==F)||(G==E)||(G==D)||(G==C)||(G==B)||(G==A)) { continue; } if ((abs(G-F)==1)||(abs(G-E)==2)||(abs(G-D)==3)||(abs(G-C)==4)||(abs(G-B)==5)||(abs(G-A)==6)) { continue; } queen[7]=G; for (int I=1;I<=8;I++) { if ((I==G)||(I==F)||(I==E)||(I==D)||(I==C)||(I==B)||(I==A)) { continue; } if ((abs(I-G)==1)||(abs(I-F)==2)||(abs(I-E)==3)||(abs(I-D)==4)||(abs(I-C)==5) ||(abs(I-B)==6)||(abs(I-A)==7)) { continue; } queen[8]=I; cout<<" NO."<<setw(2)<<count<<": "; for (int i=1;i<=8;i++) { cout<<setw(3)<<queen[i]; } count++; cout<<endl; } } } } } } } } return 0; }
建议你先初始化 queenArr 数组,你这样是一边初始化 一边去判断是否可以成功
#include <iostream> #include <cstdlib> using namespace std; void showQueens(int queenArr[],int nlen,int nSolution)//打印棋盘和皇后 { cout<<nSolution<<endl;//解法数量 for(int i = 0;i < nlen; ++i) { for(int j = 0; j < queenArr[i]; ++j)//打印皇后前面的格子 { cout<<"1";//空格子 } cout<<"Q";//皇后 for(int z = 0; z < nlen-queenArr[i]-1; ++z)//打印皇后后面的格子 { cout<<"1"; } cout<<endl; } cout<<" "<<endl; } bool rule(int queenArr[])//判断是否符合规则 { for(int i = 0; i <= 7; ++i) { for(int j = 0;j <= i-1; ++j) { if(queenArr[i] == queenArr[j])//判断皇后是否在同一列 { return true; } if(abs(queenArr[i]-queenArr[j]) == abs(i-j))//判断皇后是否在对角45° { return true; } } } return false; } void enumQueensPositon(int queenArr[],int &nSolution)//移动皇后 { for(queenArr[0] = 0; queenArr[0] < 8; ++queenArr[0]) for(queenArr[1] = 0;queenArr[1] < 8; ++queenArr[1]) for(queenArr[2] = 0;queenArr[2] < 8; ++queenArr[2]) for(queenArr[3]=0;queenArr[3] < 8; ++queenArr[3]) for(queenArr[4] = 0;queenArr[4] < 8; ++queenArr[4]) for(queenArr[5] = 0;queenArr[5] < 8; ++queenArr[5]) for(queenArr[6] = 0; queenArr[6] < 8; ++queenArr[6]) for(queenArr[7] = 0; queenArr[7] < 8; ++queenArr[7]) { if (rule(queenArr)) { continue; } else { ++nSolution; showQueens(queenArr ,8, nSolution); } } } int main() { int queenArr[8]; //每个数组表示一个皇后 int nSolution = 0;//解法数量 enumQueensPositon(queenArr,nSolution); return 0; }
你的
for(queenArr[2] = 0;queenArr[2] < 8; ++queenArr[2])
for(queenArr[2]=0;queenArr[2] < 8; ++queenArr[2])
for(int i = 1; i <= 8; ++i)
写错了
应该是
for(queenArr[2] = 0;queenArr[2] < 8; ++queenArr[2])
for(queenArr[3]=0;queenArr[3] < 8; ++queenArr[3])
for(int i = 0; i <= 7; ++i)
佩服你们打出这么多个for的毅力