zoukankan      html  css  js  c++  java
  • 八皇后

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<cstdlib>
     6 
     7 using namespace std;
     8 int n;
     9 int tot;
    10 int C[100];
    11 void search(int cur)
    12 {
    13     if(cur == n) 
    14     {
    15         tot++;
    16         printf("%d\n", tot);
    17         for(int i = 0; i < n; i++)
    18             printf("%d, %d\t", i+1 , C[i]);
    19         printf("\n--------------------------------------------------------------------\n");
    20     }
    21     else 
    22         for(int i = 0; i < n; i++)
    23         {
    24             int ok = 1;
    25             C[cur] = i;
    26             for(int j = 0; j < cur; j++)
    27             {
    28                 if(C[cur] == C[j] || cur - C[cur] == j - C[j] || cur + C[cur] == j + C[j])
    29                 {
    30                     ok = 0;
    31                     break;
    32                 }
    33             }
    34             if(ok) 
    35             {
    36                 search(cur + 1);
    37             }
    38         }
    39 }
    40 int main()
    41 {
    42     scanf("%d", &n);
    43     search(0);
    44     printf("\ntot = %d\n", tot);
    45     return 0;
    46 }

    八皇后 92个解

    并且输出了每个解

    以矩阵的形式 输出   可以的位置用X表示  其余位置用O表示

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<cstdlib>
     6 
     7 using namespace std;
     8 int n;
     9 int tot;
    10 int C[100];
    11 void search(int cur)
    12 {
    13     if(cur == n) 
    14     {
    15         tot++;
    16         printf("%d\n", tot);
    17         for(int i = 0; i < n; i++)
    18         {
    19             for(int j = 0; j < n; j++)
    20             {
    21                 if(C[i] == j) printf("X ");
    22                 else printf("O ");
    23             }
    24             printf("\n");
    25         }
    26         //    printf("%d, %d\t", i+1 , C[i] + 1);
    27         printf("----------------\n");
    28     }
    29     else 
    30         for(int i = 0; i < n; i++)
    31         {
    32             int ok = 1;
    33             C[cur] = i;
    34             for(int j = 0; j < cur; j++)
    35             {
    36                 if(C[cur] == C[j] || cur - C[cur] == j - C[j] || cur + C[cur] == j + C[j])
    37                 {
    38                     ok = 0;
    39                     break;
    40                 }
    41             }
    42             if(ok)     search(cur + 1);
    43         }
    44 }
    45 int main()
    46 {
    47     scanf("%d", &n);
    48     search(0);
    49     printf("\ntot = %d\n", tot);
    50     return 0;
    51 }
  • 相关阅读:
    R-CNN/Fast R-CNN/Faster R-CNN
    RNN的介绍
    前向传播算法(Forward propagation)与反向传播算法(Back propagation)
    world转.md
    msgid 属性
    .equals()到底是什么意思?
    iperf详细使用方法
    c语言#define用法
    android源码编译出现No private recovery resources for TARGET_DEVICE解决方法
    nginx File not found 错误
  • 原文地址:https://www.cnblogs.com/ZZZZone/p/6020930.html
Copyright © 2011-2022 走看看