#include <stdio.h> #include <cstdlib> int eight_array[8] = {0}; int count = 0; void printf_image(){ printf("\n 第 %d 种方案 \n",++count); for(int i = 0; i < 8; ++i) { int j; for(j = 0; j < eight_array[i]; ++j) { printf("%d ", 0); } printf("%d ", 1); for(int k = ++j; k < 8; ++k){ printf("%d ", 0); } printf("\n"); } printf("\n\n"); } bool enable_place(int row) { for(int k = 0; k < row; ++k) { if(eight_array[k] == eight_array[row] || (row - k)*(row -k) == (eight_array[row] - eight_array[k])*(eight_array[row] - eight_array[k])){ return false; } } return true; } void eight(int row) { if(row > 7){ printf_image(); }else { for(int i = 0; i < 8; ++i) { eight_array[row] = i; if(enable_place(row)){ eight(row+1); } } } } int main() { eight(0); system("pause"); return 0; }
c基础编程:八皇后问题(递归算法)
原文作者:ITMelody
原文地址: https://www.cnblogs.com/ITmelody/archive/2012/04/26/2471275.html
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://www.cnblogs.com/ITmelody/archive/2012/04/26/2471275.html
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。