八皇后问题

最近状态很迷啊……提不起兴致,敲了个水题,我也不知道对不对……

(三无真是太可爱了)

/*任意两个选出的格子不能在同一行,同一列,同一对角线上
问一共有几种可能*/ 
#include<bits/stdc++.h>
using namespace std;
int tot;/*解的个数*/ 
int n;
int C[100];
void search(int cur)
{
    int i,j;
    if(cur==n) tot++;
    else
    for(i=0;i<n;i++)
    {
        int ok=1;
        C[cur]=i;
        for(j=0;j<cur;j++)
        {
            if(C[j]==C[cur]||cur-C[cur]==j-C[j]||cur+C[cur]==j+C[j])
            {
                ok=0;break;
            }
        }
        if(ok)
        search(cur+1);
        
     } 
 } 
 int main()
 {
     while(cin>>n)
     {
         tot=0;
         search(0);
        cout<<tot<<endl;
    }
     return 0;
     
 }

 

posted @ 2017-08-16 15:21  时荣  阅读(87)  评论(0编辑  收藏  举报