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

    #include <stdio.h>
    #include <stdlib.h>
    int count=0;
    int a[8];
    bool issafe(int x,int y){//x为当前摆放的棋子的横坐标,y为当前摆放的棋子的纵坐标
        bool ret = true;
        for(int i=0;i<x;i++){//这里的i就是摆放好的棋子的位置
            if(x==i||a[i]==y||x-i==y-a[i]||x-i==-y+a[i]){
                ret = false;
                /*break;*/
            }
        }
        return ret;
    }
    
    //
    void dfs(int step){
        if(step==8){
            count++;//统计一共有多少种解法
            return;
        }
        for(int i =0;i<8;i++){
            if(issafe(step,i)){
                    a[step] = i;
                    dfs(step+1);
            }
        }
    
    }
    
    
    int main(){
        dfs(0);
        printf("%d
    ",count);
        /*for(int j=0;j<8;j++){
            printf("%d
    ",a[j]);
        }*/
        system("pause");
    }

    迭代变量的选取:

    能够唯一描述当前该问题的状态变量

    该问题中有两个对应的变量:1.当前摆放到第几行2.摆放的位置

    大多数想法要么平庸,要么更糟糕,这很大程度上因为绝妙的想法难得一见,而且他们还要在我们身边这个充斥了各种恶俗的所谓常识的环境中孕育生长。
  • 相关阅读:
    DZY Loves Sequences
    Boredom easy dp
    floyd算法 poj1125
    poj3259 Bellman_Ford算法
    poj1860 Bellman_Ford算法
    Java 获取资源文件路径
    CLion 2020.1.2 激活
    Kotlin学习笔记
    Kotlin Hello World
    WebStorm 2020.1.2 激活
  • 原文地址:https://www.cnblogs.com/linux0537/p/6144926.html
Copyright © 2011-2022 走看看