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皇后是坠吼的!

【冷漠脸】

比以前加了个二进制优化

 /*
LANG: C++
PROG: checker
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define siji(i,x,y) for(int i=(x) ; i <= (y) ; ++i)
#define xiaosiji(i,x,y) for(int i = (x) ; i < (y); ++i)
#define gongzi(j,x,y) for(int j = (x) ; j >= (y) ; --j)
#define ivorysi
#define mo 11447
#define eps 1e-8
#define o(x) ((x)*(x))
using namespace std;
typedef long long ll;
int LeftDiagonal,RightDiagonal,Column;
int rec[];
int n,ans,cnt;
void Print() {
siji(i,,n) {
printf("%d%c",rec[i]," \n"[i==n]);
}
}
void dfs(int k) {
if(k>n) {
++ans;
if(cnt<) {++cnt;Print();}
}
siji(i,,n){
if((LeftDiagonal>>(k+i)&)== && (RightDiagonal>>(k+n-i+)&)== && (Column>>i&)== ){
//&的优先级比==低??
rec[k]=i;
LeftDiagonal|=(<<(k+i));
RightDiagonal|=(<<(k+n-i+));
Column|=(<<i);
dfs(k+);
LeftDiagonal^=(<<(k+i));
RightDiagonal^=(<<(k+n-i+));
Column^=(<<i);
}
}
}
void solve() {
scanf("%d",&n);
dfs();
printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("checker.in","r",stdin);
freopen("checker.out","w",stdout);
#else
freopen("f1.in","r",stdin);
//freopen("f1.out","w",stdout);
#endif
solve();
return ;
}

最新文章

  1. XlFileFormat Enumeration
  2. Max Points on a Line
  3. squid源码安装下的conf文件默认值和提示
  4. 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)
  5. BetWeen和模糊查询
  6. 一起学习CMake – 02
  7. UpdatePanel和jQuery不兼容
  8. 一年java工作经验的面试题总结(持续更新中)
  9. [Kaggle] dogs-vs-cats之建立模型
  10. 基于IIS的WCF
  11. 使用SIGALARM为recvfrom设置超时
  12. 使用javascript和css模拟帧动画的几种方法浅析
  13. Java 控制类的引用类型,合理使用内存
  14. linux学习笔记之 basename, dirname
  15. js设计模式(六)---组合模式
  16. scanf的一个问题(暂未解决)
  17. JavaBase
  18. P2657 [SCOI2009]windy数
  19. [HNOI2008]玩具装箱
  20. 零基础学习Python数据分析

热门文章

  1. Hadoop生态圈-Hive的自定义函数之UDAF(User-Defined Aggregation Function)
  2. Java基础-Calendar类常用方法介绍
  3. openstack组件的数据库操作
  4. bzoj千题计划123:bzoj1027: [JSOI2007]合金
  5. 容斥 或者 单调栈 hihocoder #1476 : 矩形计数 和 G. Snake Rana 2017 ACM Arabella Collegiate Programming Contest
  6. 服务器能ping通ip,通不了域名解决方案
  7. [洛谷P3158] [CQOI2011]放棋子
  8. Machine Learning Trick of the Day (1): Replica Trick
  9. element-UI 表单图片判空验证问题
  10. sklearn_模型遍历