标签:style blog http os io strong for 2014
代码:
/* 八皇后 by Rowandjj 2014/8/8 */ #include<iostream> using namespace std; int num = 0;//种数 void permutation(int arr[],int len,int index) { if(index == len - 1) { bool flag = true; for(int i = 0; i < len; i++) { for(int j = i+1; j < len; j++) { if(i-j==arr[i]-arr[j] || i-j==arr[j]-arr[i])//在对角线上 { flag = false; break; } } } if(flag) { num++; for(int i = 0; i < len; i++) { cout<<arr[i]<<" "; } cout<<endl; } }else { for(int i = index;i < len; i++) { int temp = arr[i]; arr[i] = arr[index]; arr[index] = temp; permutation(arr,len,index+1); temp = arr[i]; arr[i] = arr[index]; arr[index] = temp; } } } void eightQueenSolution() { int len = 8; int arr[] = {0,1,2,3,4,5,6,7}; permutation(arr,len,0); } int main() { eightQueenSolution(); cout<<num<<endl; return 0; }
标签:style blog http os io strong for 2014
原文地址:http://blog.csdn.net/chdjj/article/details/38444193