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皇后是坠吼的!
【冷漠脸】
比以前加了个二进制优化
/*
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 ;
}
USACO 6.5 Checker Challenge的更多相关文章
- USACO training course Checker Challenge N皇后 /// oj10125
...就是N皇后 输出前三种可能排序 输出所有可能排序的方法数 vis[0][i]为i点是否已用 vis[1][m+i]为i点副对角线是否已用 m+i 为从左至右第 m+i 条副对角线 vis[1] ...
- 『嗨威说』算法设计与分析 - 回溯法思想小结(USACO-cha1-sec1.5 Checker Challenge 八皇后升级版)
本文索引目录: 一.回溯算法的基本思想以及个人理解 二.“子集和”问题的解空间结构和约束函数 三.一道经典回溯法题点拨升华回溯法思想 四.结对编程情况 一.回溯算法的基本思想以及个人理解: 1.1 基 ...
- USACO1.5 Checker Challenge(类n皇后问题)
B - B Time Limit:1000MS Memory Limit:16000KB 64bit IO Format:%lld & %llu Description E ...
- TZOJ 3522 Checker Challenge(深搜)
描述 Examine the 6x6 checkerboard below and note that the six checkers are arranged on the board so th ...
- USACO 1.5.4 Checker Challenge跳棋的挑战(回溯法求解N皇后问题+八皇后问题说明)
Description 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子. 列号 0 1 2 3 4 5 6 ...
- Checker Challenge跳棋的挑战(n皇后问题)
Description 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子. 列号 0 1 2 3 4 5 6 ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- ACM-Checker Challenge
题目描述:Checker Challenge 1000(ms) 10000(kb) 20 / 90 Examine the 6x6 checkerboard below and note tha ...
- N皇后问题2
Description Examine the checkerboard below and note that the six checkers are arranged on the board ...
随机推荐
- json转java对象
用了平台之后很少再接触到java和js的底层代码,前几天远程帮一个萌新远程调试代码,这个萌新按照网上的教程去将json字符转java对象却一直报错.真相是它的json字符串格式不对,他的明明是一个数组 ...
- np.argsort函数
np.argsort函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me numpy.argsort(a, axis=-1, kind='quicksort', order=None) 功能: ...
- 如何在Mongodb中实现数据超时自动删除功能?
在工作过程中,我们难免会遇到这样的问题,我们想保存一些数据,但是我们对这些数据的要求并不高,有时候往往只是想要某个时间范围内的数据,比如我们如果永远只关心从当前时间往前推半年内的数据特性,那么我们就不 ...
- C语言第九节进制
进制 什么是进制 是一种计数的方式,数值的表示形式 数一下方块的个数 汉字:十一 十进制:11 二进制:1011 八进制:13 多种进制:十进制.二进制.八进制.十六进制.也就是说,同一个整数,我们至 ...
- 动态规划:树形DP-景点中心(树的带权重心)
话说宁波市的中小学生在镇海中学参加计算机程序设计比赛,比赛之余,他们在镇海中学的各个景点参观.镇海中学共有n个景点,每个景点均有若干学生正在参 观.这n个景点以自然数1至n编号,每两个景点的编号均不同 ...
- Git之原有基础开发新功能
场景描述 当一个项目已经上线,同时又在原有基础上新增功能模块,于是乎就要在原有代码的基础上进行开发,在新增模块功能的开发的过程中,项目发现了一个紧急Bug,需要修复.操作流程如下:
- MongoDB - MongoDB CRUD Operations, Delete Documents
Delete Methods MongoDB provides the following methods to delete documents of a collection: Method De ...
- jQuery技巧笔记
1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...
- asp.net中GridView传多个值到其它页面的方法
网站开发中,在页面之间的跳转,经常会用到传值,其中可能会传递多个值. 一.CommadArgument传多个值到其他页面. 像Gridview dataList repeater等数据绑定控件中,可以 ...
- 【Foreign】tty的方程math [数学]
tty的方程math Time Limit: 50 Sec Memory Limit: 128 MB Description 给定n.m.k.p. a+b+c=n, a^2+b^2+c^2=m, a ...