8.8-N皇后

来源:互联网 发布:怎么用淘宝助理 编辑:程序博客网 时间:2024/06/05 14:42

Write an algorithm to print all ways of arranging eight queens on a chess board so that none of them share the same row, column or diagonal.

#include <iostream>using namespace std;int n=8;int tot=0;int col[8];void dfs(int cur){    if(cur==n)        tot++;//or print board    else for(int i=0; i<n; i++)        {            int ok=1;            col[cur]=i;            for(int j=0; j<cur; j++)                if(col[cur]==col[j] || cur-col[cur]== j-col[j] || cur+col[cur]==j+col[j])                {                    ok=0;                    break;                }            if(ok)                dfs(cur+1);        }}int main(){    dfs(0);    cout << tot << endl;    return 0;}


0 0
原创粉丝点击