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 ...
随机推荐
- wepy
npm install -g cnpm --registry=https://registry.npm.taobao.org https://blog.csdn.net/qq_40414159/art ...
- Docker查看映射卷报错
问题描述: 当查看Docker容器的映射卷时出现报错信息,如下: [root@kazihuo ~]# docker inspect -f {{.Volumes}} volume #volume指容 ...
- Java获取精确到毫秒的时间戳
import java.util.Date; public class Timestamp { /** 获取精确到毫秒的时间戳 * @param date * @return **/ public s ...
- 打包python脚本为exe的坎坷经历, by pyinstaller方法
打包python脚本为exe的坎坷经历, by pyinstaller方法 又应验了那句歌词. 不经历风雨, 怎么见得了彩虹. 安装过程略去不提, 仅提示: pip install pyinstall ...
- [转载]如何做到 jQuery-free?
http://www.ruanyifeng.com/blog/2013/05/jquery-free.html jQuery是现在最流行的JavaScript工具库. 据统计,目前全世界57.3%的网 ...
- 【转】线程间操作无效: 从不是创建控件“textBox2” 的线程访问它。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 【leetcode 简单】 第八十二题 反转字符串
编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man, a p ...
- HDU 1181 变形课 (深搜)
题目连接 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形 ...
- js面向对象编程思想
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 洛谷 P3307: bzoj 3202: [SDOI2013] 项链
题目传送门:洛谷P3307.这题在bzoj上是权限题. 题意简述: 这题分为两个部分: ① 有一些珠子,每个珠子可以看成一个无序三元组.三元组要满足三个数都在$1$到$m$之间,并且三个数互质,两个珠 ...