• OJ1700 八皇后问题 基本搜索算法


    基本算法之搜索
    总时间限制: 
    10000ms
     
    内存限制: 
    65536kB
    描述
    在国际象棋棋盘上放置八个皇后,要求每两个皇后之间不能直接吃掉对方。
    输入
    无输入。
    输出
    按给定顺序和格式输出所有八皇后问题的解(见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 
    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    #define up(i,l,r) for(int i=l;i<=r;++i)
    const int ma=1000;
    int tot=1;
    int a[ma],b[ma],w[ma],m[ma];
    int print()
    {
        cout<<"No. "<<tot++<<endl;
        up(j,1,8){
            up(i,1,8){
                if(j==a[i])cout<<"1 ";
                else cout<<"0 ";
            }
            cout<<endl;
        }
        
    }
    int search(int j)
    {
        for(int i=1;i<=8;++i){
            if(b[i]==0&&m[i+j]==0&&w[i-j+7]==0){
                a[j]=i;b[i]=1;
                w[i-j+7]=1;m[i+j]=1;
                if(j==8)print();
                else
                search(j+1);
                b[i]=0;
                w[i-j+7]=0;m[i+j]=0;
            }
        }
    }
    int main()
    {
        search(1);
        return 0; 
    }


  • 相关阅读:
    Mysql基本数据操作
    Mysql安装及自动化部署脚本方案
    CSS应用内容补充及小实例
    CSS详解
    python 装饰器
    HTML基础
    python 异常处理
    python生成器及迭代器
    python模块(二)
    python字符串格式化
  • 原文地址:https://www.cnblogs.com/m2364532/p/12319735.html
Copyright © 2020-2023  润新知