逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::

#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>

using namespace std;

void queen(int** array, int row, FILE* fp);
bool isAvailable(int** array, int y, int x);
bool isResult(int** array);

int main(int arg, char** argv) {
        int** array;
        
        // 分配空间
        array = new int*[8];
        for (int i = 0; i < 8; i++) {
                array[i] = new int[8];
        }
        
        // 初始化皇后状态
        for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                        array[i][j] = 0;
                }
        }
        
        FILE* fp = fopen("data.txt", "w");
        queen(array, 0, fp);
        fclose(fp);
        
        // 释放空间
        for (int i = 0; i < 8; i++) {
                delete[] array[i];
        }
        delete array;
        array = NULL;

        return 0;
}

void queen(int** array, int row, FILE* fp) {
        static int size = 1;
        if (row > 7) {
                return;
        }
        for (int j = 0; j < 8; j++) {
                // 如果合法
                if (isAvailable(array, row, j)) {
                        array[row][j] = 1;
                        
                        // 当前位置合法,赋值后,进入下一行
                        queen(array, row + 1, fp);

                        // 如果退回来后是结果,则打印出来
                        if (isResult(array)) {
                                // 把结果写入文件
                                fputc('\n', fp);
                                fputs("***********************************", fp);
                                fprintf(fp, " %d ", size++);
                                fputs("***********************************", fp);
                                fputc('\n', fp);
                                for (int m = 0; m < 8; m++) {
                                        for (int n = 0; n < 8; n++) {
                                                if (array[m][n] == 1) {
                                                        fputc('#', fp);
                                                        fputc(' ', fp);
                                                        fputc(' ', fp);
                                                } else {
                                                        fputc('O', fp);
                                                        fputc(' ', fp);
                                                        fputc(' ', fp);
                                                }
                                        }
                                        fputc('\n', fp); 
                                }

                        }
                        array[row][j] = 0;

                }
        }
}

bool isAvailable(int** array, int row, int column) { // 第row行,column列
        int i, j;

        // 检查row行
        for (i = 0; i < 8; i++) {
                if (array[row][i] != 0) {
                        return false;
                }
        }

        // 检columnx列
        for (j = 0; j < 8; j++) {
                if (array[j][column] != 0) {
                        return false;
                }
        }

        // 检查对角向左上
        i = row;
        j = column;
        while (i >= 0 && j >= 0) {
                if (array[i][j] != 0) {
                        return false;
                }
                i--;
                j--;
        }

        // 检查对角向右上
        i = row;
        j = column;
        while (i >= 0 && j < 8) {
                if (array[i][j] != 0) {
                        return false;
                }
                i--;
                j++;
        }

        // 检查对角向左下
        i = row;
        j = column;
        while (i < 8 && j >= 0) {
                if (array[i][j] != 0) {
                        return false;
                }
                i++;
                j--;
        }

        // 检查对角向右下
        i = row;
        j = column;
        while (i < 8 && j < 8) {
                if (array[i][j] != 0) {
                        return false;
                }
                i++;
                j++;
        }

        return true;
}

bool isResult(int** array) {
        int size = 0; // 皇后个数

        // 统计皇后个数
        for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                        if (array[i][j] != 0) {
                                size++;
                        }
                }
        }

        // 没有8个皇后,说明不是结果

       // 有8个皇后,因为每个皇后的放置都是在合法位置, 所以只需要简单的计算皇后个数就知道是不是结果.
        if (size != 8) {
                return false;
        }

        return true;
}

posted on 2010-12-17 17:51 逛奔的蜗牛 阅读(391) 评论(0)  编辑 收藏 引用 所属分类: C/C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理