• 八皇后问题 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
  • 相关阅读:
    win8 app scrollviewer ZoomMode
    win8 metro app 不支持 trigger
    WinRT Convert Stream to BitmapImage
    Java深度历险(二)——Java类的加载、链接和初始化(收藏)
    简单的触发器实现
    Java深度历险(一)——Java字节代码的操纵(收藏)
    借用网上大神的一些知识,html5 video 视频播放都兼容(Android,iOS,电脑)
    还原或删除sql server 2008数据库时,经常烩出现: “因为数据库正在使用,所以无法获得对数据库的独占访问权”,终解决方案
    调研《构建之法》指导下的历届作品
    Hibernate 学习笔记一
  • 原文地址:https://www.cnblogs.com/dulute/p/7497175.html
Copyright © 2020-2023  润新知