TZOJ 3522 Checker Challenge(深搜)

时间:2019-10-11
本文章向大家介绍TZOJ 3522 Checker Challenge(深搜),主要包括TZOJ 3522 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 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.

输入

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

题意

N*N的棋盘填棋,要求每两个棋子不在同一行同一列同一斜,按字典序输出前3种,再输出总数。

题解

经典爆搜,跟八皇后有点类似,由于13比较慢,可以把表存下来。

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int n,h[15],l[15],out,sum;
 5 int biao[]={4,40,92,352,724,2680,14200,73712};
 6 bool check(int p,int j)
 7 {
 8     for(int i=p-1;i>=1;i--)
 9         if(abs(j-h[i])==p-i)
10             return 0;
11     return 1;
12 }
13 void dfs(int p)
14 {
15     if(out>3)return;
16     if(p==n+1)
17     {
18         sum++;
19         if(++out<=3)
20         {
21             printf("%d",h[1]);
22             for(int i=2;i<p;i++)
23                 printf(" %d",h[i]);
24             printf("\n");
25         }
26         return;
27     }
28     for(int i=1;i<=n;i++)
29     {
30         if(!l[i]&&check(p,i))
31         {
32             l[i]=1;
33             h[p]=i;
34             dfs(p+1);
35             h[p]=0;
36             l[i]=0;
37         }
38     }
39 }
40 int main()
41 {
42     scanf("%d",&n);
43     dfs(1);
44     printf("%d",biao[n-6]);
45     return 0;
46 }
47 /*
48 2 4 6 1 3 5
49 3 6 2 5 1 4
50 4 1 5 2 6 3
51 4
52 1 3 5 7 2 4 6
53 1 4 7 3 6 2 5
54 1 5 2 6 3 7 4
55 40
56 1 5 8 6 3 7 2 4
57 1 6 8 3 7 4 2 5
58 1 7 4 6 8 2 5 3
59 92
60 1 3 6 8 2 4 9 7 5
61 1 3 7 2 8 5 9 4 6
62 1 3 8 6 9 2 5 7 4
63 352
64 1 3 6 8 10 5 9 2 4 7
65 1 3 6 9 7 10 4 2 5 8
66 1 3 6 9 7 10 4 2 8 5
67 724
68 1 3 5 7 9 11 2 4 6 8 10
69 1 3 6 9 2 8 11 4 7 5 10
70 1 3 7 9 4 2 10 6 11 5 8
71 2680
72 1 3 5 8 10 12 6 11 2 7 9 4
73 1 3 5 10 8 11 2 12 6 9 7 4
74 1 3 5 10 8 11 2 12 7 9 4 6
75 14200
76 1 3 5 2 9 12 10 13 4 6 8 11 7
77 1 3 5 7 9 11 13 2 4 6 8 10 12
78 1 3 5 7 12 10 13 6 4 2 8 11 9
79 73712
80 */

原文地址:https://www.cnblogs.com/taozi1115402474/p/11652720.html