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

    在国际象棋棋盘上放置八个皇后,要求每两个皇后之间不能直接吃掉对方。Input无输入。Output按给定顺序和格式输出所有八皇后问题的解(见Sample Output)。Sample Input

    
    

    Sample Output

    No. 1
    1 0 0 0 0 0 0 0 
    0 0 0 0 0 0 1 0 
    0 0 0 0 1 0 0 0 
    0 0 0 0 0 0 0 1 
    0 1 0 0 0 0 0 0 
    0 0 0 1 0 0 0 0 
    0 0 0 0 0 1 0 0 
    0 0 1 0 0 0 0 0 
    No. 2
    1 0 0 0 0 0 0 0 
    0 0 0 0 0 0 1 0 
    0 0 0 1 0 0 0 0 
    0 0 0 0 0 1 0 0 
    0 0 0 0 0 0 0 1 
    0 1 0 0 0 0 0 0 
    0 0 0 0 1 0 0 0 
    0 0 1 0 0 0 0 0 
    No. 3
    1 0 0 0 0 0 0 0 
    0 0 0 0 0 1 0 0 
    0 0 0 0 0 0 0 1 
    0 0 1 0 0 0 0 0 
    0 0 0 0 0 0 1 0 
    0 0 0 1 0 0 0 0 
    0 1 0 0 0 0 0 0 
    0 0 0 0 1 0 0 0 
    No. 4
    1 0 0 0 0 0 0 0 
    0 0 0 0 1 0 0 0 
    0 0 0 0 0 0 0 1 
    0 0 0 0 0 1 0 0 
    0 0 1 0 0 0 0 0 
    0 0 0 0 0 0 1 0 
    0 1 0 0 0 0 0 0 
    0 0 0 1 0 0 0 0 
    No. 5
    0 0 0 0 0 1 0 0 
    1 0 0 0 0 0 0 0 
    0 0 0 0 1 0 0 0 
    0 1 0 0 0 0 0 0 
    0 0 0 0 0 0 0 1 
    0 0 1 0 0 0 0 0 
    0 0 0 0 0 0 1 0 
    0 0 0 1 0 0 0 0 
    No. 6
    0 0 0 1 0 0 0 0 
    1 0 0 0 0 0 0 0 
    0 0 0 0 1 0 0 0 
    0 0 0 0 0 0 0 1 
    0 1 0 0 0 0 0 0 
    0 0 0 0 0 0 1 0 
    0 0 1 0 0 0 0 0 
    0 0 0 0 0 1 0 0 
    No. 7
    0 0 0 0 1 0 0 0 
    1 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 1 
    0 0 0 1 0 0 0 0 
    0 1 0 0 0 0 0 0 
    0 0 0 0 0 0 1 0 
    0 0 1 0 0 0 0 0 
    0 0 0 0 0 1 0 0 
    No. 8
    0 0 1 0 0 0 0 0 
    1 0 0 0 0 0 0 0 
    0 0 0 0 0 0 1 0 
    0 0 0 0 1 0 0 0 
    0 0 0 0 0 0 0 1 
    0 1 0 0 0 0 0 0 
    0 0 0 1 0 0 0 0 
    0 0 0 0 0 1 0 0 
    No. 9
    0 0 0 0 1 0 0 0 
    1 0 0 0 0 0 0 0 
    0 0 0 1 0 0 0 0 
    0 0 0 0 0 1 0 0 
    0 0 0 0 0 0 0 1 
    0 1 0 0 0 0 0 0 
    0 0 0 0 0 0 1 0 
    0 0 1 0 0 0 0 0 
    ...以下省略

    Hint此题可使用函数递归调用的方法求解。 

    经典的DFS入门题 核心代码就是一个for循环 代码简单易懂  ,但是不知怎么优化 才能让时间变成0。

     1 #include <iostream>
     2 using namespace std;
     3 #include<string.h>
     4 #include<set>
     5 #include<stdio.h>
     6 #include<math.h>
     7 #include<queue>
     8 #include<map>
     9 #include<algorithm>
    10 #include<cstdio>
    11 #include<cmath>
    12 #include<cstring>
    13 #include <cstdio>
    14 #include <cstdlib>
    15 #include<stack>
    16 int add=0;
    17 int a[20][20];
    18 
    19 int panduan(int x,int y)
    20 {
    21     for(int i=0;i<x;i++)
    22     {
    23         if(a[i][y]==1)
    24             return 0;
    25     }
    26     int y1=y+x;
    27     int x1=0;
    28     for(;x1<=8&&y1>0;x1++,y1--)
    29     {
    30         if(a[x1][y1]==1)
    31             return 0;
    32     }
    33     x+=8;
    34     y+=8;
    35     for(;x>0&&y>0;x--,y--)
    36     {
    37         if(a[x][y]==1)
    38             return 0;
    39     }
    40         return 1;
    41 }
    42 
    43 
    44 void dfs(int x)
    45 {
    46     if(x==9)
    47     {
    48         printf("No. %d
    ",++add);
    49         for(int i=1;i<=8;i++)
    50         {
    51             int flag=0;
    52             for(int j=1;j<=8;j++)
    53             {
    54                 if(!flag)
    55                 {
    56                     flag=1;
    57                     printf("%d",a[j][i]);
    58                 }
    59                 else
    60                 {
    61                     printf(" %d",a[j][i]);
    62                 }
    63             }
    64             printf("
    ");
    65         }
    66         return ;
    67     }
    68     for(int i=1;i<=8;i++)
    69     {
    70         if(panduan(x,i))
    71         {
    72             a[x][i]=1;
    73             dfs(x+1);
    74             a[x][i]=0;
    75         }
    76     }
    77     return ;
    78 }
    79 
    80 int main()
    81 {
    82     memset(a,0,sizeof(a));
    83         dfs(1);
    84     return 0;
    85 }
    View Code
  • 相关阅读:
    关于代码风格
    python笔记-glob模块
    python笔记-mysql安装与配置
    python笔记-shutil模块
    python笔记-os模块
    python笔记-内存分析
    python笔记-装饰器
    python笔记-正则表达式
    python笔记-迭代器-生成器-对象生成式
    python笔记-模块和包
  • 原文地址:https://www.cnblogs.com/dulute/p/7497175.html
Copyright © 2011-2022 走看看