USACO-cha1-sec1.5 Checker Challenge

来源:互联网 发布:三个矩阵分配律 编辑:程序博客网 时间:2024/05/23 14:35

Checker Challenge

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:

ROW123456COLUMN246135

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 53 6 2 5 1 44 1 5 2 6 34

————————————————————愉快的分割线————————————————————

前言:不许作弊,不许打表,只能认真做。位运算最最保险。

思路:要求输出前三个解的路径,这对dfs来说不太好办。不管怎么说,先把位运算N皇后写出来。后来开一个数组,在找到了最右边可以放置皇后的地方之后,dfs之前,对前三个解进行记录。因为用一个int数row来表示行的状态,那么根据row当中1的数量可以得知这是第几行。输出之后,发现从7*7开始,前几个位置竟然出现了0。最右边能放的位置怎么可能是0呢?

仔细思考,发现dfs是罪魁祸首。那几个0其实就应该前一个解的前几个数字一模一样。因为一旦dfs进去之后,会找到这个子结点下所有可行解,所以说,没有机会去记录前几个一样的数字了。如下:

int pos = 合法位置;while(pos) {    int p = 最右的合法位置;    if(解小于3个) {        int index = 行号;        ans[][index] = p对应的列编号;    }    dfs(,,);    pos -= p;}
可以看出,如果前面几个列编号是正确的,那么只能dfs后面的编号,无法回过头来复制前几个编号。输出的时候复制一下就行了。这加深了对dfs的理解!

代码如下:

/*ID: j.sure.1PROG: checkerLANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <string>#include <iostream>using namespace std;/****************************************/int n, ans[3][50], lim, tot;void dfs(int row, int ld, int rd) {    if(row == lim) {//找到一个可行解tot++;return ;    }    int pos = (~(row|ld|rd)) & lim;//取反之后空位置变为1(超出棋盘的0也变成1),再进行与,得到空位置    while(pos) {int p = pos & -pos;//取最右边的1if(tot < 3) {int tmp = row, index = 0;for(int i = 0; i < n; i++) {if(tmp & 1)index++;tmp >>= 1;}//统计1的个数做下标ans[tot][index] = (int)(log2((float)p)) + 1;//根据p的值对应出位置}dfs(row|p, (ld|p)<<1, (rd|p)>>1);//ld、rd代表对角线,因为只关心当前行的合法位置,所以相当于前一行的row进行左移和右移pos -= p;//因为是最右边的1,所以可以直接减去,代表放置了一个皇后    }}int main(){freopen("checker.in", "r", stdin);freopen("checker.out", "w", stdout);scanf("%d", &n);lim = (1 << n) - 1;tot = 0;dfs(0, 0, 0);for(int i = 0; i < (tot < 3 ? tot : 3); i++) {for(int j = 0; j < n-1; j++) {if(ans[i][j] == 0)ans[i][j] = ans[i-1][j];printf("%d ", ans[i][j]);}printf("%d\n", ans[i][n-1]);}    printf("%d\n", tot);fclose(stdin);fclose(stdout);    return 0;}


0 0
原创粉丝点击