C 八皇后

八皇后是一道非常具典型性的题目。

它的基本要求是这种:在一个8*8的矩阵上面放置8个物体,一个矩阵点仅仅同意放置一个物体。随意两个点不能在一行上,也不能在一列上,不能在一条左斜线上,当然也不能在一条右斜线上。
初看到这道题目。大家的第一印象是遍历,可是经过实践之后发现遍历事实上不好写,并且复杂度非常低。不仅须要遍历8*8*8*8*8*8*8*8*8 = 2^24次数据,还要推断各种条件,实际的计算复杂度还要比較这个高。

事实上我们细致看一看。这中间非常多的计算事实上非常多是不须要的,由于假设我们在某一行没有能够插入的数据的话,那么这后面的行事实上就不用考虑了。也就是说,我们仅仅有在保证前面 插入的物体都合法有效的情况下。才干进行下一次的物体插入。无谓的遍历仅仅会是无用功。


那么。我们应该怎么做呢?事实上步骤不太难:

(1)在第n行寻找能够插入的位置。中间涉及到位置合法性的推断
(2)假设没有能够插入的位置,返回
(3)假设有能够插入的位置, 插入数据。此时再推断是否已经是最后一                  行,假设是,打印输出返回;反之继续对下一行数据进行试探处理。

有了上面的步骤,我们就能够书写代码了。老规矩。朋友们能够自己先尝试一下。


a)定义全局堆栈和打印函数

static int gEightQueen[8] = {0};  
static int gCount = 0;  

void print()  
{  
    int outer;  
    int inner;  

    for(outer = 0; outer <8; outer ++){  
        for(inner = 0; inner < gEightQueen[outer]; inner ++)  
            printf("* ");  

        printf("# ");  

        for(inner = gEightQueen[outer] + 1; inner < 8; inner ++)  
            printf("* ");  

        printf("
");  
    }  

    printf("=====================================
");  
}  

b)加入位置合法性的函数推断

int check_pos_valid(int loop, int value)  
{  
    int index;  
    int data;  

    for(index = 0; index < loop; index ++){  
        data = gEightQueen[index];  

        if(value == data)  
            return 0;  

        if((index + data) == (loop + value))  
            return 0;  

        if((index - data) == (loop - value))  
            return 0;  
    }  

    return 1;  
}  

c) 八皇后遍历

void eight_queen(int index)  
{  
    int loop;  

    for(loop = 0; loop < 8; loop++){  
        if(check_pos_valid(index, loop)){  
            gEightQueen[index] = loop;  

            if(7 == index){  
                gCount ++, print();  
                gEightQueen[index] = 0;  
                return;  
            }  

            eight_queen(index + 1);  
            gEightQueen[index] = 0;  
        }  
    }  
}  

总结:

(1)迭代递归是编程的难点,须要自己好好实践。看别人写一百遍,不如自己写一遍
(2)递归的时候务必注意函数return的出口
(3)递归函数中语句的顺序不要随意更换
(4)递归函数中注意数据的保存和恢复
(5)递归函数也要验证。能够用程序验证法。也能够用其它函数的结果来验证

以下是完整的代码,大家能够直接保存成queue.cpp。直接编译执行就可以。

能够打印出全部92种情况

#include <iostream>  
using namespace std;  

static int gEightQueen[8] = {0};  
static int gCount = 0;  


void print()  
{  
    int outer;  
    int inner;  

    for(outer = 0; outer <8; outer ++){  
        for(inner = 0; inner < gEightQueen[outer]; inner ++)  
            printf("* ");  

        printf("# ");  

        for(inner = gEightQueen[outer] + 1; inner < 8; inner ++)  
            printf("* ");  

        printf("
");  
    }  

    printf("=====================================
");  
}  

int check_pos_valid(int loop, int value)  
{  
    int index;  
    int data;  

    for(index = 0; index < loop; index ++){  
        data = gEightQueen[index];  

        if(value == data)  
            return 0;  

        if((index + data) == (loop + value))  
            return 0;  

        if((index - data) == (loop - value))  
            return 0;  
    }  

    return 1;  
}  



void eight_queen(int index)  
{  
    int loop;  

    for(loop = 0; loop < 8; loop++){  
        if(check_pos_valid(index, loop)){  
            gEightQueen[index] = loop;  

            if(7 == index){  
                gCount ++, print();  
                gEightQueen[index] = 0;  
                return;  
            }  

            eight_queen(index + 1);  
            gEightQueen[index] = 0;  
        }  
    }  
}  



int main(int argc, char* argv[])  
{  
    eight_queen(0);  
    printf("total = %d
", gCount);  
    return 1;  
}  
原文地址:https://www.cnblogs.com/mthoutai/p/7065519.html