[USACO 1.5.4]跳棋的挑战
地址:http://hustoj.sinaapp.com/problem.php?id=1836
摆棋子,但是其横竖斜向均不可另有棋子,输出前三种解(按字典序输出),并输出总解数
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #define MAX 13 5 using namespace std; 6 7 int a[MAX]; 8 int n,con; 9 10 void print() 11 { 12 for(int i=0;i<n;i++) 13 { 14 cout<<a[i]+1; 15 if(i!=n-1) cout<<" "; 16 } 17 cout<<endl; 18 } 19 20 bool check(int x,int y) 21 { 22 for(int i=0;i<x;i++) 23 { 24 if(a[i]==y) return false;//列 25 if((i+a[i])==(x+y)) return false;//主对角线 26 if(abs(i-x)==abs(y-a[i])) return false;//副对角线 27 } 28 return true; 29 } 30 31 void put(int l) 32 { 33 for(int i=0;i<n;i++) 34 { 35 a[l]=i; 36 if(check(l,a[l])) 37 { 38 if(l==n-1) 39 { 40 con++; 41 if(con<=3) print(); 42 } 43 else put(l+1); 44 } 45 } 46 } 47 48 int main() 49 { 50 ios::sync_with_stdio(false); 51 cin>>n; 52 memset(a,-1,MAX*sizeof(int)); 53 put(0); 54 cout<<con<<endl; 55 return 0; 56 }

浙公网安备 33010602011771号