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

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #define N 8
     4 bool matrix[N][N] = {0};
     5 void Print(bool m[N ][N ]){
     6     static int count = 1;
     7     printf("Case %d:
    ", count++);
     8     for (int i = 0; i < N; i++) {
     9         for (int j = 0; j < N; j++)
    10             m[i][j]==1?printf("Q") : printf(".");
    11         printf("
    ");
    12     }
    13     printf("
    ");
    14 }
    15 bool Safety(bool m[N][N], int row, int col)
    16 {
    17     for (int i = 0; i < row ; i++)
    18     {
    19          for (int j = 0; j < N; j++)
    20                    if(m[i][j]==1&&(j==col||abs(row-i)==abs(col-j)))
    21                 return false;
    22     }
    23     return true;
    24 }
    25 void BackTrack(const int i) {
    26     if (i >=N)
    27         Print(matrix);
    28     else
    29         for (int j = 0; j < N; ++j) {
    30             matrix[i][j] = 1;
    31             if(Safety(matrix,i,j))
    32                          BackTrack(i + 1);
    33             matrix[i][j] = 0;
    34         }
    35 }
    36 int main(void)
    37 {
    38     BackTrack(0);
    39     return 0;
    40 }
  • 相关阅读:
    HTML框架
    HTML链接
    kzalloc 函数详解(转载)
    LCD接口(转载)
    S3C2440上RTC时钟驱动开发实例讲解(转载)
    PHP 真值与空值
    http chunked 理解
    c# 基础
    美式音标注意事项
    groovy 闭包
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6576098.html
Copyright © 2011-2022 走看看