USACO 6.5 Checker Challenge

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:

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.

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

HINTS (use them carefully!)

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 }
时间: 2024-10-10 03:19:27

USACO 6.5 Checker Challenge的相关文章

Checker Challenge

题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=60489 题意: 该题即n皇后问题.给定n行n列,摆放n皇后,使得它们互不攻击.每个皇后攻击范围为同行同列和对角线. 输入:单行输入n,表示给定n*n的棋盘,摆放n个皇后. 输出:若n大于3,则前三行输出找出的前三个方案(每个方案占一行),第四行输出方案数,否则(n<3)输出所有方案后(每个方案占一行),单行输出方案数.案例: Sample Input 6 S

在皇后问题的基础上输出棋子状态

Examine the $6\times 6$ 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 nor

【USACO】checker

一看题目 经典的8皇后问题 不过是皇后数量可变而已 不用想 回溯法. 需要个生成每次可选择序列的函数, 在存储可选择的序列时按照先大后小的顺序排的.这样每次找最小和去掉最小都很方便,只要有个记录数量的变量 每次减1就好了.  写完后,居然悲剧了. 在皇后数量达到13时, 在自己电脑上跑 内存溢出了 在评分系统上超时了.需要优化. #include <stdio.h> //k计算第几层从0开始 x已经摆好的位置 S存放产生的位置 l存放产生的数量 N一共有多少位置可以选择 int calcula

[USACO 6.5.5]Checker Challerge

题目大意 n皇后问题 题解 轻微的剪枝就过了. HINT4不会写... 代码 /* TASK:checker LANG:C++ */ #include <cstdio> #include <cstring> using namespace std; bool col[15], zd[30], fd[30]; int prow[15]; int ans, n; void dfs(int row) { if (row == n) { ans++; if (ans <= 3) {

算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发.   一.UVaOJ http://uva.onlinejudge.org  西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ.   二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html   "AO

(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html “AOAPC I”是刘汝佳(大

COGS 696. [IOI1996][USACO 2.3] 最长前缀

★   输入文件:prefix.in   输出文件:prefix.out   简单对比时间限制:1 s   内存限制:128 MB 描述 USACO 2.3.1 IOI96 在生物学中,一些生物的结构是用包含其要素的大写字母序列来表示的.生物学家对于把长的序列分解成较短的序列(即元素)很感兴趣. 如果一个集合 P 中的元素可以通过串联(元素可以重复使用,相当于 Pascal 中的 “+” 运算符)组成一个序列 S ,那么我们认为序列 S 可以分解为 P 中的元素.元素不一定要全部出现(如下例中B

USACO prefix TrieTree + DP

/* ID:kevin_s1 PROG:prefix LANG:C++ */ #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <map> #include <set> #include <algorithm> #include <cstdlib>

【USACO 1.3.4】牛式

[題目描述 ] 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ---------- * * * * * * ---------- * * * * 数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0. 注意一下在美国的学校中教的"部分乘积",第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积. 写一个程序找出所有的牛式. [格式] INPUT FORMAT: (f