8皇后问题续
来源:互联网 发布:江西淘宝大学电话 编辑:程序博客网 时间:2024/06/07 11:12
接着上篇文章,探讨下八皇后问题:
下面是修改后的源码,参考了网友的答案:
#include<stdio.h>#define Bool int#define True 1#define False 0static count=0;void print(int gEightQueen[])//根据gEightQueen[]中的值,打印出皇后图形{int outer;int inner;for(outer = 0; outer <8; outer ++){for(inner = 0; inner < gEightQueen[outer]; inner ++)printf("0 ");printf("1 ");for(inner = gEightQueen[outer] + 1; inner < 8; inner ++)printf("0 ");printf("\n");}printf("=====================================\n");}Bool IsSafe(int col,int row,int queenList[]){int tempCol;int tempRow;//只检查前面的列for (tempCol = 0; tempCol < col; tempCol++){tempRow = queenList[tempCol];if (tempRow == row){//同一行return False;}if (tempCol == col){//同一列return False;}if (tempRow - tempCol == row - col || tempRow + tempCol == row + col){return False;}}return True;}void eight_queen(int col, int queenList[]){int row;for(row = 0; row < 8; row++){if(IsSafe(col, row, queenList)){queenList[col] = row;if(7 == col){count ++, print(queenList);//queenList[col] = 0;return;}eight_queen(col + 1, queenList);//queenList[col] = 0;}}}int main(){int queenList[8];eight_queen(0,queenList);printf("%d ",count);}
- 8皇后问题续
- 8皇后问题和N皇后问题
- 8皇后问题改进~
- 8皇后问题
- 8皇后问题求解
- 8皇后问题
- 8皇后问题
- 8皇后问题算法
- 8皇后问题
- 8皇后问题
- 8皇后问题
- 8皇后问题
- 8皇后问题
- 8皇后问题
- 8皇后问题
- 【算法】8皇后问题
- 8皇后问题算法
- 8皇后问题
- JavaScript Memory Leak Detector
- cocos2d-x demo资源
- 半世流璃
- hdu 1164 试除法对整数分解
- 铁道部12306后台技术框架、完整异常栈信息以及技术缺点和漏洞
- 8皇后问题续
- JAVA--第五周实验--任务6--封装一类矩阵对象,完成矩阵中数据的位置变换、加法、乘法的功能
- android ANR的一些解决方案
- JAVASE学习笔记一
- source insight添加修改文件属性命令
- 输入三个整数x,y,z,请把这三个数由小到大输出 C/C++
- 周立功谈嵌入式:我的25年嵌入式生涯
- 给定能随机生成整数1到5的函数,写出能随机生成整数1到7的函数
- Android基础 为listView增加更多按钮(分页显示)