题目描述:Checker Challenge
 1000(ms)
 10000(kb)
 20 / 90

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 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.

输入

A single line that contains a single integer N (6 <= N <= 13) that is the dimension of the N x N checkerboard.

输出

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.

样例输入

6

样例输出

2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4
提 交

gcc
 
 
 
 
 
1
 
 

解题思路:N皇后问题,一般的DFS会超时,用位运算才可以A。

////// Checker Challenge.cpp : 定义控制台应用程序的入口点。
//////
////
#include "stdafx.h"
////
////
////#include <stdio.h>
////#include <math.h>
////#include <string.h>
////
////int n, ans;
////int res[15],save[15];
////
////struct R
////{
//// int r[15][15];
////
////}R[15];
////
////void DFS(int i)
////{
//// if (i == n)
//// {
//// ans++;
//// if (ans <= 3)
//// {
//// memcpy(R[n].r[ans-1], res, sizeof(res));
//// }
//// return;
//// }
////
//// for (int k = 0; k < n; k++)//遍历所有列
//// {
//// int j;
//// for (j = 0; j < i; j++)//和已经放好的皇后位置比较
//// {
//// if (k == res[j]) break;
//// if (abs(k - res[j]) == i - j) break;
//// }
//// if (j >= i)
//// {
//// res[i] = k;
//// DFS(i + 1);
//// }
//// }
////}
////
////int main()
////{
//// for (n = 0; n <= 13; n++)
//// {
//// ans = 0;
//// DFS(0);
//// save[n] = ans;
////
//// }
//// while (scanf("%d",&n)!=EOF)
//// {
////
//// for (int j = 0; j < 3; j++)
//// {
//// for (int i = 0; i < n; i++)
//// {
//// if (i != n-1) printf("%d ", R[n].r[j][i] + 1);
////
//// else printf("%d\n", R[n].r[j][i] + 1);
//// }
//// }
//// printf("%d\n", save[n]);
////
//// }
////} //简化的思想:
//用位运算代替for循环遍历每个皇后的位置
//用垂直、对角线、斜对角线的位运算 /*整个逻辑:
//1.求将要放入皇后的位置
//2.更新可放皇后的位置
//3.DFS循环*/ #include <stdio.h> int n,ans,uplimit,res[]; int binary2(int num)
{
int ans = ;
while (num)
{
num = num >> ;
ans++;
}
return ans;
} void DFS(int i,int vertical, int diagonal, int antidiagonal)
{
if (i >= n)
{
ans++;
if (ans <=)
{
//输出结果
for (int i = ; i < n; i++)
{
if (i != n-) printf("%d ", res[i]);
else printf("%d\n", res[i]);
}
}
return;
}
int avail = uplimit & (~(vertical | diagonal | antidiagonal));
while (avail)
{
int pos = avail & (-avail);
avail = avail - pos;
res[i] = binary2(pos);
DFS(i + ,vertical + pos, (diagonal + pos) << , (antidiagonal + pos) >> );
} } int main()
{
while (scanf("%d", &n) != EOF)
{
ans = ;
uplimit = ( << n) - ;
DFS(, , , );
printf("%d\n", ans);
}
return ;
}

ACM-Checker Challenge的更多相关文章

  1. USACO1.5 Checker Challenge(类n皇后问题)

    B - B Time Limit:1000MS     Memory Limit:16000KB     64bit IO Format:%lld & %llu   Description E ...

  2. USACO 6.5 Checker Challenge

    Checker Challenge Examine the 6x6 checkerboard below and note that the six checkers are arranged on ...

  3. 『嗨威说』算法设计与分析 - 回溯法思想小结(USACO-cha1-sec1.5 Checker Challenge 八皇后升级版)

    本文索引目录: 一.回溯算法的基本思想以及个人理解 二.“子集和”问题的解空间结构和约束函数 三.一道经典回溯法题点拨升华回溯法思想 四.结对编程情况 一.回溯算法的基本思想以及个人理解: 1.1 基 ...

  4. TZOJ 3522 Checker Challenge(深搜)

    描述 Examine the 6x6 checkerboard below and note that the six checkers are arranged on the board so th ...

  5. USACO 1.5.4 Checker Challenge跳棋的挑战(回溯法求解N皇后问题+八皇后问题说明)

    Description 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子. 列号 0 1 2 3 4 5 6 ...

  6. Checker Challenge跳棋的挑战(n皇后问题)

    Description 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子. 列号 0 1 2 3 4 5 6 ...

  7. USACO training course Checker Challenge N皇后 /// oj10125

    ...就是N皇后 输出前三种可能排序 输出所有可能排序的方法数 vis[0][i]为i点是否已用 vis[1][m+i]为i点副对角线是否已用  m+i 为从左至右第 m+i 条副对角线 vis[1] ...

  8. N皇后问题2

    Description Examine the  checkerboard below and note that the six checkers are arranged on the board ...

  9. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

随机推荐

  1. Memcached 最新版本发布,不再仅仅是个内存缓存了

    导读 Memcached 1.5.18和之后版本可以在服务重启时恢复内存缓存.新版本还通过DAX文件系统挂载来实现缓存持久性功能. 可以通过在启动选项使用该功能: -e /tmpfs_mount/me ...

  2. Fiddler抓取HTTPS

    对于想抓取HTTPS的测试初学者来说,常用的工具就是fiddler.可是在初学时,大家对于fiddler如何抓取HTTPS真是伤了脑筋,可能你一步步按着网上的帖子成功了,那当然是极好的. 有可能没有成 ...

  3. yarn container 的日志路径

    /etc/hadoop/conf/yarn-site.xml 配置文件中 - yarn.nodemanager.log-dirs 指定本机的日志路径 (/hadoopfs/fs1/yarn/nodem ...

  4. 阿里RocketMq试用记录+简单的Spring集成

    CSDN学院招募微信小程序讲师啦 程序猿全指南,让[移动开发]更简单! [观点]移动原生App开发 PK HTML 5开发 云端应用征文大赛,秀绝招,赢无人机! 阿里RocketMq试用记录+简单的S ...

  5. 2. 引用计数法(Reference Counting)

    1960年,George E. Collins 在论文中发布了引用计数的GC算法. 引用计数法意如了一个概念,那就是"计数器",计数器表示的是对象的人气指数, 也就是有多少程序引用 ...

  6. python2中新式类和经典类的多重继承调用顺序

    class A: def foo(self): print('called A.foo()') class B(A): pass class C(A): def foo(self): print('c ...

  7. ubuntu18.04窗口截图和选区截图快捷键

    解决方法: 1.点击左下角的系统设置. 2.点击设备. 3.点击键盘,可查看各种截图操作的快捷键.  PS:双击图中的快捷键可以设置新的快捷键.

  8. SQL state [72000]; error code [1461]; ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值 ; nested exception is java.sql.BatchUpdateException: ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值

    本文转自  https://www.cnblogs.com/yingsong/p/5685790.html 原 因:某一个字段本为varchar2(1024),但是实际要插入的值超过varchar2允 ...

  9. Redis——进阶

    redis的持久化 redis提供两种备份方式,一种是RDB 一种是AOFRDB默认开启.关闭注释掉所有的save,存储的是redis 具体的值,会压缩存储.AOF配置文件中appendonly ye ...

  10. Java提升三:函数式接口

    1. 定义 函数式接口即是有且仅有一个抽象方法的接口. 注意: (1)函数式接口只对于抽象方法有要求,对于接口中的默认方法,静态方法,私有方法数量并不作特殊要求. (2)既然函数式接口定义了抽象方法, ...