USACO 1.5 Checker Challenge

1 /*
2 ID: jiafeim1
3 PROG: checker
4 LANG: C++
5 */
6 #include <iostream>
7 #include <fstream>
8 #include <algorithm>
9
10 using namespace std;
11
12 bool column[16]={false};
13 bool lu[40] = {false};
14 bool ru[40] = {false};
15
16 int res[15];
17 int top = 0;
18 int n;
19 int countx = 0;
20 ofstream fout ("checker.out");
21 void work()
22 {
23 if(top==n)
24 {
25 ++countx;
26 if(countx<=3)
27 {
28 fout<<res[0]+1;
29 for(int i = 1;i!=n;++i)
30 {
31 fout<<" "<<res[i]+1;
32 }
33 fout<<endl;
34 }
35 return;
36 }
37 else
38 {
39 for(int i =0 ;i!=n;++i)
40 {
41 if(!column[i]&&!(lu[i+top])&&!(ru[n-i-1+top]))
42 {
43 column[i]=lu[i+top]=ru[n-i-1+top] = true;
44 res[top++] = i;
45 work();
46 --top;
47 column[i] = lu[i+top] =ru[n-i-1+top] = false;
48 }
49 }
50 }
51 }
52 void work1(int num)
53 {
54
55 column[num]=lu[num+top]=ru[n-num-1+top] = true;
56 res[top++] = num;
57 work();
58 --top;
59 column[num] = lu[num+top] =ru[n-num-1+top] = false;
60
61 }
62 int main()
63 {
64
65 ifstream fin ("checker.in");
66
67 fin>>n;
68 if(n<=8)
69 {
70 for(int i =0;i!=n;++i) work1(i);
71 }
72 else
73 {
74 if(n%2==0)
75 {
76 for(int i = 0;i!=n/2;++i) work1(i);
77 countx*=2;
78 }
79 else
80 {
81 for(int i = 0;i!=n/2;++i) work1(i);
82 int temp = countx;
83 work1(n/2);
84 countx = countx + temp;
85 }
86 }
87
88
89 fout<<countx<<endl;
90 fin.close();
91 fout.close();
92 return 0;
93 }
posted @ 2011-05-02 16:30  幻魇  阅读(272)  评论(0编辑  收藏  举报