zoukankan      html  css  js  c++  java
  • 第一个八皇后

    #include<iostream>
    #include<stdio.h>
    #define LOCAL
    using namespace std;
    int m[10][10]={0};
    int num=0;
    int check(int row,int column)//检查当前状态下能否放置
    {
        int i,j;
        if(row==1) return 1;
        for(i=1;i<row;i++)//对同一列的棋盘进行检查
            if(m[i][column]==1) return 0;
        i=row-1;
        j=column-1;
        while(i>=1&&j>=1)对左上到当前点进行检查
        {
            if(m[i][j]==1) return 0;
            i--;j--;
        }
        i=row-1;
        j=column+1;
        while(i>=1&&j<=8)//对当前点到又上进行检查
        {
            if(m[i][j]==1) return 0;
            i--;
            j++;
        }
        return 1;//成功
    }
    void output()//输出
    {
        int i,j;
        num++;
        cout<<"the NO."<<num<<endl;
        for(i=1;i<=8;i++)
        {
            for(j=1;j<=8;j++) cout<<m[i][j]<<" ";
            cout<<endl;
        }
        cout<<endl;
    }
    int place(int row)
    {
        int j;
        for(j=1;j<=8;j++)//对当前row行的1-8进行尝试
        {
            m[row][j]=1;
            if(check(row,j)==1)//如果成功
            {
                if(row==8) output();//增加num,输出结果
                else place(row+1);//否则进入到下一行的搜索
            }
            m[row][j]=0;失败则的回溯到上一row行
        }
    }
    int main()
    {
        #ifdef LOCAL
          freopen("data.out","w",stdout);
        #endif // LOCAL
        place(1);
        cout<<num<<endl;
        return 0;
    }

  • 相关阅读:
    数组用法
    前端,面试常见问题总结
    webAPP如何实现移动端拍照上传(Vue组件示例)?
    某某某家前端面试
    腾讯地图前端面试经验
    京东2017校招前端主观题汇总
    计算机领域相关期刊会议及排名
    深度学习入门一周,我都做了些什么
    windows7 64位安装tensorflow 1.4.0 CPU版本
    ThreeJS的特效合成器和后期处理通道
  • 原文地址:https://www.cnblogs.com/acalvin/p/3480799.html
Copyright © 2011-2022 走看看