鱼C论坛

 找回密码
 立即注册
查看: 1893|回复: 0

八皇后问题

[复制链接]
发表于 2020-2-11 21:22:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
参考了小甲鱼的代码,虽然有些地方看不懂
这里的left函数不是必须的,只是为了严谨
花了几天时间,想出来了感觉好有成就感


  1. #include <stdio.h>
  2. #define OCUR 'O'         //该位置已有皇后
  3. #define NOTOCUR '*'     //无皇后时数组的填充符号
  4. #define NUM 8         //皇后个数

  5. void Queen(int x,int y,char str[NUM][NUM]);// 求解函数
  6. void print(char str[NUM][NUM]);//打印解函数
  7. int left(int x,int y,char str[NUM][NUM]);//判断(x,y)的左侧是否已有皇后
  8. int top(int x,int y,char str[NUM][NUM]);//判断(x,y)的上侧是否已有皇后
  9. int left_top(int x,int y,char str[NUM][NUM]);//判断(x,y)的左上侧是否已有皇后
  10. int right_top(int x,int y,char str[NUM][NUM]);//判断(x,y)的右上侧是否已有皇后
  11. void init(char str[NUM][NUM]);//初始化,使棋盘上无皇后
  12. static int count = 0;//全局变量,用于记录已求解的个数

  13. int main(){
  14.         char str[NUM][NUM];

  15.         int x=0,y=0;
  16.         while(x != NUM){
  17.                 printf("当x=%d时---->\n",x);
  18.                 init(str);
  19.                 Queen(x,y,str);
  20.                 x++;
  21.         }
  22.         printf("count = %d\n",count);

  23.         return 0;
  24. }
  25. void init(char str[NUM][NUM]){
  26.         int i,j;
  27.         for(i=0;i<NUM;i++){
  28.                 for(j=0;j<NUM;j++){
  29.                         str[i][j] = NOTOCUR;
  30.                 }
  31.         }
  32. }
  33. void print(char str[NUM][NUM]){
  34.         printf("找到第%d组:\n",count);
  35.         int i,j;
  36.         for(i=0;i<NUM;i++){
  37.                 for(j=0;j<NUM;j++){
  38.                         printf("%c ",str[j][i]);
  39.                 }
  40.                 putchar('\n');
  41.         }
  42.         printf("*******************\n");
  43. }
  44. void Queen(int x,int y,char str[NUM][NUM]){
  45.         if(top(x,y,str) && left(x,y,str) && left_top(x,y,str) && right_top(x,y,str)){
  46.                 str[x][y] = OCUR;
  47.                 y++;
  48.                 x=0;
  49.                 if(y == NUM){
  50.                         count++;
  51.                         print(str);
  52.                         return;
  53.                 }
  54.                 while(x != NUM){
  55.                         Queen(x,y,str);
  56.                         str[x][y] = NOTOCUR;
  57.                         x++;
  58.                 }
  59.         }
  60.        
  61.         return;
  62. }
  63. int top(int x,int y,char str[NUM][NUM]){
  64.         int j;
  65.        
  66.         for(j = y;j >= 0;j--){
  67.                 if(str[x][j] == OCUR){
  68.                         return 0;
  69.                 }
  70.         }
  71.        
  72.         return 1;
  73. }
  74. int left(int x,int y,char str[NUM][NUM]){
  75.         int i;

  76.         for(i = x;i >= 0;i--){
  77.                 if(str[i][y] == OCUR){
  78.                         return 0;
  79.                 }
  80.         }
  81.        
  82.         return 1;
  83. }
  84. int left_top(int x,int y,char str[NUM][NUM]){
  85.         int i,j;

  86.         for(i=x,j=y;i>=0 && j>=0;i--,j--){
  87.                 if(str[i][j] == OCUR){
  88.                         return 0;
  89.                 }       
  90.         }

  91.         return 1;
  92. }
  93. int right_top(int x,int y,char str[NUM][NUM]){
  94.         int i,j;

  95.         for(i=x,j=y;i < NUM && j>=0;i++,j--){
  96.                 if(str[i][j] == OCUR){
  97.                         return 0;
  98.                 }       
  99.         }
  100.        
  101.         return 1;
  102. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 21:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表