博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第一个八皇后
阅读量:6868 次
发布时间:2019-06-26

本文共 950 字,大约阅读时间需要 3 分钟。

#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;
}

转载于:https://www.cnblogs.com/acalvin/p/3480799.html

你可能感兴趣的文章
Storm与Spark Streaming比较
查看>>
我的友情链接
查看>>
Exchange Server 运维管理01:Exchange中Active Directory 有什么用?
查看>>
dhcp服务在企业中的应用
查看>>
linux系统管理之四:服务状态
查看>>
VMware View FAQ[一]
查看>>
【原创翻译】布尔值(boolean)
查看>>
三元运算式、lambda表达式、内置函数map、reduce、filter以及yield生成器
查看>>
MySQL分库分表分表后数据的查询(5th)
查看>>
iOS-点击图片放大,再次点击返回原视图 类似查看相册的功能
查看>>
JAVA -- stateless4j StateMachine 使用浅析(二)
查看>>
oracle checkpoint
查看>>
centos 6.5 x64bit 快速安装openstack
查看>>
收藏的文章
查看>>
第二章链路层
查看>>
我的友情链接
查看>>
php mb_substr()函数的详细解释!
查看>>
谈谈技术规范的制定
查看>>
ubuntu12.04mtk编译环境安装
查看>>
Haproxy负载均衡比较
查看>>