#include<iostream>
#include<cmath>
using namespace std;
int chess[100][100]={0};
int n=8;
int num=1;
bool sucess(int row,int w)
{
for(int i=0;i<row;i++)
for(int j=0;j<n;j++)
{ if(chess[i][j]==1) if(i==row||j==w||abs(row-i)==abs(w-j)) return false; }
return true;
}
void print()
{
for(int i=0;i<n;i++){ for(int j=0;j<n;j++)
cout<<chess[i][j]<<" ";
cout<<endl; }
}
void dfs(int row)
{
if(row==n)
{ cout<<"No."<<num++<<endl; print();
}
else
{ for(int j=0;j<n;j++) { chess[row][j]=1; if(sucess(row,j)) { dfs(row+1); } chess[row][j]=0; }
}
}
int main()
{
dfs(0);
}

uj5u.com熱心網友回復:
No. num之間是不是有個空格uj5u.com熱心網友回復:
我試了還是不對,它顯示的是演算法錯誤uj5u.com熱心網友回復:
我試了還是不對,它顯示的是演算法錯誤轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/140722.html
標籤:其它技術問題
上一篇:vc6 番茄助手怎么用啊?
下一篇:請問大佬
