鱼C论坛

 找回密码
 立即注册
查看: 2621|回复: 1

代码不长,没看懂,请教下大家。

[复制链接]
发表于 2016-4-8 14:10:45 | 显示全部楼层 |阅读模式

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

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

x
  1. # include <iostream>
  2. using namespace std;

  3. int col[8],Left[15],Right[15];
  4. int queen[8];
  5. int n=0;
  6. int sum=0;

  7. void generate()
  8. {
  9.         int h,i;
  10.         for(h=0;h<=7;h++)
  11.         {
  12.                 if(col[h]&& Left[n+h] && Right[n-h+7])
  13.                 {
  14.                         queen[n]=h;
  15.                         col[h]=false;
  16.                         Left[n+h]=false;
  17.                         Right[n-h+7]=false;
  18.                         n+=1;
  19.                         if(n==8)
  20.                         {
  21.                                 sum++;
  22.                                 cout<<"  "<<sum;
  23.                                 for(i=0;i<=7;i++)
  24.                                  cout<<"\t"<<queen[i];
  25.                                 cout<<endl;
  26.                         }
  27.                         else generate();
  28.                         n--;
  29.                         Left[n+h]=true;
  30.                         Right[n-h+7]=true;
  31.                         col[h]=true;
  32.                 }
  33.         }
  34. }
  35. void main()
  36. {  
  37.         int c,s;
  38.         for(c=0;c<=7;++c)
  39.                 col[c]=true;
  40.         for(s=0;s<=14;++s)
  41.         {
  42.                 Left[s]=true;
  43.                 Right[s]=true;
  44.         }
  45.         cout<<"行数:\t0\t1\t2\t3\t4\t5\t6\t7"<<endl;
  46.          generate();
  47.         cout<<"八皇后摆法总数:"<<sum<<endl;
  48.         system("pause");
  49. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-4-8 14:11:20 | 显示全部楼层
generate()这个函数真心看不懂
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 05:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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