Checker Challenge

#include "stdafx.h"   
#include<iostream>   
#include<fstream>   
using namespace std;  
const int MAX=13;  
int N;         //維數   
int count;        //計數,保證前三個輸出,後面的不輸出。   
int col[MAX];  
ofstream fout;  
bool check(int x,int y)  
{  
    for(int i=0;i<x;i++)  
    {  
        int j=col[i];  
        if(x==i || y==j)    //爲了行列不衝突   
            return false;  
        if(i-j==x-y || i+j==x+y) //爲了對角不衝突   
            return false;  
    }  
    return true;  
}  
int DFS(int x)  
{  
    if(x==N)  
    {  
       if(count<3)  
       {  
           for(int i=0;i<N-1;i++)  
               fout<<col[i]+1<<" ";  
           fout<<col[N-1]+1<<endl;  
       }  
       count++;  
    }  
    else  
    {  
        for(int j=0;j<N;j++)  
        {  
            if(check(x,j))  
            {  
                col[x]=j;  
                DFS(x+1);  
                col[x]=0;  
            }  
        }  
    }  
    return 0;  
}  
int main()  
{  
    fout.open("checker.out");  
    ifstream fin("checker.in");  
    fin>>N;  
    DFS(0);  
    fout<<count<<endl;  
    return 0;  
}  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章