题目描述: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 ;
}

最新文章

  1. 总结C#保留小数位数及百分号处理
  2. mysql之替换字符串
  3. IL查看override
  4. 创投女王徐新:如何迅速做到细分市场第一(FW)
  5. CCScrollView/CCTableView(CCTableViewDelegate CCTableViewDataSource CCTableView-滑动列表-游戏中大量使用 非常重要的一个类)
  6. EasyUI基础入门之Pagination(分页)
  7. 几种server模型
  8. 转:三十二、Java图形化界面设计——布局管理器之CardLayout(卡片布局)
  9. 20M宽带的网速等价于多少?
  10. QSettings保存程序设置
  11. 正则表达式 \b (转)
  12. 句柄C++
  13. from中buttone 和 input type=&quot;button&quot; 区别
  14. 爬虫3 requests基础
  15. BZOJ4543[POI2014]Hotel加强版——长链剖分+树形DP
  16. POI导出Excel(xls、xlsx均可以,也支持图片)——(三)
  17. ZeroTier One
  18. 面试题20:搜索二叉树可能有两个元素发生了交换,如何恢复BST?
  19. DDL为什么不能rollback?
  20. 基于spec评论作品

热门文章

  1. 浅谈Windows入侵检查
  2. KALI 2017 X64安装到U盘
  3. java中常用的数据结构--Map
  4. _CrtIsValidHeapPointer(pUserData)
  5. DeepLearning算法文章
  6. Linux命令:cp命令
  7. 1-6SpringBoot之事务管理@Transactional
  8. springboot中文官方文档
  9. vue-router 一个十分简单的应用场景
  10. SpringIOC初始化过程源码跟踪及学习