鱼C论坛

 找回密码
 立即注册
查看: 1806|回复: 2

为什么n>=10的 n皇后问题算不出来?

[复制链接]
发表于 2018-9-28 18:04:48 | 显示全部楼层 |阅读模式

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

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

x
RT。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-28 19:41:57 | 显示全部楼层
内存满了?不至于吧?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-28 20:58:35 | 显示全部楼层
claws0n 发表于 2018-9-28 19:41
内存满了?不至于吧?
  1. int Count=0;       //方法计数
  2. int FirstRow=0;   //第一行皇后位置
  3. int RowIsOk=0;  
  4. int IsNotDanger(int row,int col)
  5. {
  6.     if(chess[row][col]==-1)return 0;   //当前位置
  7.     for(int m=0;m<row;m++)
  8.         if(chess[m][col]==1)return 0;  //列
  9.     for(int m=row-1,n=col-1;m>=0&&n>=0;m--,n--)
  10.         if(chess[m][n]==1)return 0;    //左上角
  11.     for(int m=row-1,n=col+1;m>=0&&n<N;m--,n++)
  12.         if(chess[m][n]==1)return 0;    //右上角
  13.     for(int m=row+1,n=col-1;m<N&&n>=0;m++,n--)
  14.         if(chess[m][n]==1)return 0;    //左下角
  15.     for(int m=row+1,n=col+1;m<N&&n<N;m++,n++)
  16.         if(chess[m][n]==1)return 0;    //左上角
  17.     return 1;
  18. }
  19. void Queen2(int row)
  20. {
  21.     if(FirstRow==N)return;
  22.     RowIsOk=0;
  23.     if(row==N)
  24.     {
  25.         Count++;
  26.         printf("第 %d 种:\n",Count);
  27.         for(int i=0;i<N;i++)
  28.         {
  29.             for(int j=0;j<N;j++)
  30.             {
  31.                 if(chess[i][j]==1)
  32.                     printf("%d ",chess[i][j]);
  33.                else printf("0 ");
  34.             }
  35.             cout<<endl;
  36.         }
  37.         for(int j=0;j<N;j++)
  38.             if(chess[row-1][j]==1)chess[row-1][j]=-1;
  39.     }
  40.     else
  41.     {
  42.         for(int j=0;j<N;j++)
  43.             if(IsNotDanger(row,j))
  44.             {
  45.                 RowIsOk=1;
  46.                 chess[row][j]=1;
  47.                 Queen2(row+1);
  48.             }
  49.         if(!RowIsOk)
  50.         {
  51.             for(int j=0;j<N;j++)
  52.                 if(chess[row-1][j]==1)
  53.                 {
  54.                     chess[row-1][j]=-1;
  55.                     break;
  56.                 }
  57.             for(int j=0;j<N;j++)chess[row][j]=0;
  58.             if(row==1)FirstRow++;
  59.             Queen2(row-1);
  60.         }
  61.     }
  62. }
  63. int main()
  64. {
  65.     for(int i=0;i<N;i++)
  66.         for(int j=0;j<N;j++)
  67.             chess[i][j]=0;
  68.     Queen2(0);
  69.     return 0;
  70. }
复制代码

麻烦帮我看一下,谢谢!
N<10的时候一切正常,N>=10就计算不出来了。

N=9

N=9

N=10

N=10

N=8

N=8
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-26 17:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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