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 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.
TIME LIMIT: 1 CPU second
PROGRAM NAME: checker
INPUT FORMAT
A single line that contains a single integer N (6 <= N <= 13) that is the dimension of the N x N checkerboard.SAMPLE INPUT (file checker.in)
6
OUTPUT FORMAT
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.SAMPLE OUTPUT (file checker.out)
2 4 6 1 3 5 3 6 2 5 1 4 4 1 5 2 6 3 4
描述
检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行、每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子。
0 1 2 3 4 5 6 ------------------------- 1 | | O | | | | | ------------------------- 2 | | | | O | | | ------------------------- 3 | | | | | | O | ------------------------- 4 | O | | | | | | ------------------------- 5 | | | O | | | | ------------------------- 6 | | | | | O | | -------------------------上面的布局可以用序列2 4 6 1 3 5来描述,第i个数字表示在第i行的相应位置有一个棋子,如下:
行号 1 2 3 4 5 6 列号 2 4 6 1 3 5这只是跳棋放置的一个解。请编一个程序找出所有跳棋放置的解。并把它们以上面的序列方法输出。解按字典顺序排列。请输出前3个解。最后一行是解的总个数。
格式
测试时间: 1s
程序名: checker
输入格式:
(checker.in)
一个数字N (6 <= N <= 13) 表示棋盘是N x N大小的。
输出格式:
(checker.out)
前三行为前三个解,每个解的两个数字之间用一个空格隔开。第四行只有一个数字,表示解的总数。
按位运算求解:
//代码摘自http://www.cnblogs.com/albert1017/archive/2013/01/15/2860973.html
//作者的注释写的很详细,便于理解
/* ** 目前最快的N皇后递归解决方法 ** N Queens Problem ** 试探-回溯算法,递归实现 */ #include "iostream" using namespace std; #include "time.h" // sum用来记录皇后放置成功的不同布局数;upperlim用来标记所有列都已经放置好了皇后。 long sum = 0, upperlim = 1; // 试探算法从最右边的列开始。 void test(long row, long ld, long rd) { if (row != upperlim) { // row,ld,rd进行“或”运算,求得所有可以放置皇后的列,对应位为0, // 然后再取反后“与”上全1的数,来求得当前所有可以放置皇后的位置,对应列改为1 // 也就是求取当前哪些列可以放置皇后 long pos = upperlim & ~(row | ld | rd); while (pos) // 0 -- 皇后没有地方可放,回溯 { // 拷贝pos最右边为1的bit,其余bit置0 // 也就是取得可以放皇后的最右边的列 long p = pos & -pos; // 将pos最右边为1的bit清零 // 也就是为获取下一次的最右可用列使用做准备, // 程序将来会回溯到这个位置继续试探 pos -= p; // row + p,将当前列置1,表示记录这次皇后放置的列。 // (ld + p) << 1,标记当前皇后左边相邻的列不允许下一个皇后放置。 // (rd + p) >> 1,标记当前皇后右边相邻的列不允许下一个皇后放置。 // 此处的移位操作实际上是记录对角线上的限制,只是因为问题都化归 // 到一行网格上来解决,所以表示为列的限制就可以了。显然,随着移位 // 在每次选择列之前进行,原来N×N网格中某个已放置的皇后针对其对角线 // 上产生的限制都被记录下来了 test(row + p, (ld + p) << 1, (rd + p) >> 1); } } else { // row的所有位都为1,即找到了一个成功的布局,回溯 sum++; } } int main(int argc, char *argv[]) { time_t tm; int n = 16; if (argc != 1) n = atoi(argv[1]); tm = time(0); // 因为整型数的限制,最大只能32位, // 如果想处理N大于32的皇后问题,需要 // 用bitset数据结构进行存储 if ((n < 1) || (n > 32)) { printf(" 只能计算1-32之间\n"); exit(-1); } printf("%d 皇后\n", n); // N个皇后只需N位存储,N列中某列有皇后则对应bit置1。 upperlim = (upperlim << n) - 1; test(0, 0, 0); printf("共有%ld种排列, 计算时间%d秒 \n", sum, (int) (time(0) - tm)); system("pause"); return 0; }
//该题目的实现代码
//直接分析了大神写的http://www.cnblogs.com/AbandonZHANG/archive/2012/07/19/2598497.html
/* ID: ****** LANG: C++ TASK: checker */ #include<fstream> #include<iostream> #include<cmath> #include<cstring> using namespace std; ifstream fin("checker.in"); ofstream fout("checker.out"); int num,upperlim; int N; int q[20]; //记录每行第几个落子,即结果集的存储 void test(int row,int ld,int rd,int n)//此处比上面的标准算法多添加了int n ,便于记录结果 { int p,pos; if (row!=upperlim) { pos=upperlim & ~(row|ld|rd); while (pos!=0) { p=pos & -pos; pos=pos-p; q[n]=round(log(p)/log(2))+1; //记录当前行跳棋在哪个位置,路径存储 if (q[n]==0) q[n]=N;//0即N test(row+p,(ld+p)<<1,(rd+p)>>1,n+1); } } else { if (num<3) { for (int i=1;i<n-1;i++) fout<<q[i]<<' '; fout<<q[n-1]<<endl; } num++; } } int main() { fin>>N; upperlim=(1<<N)-1; test(0,0,0,1); fout<<num<<endl; return 0; }