zoukankan      html  css  js  c++  java
  • 在皇后问题的基础上输出棋子状态

    Examine the $6 imes 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$ ($6leq Nleq 13$) that is the dimension of the $N imes 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;


     

  • 相关阅读:
    rabbitmq技术的一些感悟(一)
    用python做自己主动化測试--对server端的自己主动化測试(3)-很多其它http client实例
    huffman编码——原理与实现
    MS-SQLSERVER中的MSDTC不可用解决方法
    基础总结篇之中的一个:Activity生命周期
    STL vector使用方法介绍
    Cocos2d-x Render-NewCulling
    C++ Primer笔记9_构造函数_拷贝构造(深拷贝与浅拷贝)
    shell编程基础
    软考复习之路—组成原理
  • 原文地址:https://www.cnblogs.com/Lynn0814/p/4696608.html
Copyright © 2011-2022 走看看