在皇后问题的基础上输出棋子状态

Examine the $6\times 6$ 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.)

 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.

Input

A single line that contains a single integer $N$ ($6\leq N\leq 13$) that is the dimension of the $N\times N$ checkerboard.

Output

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 Input

6

Sample Output

2 4 6 1 3 5

3 6 2 5 1 4

4 1 5 2 6 3

4

思路:

跟皇后问题一样,只要加个输出就好!

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 int n, vis[5][40], a[20], tot = 0;
 5 int vis1[20][10][40];
 6 int queue[20];
 7 using namespace std;
 8 void dfs(int cur)
 9 {
10     if (cur == n+1)
11     {
12         tot++;
13         if (tot<4&&tot>0)
14         for (int i = 1; i <= n; i++)
15
16         {
17             vis1[n][tot][i] = a[i];
18
19         }
20     }
21     else for (int i = 1; i<=n; i++)
22         if (!vis[1][i] && !vis[2][cur + i] && !vis[3][cur - i + n])
23         {
24         a[cur] = i;
25         vis[1][i] = vis[2][cur + i] = vis[3][cur - i + n] = 1;
26         dfs(cur + 1);
27         vis[1][i] = vis[2][cur + i] = vis[3][cur - i + n] = 0;
28         }
29 }
30 int main()
31 {
32     memset(queue, 0, sizeof(queue));
33     for (n = 6; n <= 13; n++)
34     {
35
36         tot = 0;
37         dfs(1);
38
39         queue[n] = tot;
40
41     }
42     int q;
43     cin >> q;
44
45         for (int i = 1; i <= 3; i++)
46         {
47             for (int j = 1; j <= q; j++)
48             {
49                 if (j == q)
50                     cout << vis1[q][i][j] << endl;
51                 else
52                     cout << vis1[q][i][j] << " ";
53
54             }
55         }
56             cout << queue[q]<< endl;
57
58     return 0;
时间: 2024-08-24 21:16:08

在皇后问题的基础上输出棋子状态的相关文章

【linux基础】17、TCP/IP网络基础(上)

一.计算机网络概述 1.什么是网络? 一群具有独立功能的计算机组成的系统通过通信设备及传输媒体互连起来,在通信软件支持下,实现资源共享.信息交换的整个系统 产生计算机网络的基本条件:通信技术与计算机技术的结合 2.计算机网络的产生与发展 1954年 主机-终端的通信网络  第一代计算机网络 以单个主机为中心.面向终端设备的网络结构.系统中除主计算机具有独立的数据处理功能外,系统中所连接的终端设备均无独立处理数据的功能 1969年 主机-主机的通信网络  ARPANET网  第二代计算机网络 以分

【JavaEE】SSH+Spring Security基础上配置AOP+log4j

Spring Oauth2大多数情况下还是用不到的,主要使用的还是Spring+SpringMVC+Hibernate,有时候加上SpringSecurity,因此,本文及以后的文章的example中都不会包含oauth2的配置,需要的话把前文的applicationContext-security.xml和pom.xml加上就可以了,本文在“SSH+Spring Security搭建方法及example”一文的基础上做一些调整,主要内容是:配置Spring AOP并且用log4j来记录日志.

Swift基础之OC文件调用Swift代码(在上次的基础上写的)

前两天刚写过Swift调用OC,今天在原来的基础上,实现OC调用Swift. 首先,创建一个OneSwiftFile.swift文件,创建一个继承于NSObject的类(这个地方你可以自己选择继承的父类) 然后在类中创建几个方法,方便在OC文件中使用 class OneSwiftFile:NSObject{    override init() {        NSLog("这是在init方法中...");    }        //创建一个方法    func someFunc(

Java基础——输入/输出(一)

概述 Java的IO通过java.io包下是类和接口来支持,在java.io包下主要包括输入,输出两种IO流,每种输入.输出流又可分为字节流和字符流两大类.其中字节流以字节为单位来处理输入.输出操作,而字符流以字符来处理来处理输入.输出操作. 一.File类 File类可以使用文件路径字符串来创建File实例,该文件字符串可以是绝对路径,也可以是相对路径.在默认情况下,系统总是依据用户的工作路径来解释相对路径,这个路径由系统属性“userdir”指定. 一旦创建了File对象后,就可以调用Fil

在Livemedia的基础上开发自己的流媒体客户端

一.背景 二.Livemedia框架介绍 1.总体框架 2.客户端框架 2.1 客户端openRTSP流程 2.2增加一种新的媒体 2.2.1增加媒体的format 2.2.2 新媒体需要考虑的问题 2.3类详细说明 2.3.1 BasicUsageEnvironment, UsageEnvironment 2.3.2 groupsock 2.3.3 livemedia 三.一些总结 A. Buffer 管理 B. How to control the receive loop C. PAUSE

如何在robotframework基础上使用数据驱动测试

一.写在前面 robotframework是很好用的关键字驱动测试框架,但是在实际工作中也有些地方使用不便,比如在我们设计参数校验测试case时,往往只是想修改校验参数类型而不得不做大量复制粘贴操作,最终导致了一个测试case中存在大量的冗余代码,而且如果在后续的使用中一旦出现变动,测试case将不便维护. 针对此情况,想到了尝试在robotframework功能基础上封装了一个轻型框架,用来实现部分接口测试场景的数据驱动方式的自动化测试. 二.所需组件说明 1.python2.7 2.robo

ffmpeg在android上输出滑屏问题处理

ffmpeg部分机器上有花屏的问题 原代码如下: while(av_read_frame(formatCtx, &packet)>=0 && !_stop && NULL!=window && bInit) { // Is this a packet from the video stream? if(packet.stream_index==videoStream) { // Decode video frame avcodec_decode

【G】开源的分布式部署解决方案(二) - 好项目是从烂项目基础上重构出来的

G.系列导航 [G]开源的分布式部署解决方案 - 预告篇 [G]开源的分布式部署解决方案(一) - 开篇 [G]开源的分布式部署解决方案(二) - 好项目是从烂项目基础上重构出来的 分析目前项目结构 眼前出现这么一坨坨的文件夹,相信很多人已经看不下去了.是的,首先就是要把它给做掉. 按照这个项目文件夹的命名意图,大概可以划分如下: 1.Business:业务代码 2.Data:数据访问 3.Helpers:辅助类(通用类库之类的) 4.Models:各种模型(包括视图模型) 5.theme:皮肤

最佳的项目一定建立在最佳的软件工程基础上的

快速软件开发有几个基本原则:管理原则.技术原则.质量保证原则. 其中和我目前关系较深的是技术原则. 作者在第四章的前段便引用了这句话“最佳的项目一定建立在最佳的软件工程基础上的”. 而我认为我计算机相关的基础都比较薄弱,作者并没有给出软件工程基础相关的推荐书籍. 不知道是否有一些软件工程基础的推荐书籍. 最好是能够相互对照的.