标签:clu diagonal ica blog second char solution integer const
Examine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more than one in any diagonal. (Diagonals run from southeast to northwest and southwest to northeast and include all diagonals, not just the major two.)
Column 1 2 3 4 5 6 ------------------------- 1 | | O | | | | | ------------------------- 2 | | | | O | | | ------------------------- 3 | | | | | | O | ------------------------- 4 | O | | | | | | ------------------------- 5 | | | O | | | | ------------------------- 6 | | | | | O | | -------------------------
The solution shown above is described by the sequence 2 4 6 1 3 5, which gives the column positions of the checkers for each row from 1 to 6:
ROW | 1 | 2 | 3 | 4 | 5 | 6 |
COLUMN | 2 | 4 | 6 | 1 | 3 | 5 |
This is one solution to the checker challenge. Write a program that finds all unique solution sequences to the Checker Challenge (with ever growing values of N). Print the solutions using the column notation described above. Print the first three solutions in numerical order, as if the checker positions form the digits of a large number, and then a line with the total number of solutions.
Special note: the larger values of N require your program to be especially efficient. Do not precalculate the value and print it (or even find a formula for it); that‘s cheating. Work on your program until it can solve the problem properly. If you insist on cheating, your login to the USACO training pages will be removed and you will be disqualified from all USACO competitions. YOU HAVE BEEN WARNED.
A single line that contains a single integer N (6 <= N <= 13) that is the dimension of the N x N checkerboard.
6
The first three lines show the first three solutions found, presented as N numbers with a single space between them. The fourth line shows the total number of solutions found.
2 4 6 1 3 5 3 6 2 5 1 4 4 1 5 2 6 3 4
HINT 1 HINT 2 HINT 3 HINT 4 HINT 5 HINT 6
——————————————————————题解
那么USACO这个阶梯式题库的选题人大概是觉得
你前面做过的网络流啊,最小割啊,字典树啊,tarjan啊,二分图啊,最小环啊,欧拉路啊,记搜啊,各种各样奇怪的dp,各种各样奇怪的剪枝
都没n皇后难,n皇后才是最难的,n皇后是坠吼的!
【冷漠脸】
比以前加了个二进制优化
1 /* 2 LANG: C++ 3 PROG: checker 4 */ 5 #include <iostream> 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 #define siji(i,x,y) for(int i=(x) ; i <= (y) ; ++i) 11 #define xiaosiji(i,x,y) for(int i = (x) ; i < (y); ++i) 12 #define gongzi(j,x,y) for(int j = (x) ; j >= (y) ; --j) 13 #define ivorysi 14 #define mo 11447 15 #define eps 1e-8 16 #define o(x) ((x)*(x)) 17 using namespace std; 18 typedef long long ll; 19 int LeftDiagonal,RightDiagonal,Column; 20 int rec[15]; 21 int n,ans,cnt; 22 void Print() { 23 siji(i,1,n) { 24 printf("%d%c",rec[i]," \n"[i==n]); 25 } 26 } 27 void dfs(int k) { 28 if(k>n) { 29 ++ans; 30 if(cnt<3) {++cnt;Print();} 31 } 32 siji(i,1,n){ 33 if((LeftDiagonal>>(k+i)&1)==0 && (RightDiagonal>>(k+n-i+1)&1)==0 && (Column>>i&1)==0 ){ 34 //&的优先级比==低?? 35 rec[k]=i; 36 LeftDiagonal|=(1<<(k+i)); 37 RightDiagonal|=(1<<(k+n-i+1)); 38 Column|=(1<<i); 39 dfs(k+1); 40 LeftDiagonal^=(1<<(k+i)); 41 RightDiagonal^=(1<<(k+n-i+1)); 42 Column^=(1<<i); 43 } 44 } 45 } 46 void solve() { 47 scanf("%d",&n); 48 dfs(1); 49 printf("%d\n",ans); 50 } 51 int main(int argc, char const *argv[]) 52 { 53 #ifdef ivorysi 54 freopen("checker.in","r",stdin); 55 freopen("checker.out","w",stdout); 56 #else 57 freopen("f1.in","r",stdin); 58 //freopen("f1.out","w",stdout); 59 #endif 60 solve(); 61 return 0; 62 }
标签:clu diagonal ica blog second char solution integer const
原文地址:http://www.cnblogs.com/ivorysi/p/7041219.html