八皇后问题一种求解思路

 

//这个程序是用来求八皇后问题的所有解
//基本思路是当(i,j)位置被皇后占了的时候,i行,j列,和i+j,i-j相等的斜行也不能够被其他皇后占有

#include
"stdio.h"
#include
"stdlib.h"
#define SIZE  8
#define TOTALNUM 100 

int row[ SIZE ] ;//记录行的数组
int col[ SIZE ] ;//记录列的数组
int titlr[2*SIZE-3];//记录斜行的数组
int titll[2*SIZE-3];//记录斜行左的数组
int totalway[8];//记录所有的解
int totalwayLen = 0 ;

void PrintWay()  //打印每一组解
{
    printf(
"The Number %2d of Way : ",totalwayLen ) ;
 
for ( int i = 0 ; i < SIZE ; i ++ )
  printf(
"%-2d",totalway[i]+1);
 printf(
" ");
}

void ReturnLast( int i )  //返回时将上一组还原
{

 
int temp = totalway[i-1];
 row[i
-1= 0 ;
    col[temp] 
= 0 ;
    titlr[i
-1-temp+6= 0 ;
 titll[i
-1+temp-1= 0 ;

}

void SeacherWay( int i ) //从何行开始搜索
{
     
for ( int j = 0 ; j < SIZE ; j ++ )
  {
   
if ( row[i] == 0 && col[j] == 0 && titlr[i-j+6== 0 && titll[i+j-1== 0 ) //当这个位置还是空闲的时候
   {
    totalway[i] 
= j ; //记录列的位置
    if ( i == SIZE - 1 )  //最后一组分配位置成功
    {
     totalwayLen 
++ ;
     PrintWay();
                 ReturnLast(i);
                 
return ;
    }
    row [i] 
= 1//将占的位置恢复
    col [j] = 1;
    titlr[i
-j+6= 1;
    titll[i
+j-1= 1;
             SeacherWay(i
+1);
       
   }

  }
  
if ( j == SIZE ) 
  {
     ReturnLast(i);
  
return ;
  }
}

int main()
{
      SeacherWay(
0) ;
   printf(
"%d " ,totalwayLen ) ;
   
return 0 ;

 

最近写的:

#include<iostream>
#include
<cmath>
using namespace std;
#define SIZE 8 


int queen[SIZE][SIZE],count = 0 ;

bool isCan(int nowrow ,int col)
{
    
for ( int row = 0 ; row < nowrow ; row ++)
    {
        
if ( queen[row][col] == 1)
        {
            
return false;
        }
        
for ( int i = 0 ; i < SIZE ; i ++)
        {
            
bool flag = ((row + i == nowrow +col) || ( row - i == nowrow - col));
            
if ( flag && queen[row][i] == 1)
            {
                
return false;
            }
        }
    }
    
return true ;
}


void eightQueen(int n)
{
    
if ( n == 0)
    {
        
for ( int i = 0 ; i < SIZE ; i ++ )
        {
            
for ( int j = 0 ; j < SIZE ; j++ )
            {
                
if ( queen[i][j] == 1)
                    cout
<<j;
            }

        }
        cout
<<endl;
        count 
++ ;
        
return ;
    }
    
else
    {
        
int row = SIZE - n ;
        
for ( int col = 0 ; col < SIZE ; col ++ )
        { 
            
if(isCan(row,col))
            {
                queen[row][col] 
= 1 ;
                eightQueen(n
-1);
                queen[row][col] 
= 0 ;
            }    
        }    
    }
}


int main()
{
    eightQueen(
8);
    cout
<<"count:"<<count<<endl;
    
return 0 ;
}

 

 

在vc++中运行结果为(共有92组解):

The Number  1 of Way : 1 5 8 6 3 7 2 4
The Number  2 of Way : 1 6 8 3 7 4 2 5
The Number  3 of Way : 1 7 4 6 8 2 5 3
The Number  4 of Way : 1 7 5 8 2 4 6 3
The Number  5 of Way : 2 4 6 8 3 1 7 5
The Number  6 of Way : 2 5 7 1 3 8 6 4
The Number  7 of Way : 2 5 7 4 1 8 6 3
The Number  8 of Way : 2 6 1 7 4 8 3 5
The Number  9 of Way : 2 6 8 3 1 4 7 5
The Number 10 of Way : 2 7 3 6 8 5 1 4
The Number 11 of Way : 2 7 5 8 1 4 6 3
The Number 12 of Way : 2 8 6 1 3 5 7 4
The Number 13 of Way : 3 1 7 5 8 2 4 6
The Number 14 of Way : 3 5 2 8 1 7 4 6
The Number 15 of Way : 3 5 2 8 6 4 7 1
The Number 16 of Way : 3 5 7 1 4 2 8 6
The Number 17 of Way : 3 5 8 4 1 7 2 6
The Number 18 of Way : 3 6 2 5 8 1 7 4
The Number 19 of Way : 3 6 2 7 1 4 8 5
The Number 20 of Way : 3 6 2 7 5 1 8 4
The Number 21 of Way : 3 6 4 1 8 5 7 2
The Number 22 of Way : 3 6 4 2 8 5 7 1
The Number 23 of Way : 3 6 8 1 4 7 5 2
The Number 24 of Way : 3 6 8 1 5 7 2 4
The Number 25 of Way : 3 6 8 2 4 1 7 5
The Number 26 of Way : 3 7 2 8 5 1 4 6
The Number 27 of Way : 3 7 2 8 6 4 1 5
The Number 28 of Way : 3 8 4 7 1 6 2 5
The Number 29 of Way : 4 1 5 8 2 7 3 6
The Number 30 of Way : 4 1 5 8 6 3 7 2
The Number 31 of Way : 4 2 5 8 6 1 3 7
The Number 32 of Way : 4 2 7 3 6 8 1 5
The Number 33 of Way : 4 2 7 3 6 8 5 1
The Number 34 of Way : 4 2 7 5 1 8 6 3
The Number 35 of Way : 4 2 8 5 7 1 3 6
The Number 36 of Way : 4 2 8 6 1 3 5 7
The Number 37 of Way : 4 6 1 5 2 8 3 7
The Number 38 of Way : 4 6 8 2 7 1 3 5
The Number 39 of Way : 4 6 8 3 1 7 5 2
The Number 40 of Way : 4 7 1 8 5 2 6 3
The Number 41 of Way : 4 7 3 8 2 5 1 6
The Number 42 of Way : 4 7 5 2 6 1 3 8
The Number 43 of Way : 4 7 5 3 1 6 8 2
The Number 44 of Way : 4 8 1 3 6 2 7 5
The Number 45 of Way : 4 8 1 5 7 2 6 3
The Number 46 of Way : 4 8 5 3 1 7 2 6
The Number 47 of Way : 5 1 4 6 8 2 7 3
The Number 48 of Way : 5 1 8 4 2 7 3 6
The Number 49 of Way : 5 1 8 6 3 7 2 4
The Number 50 of Way : 5 2 4 6 8 3 1 7
The Number 51 of Way : 5 2 4 7 3 8 6 1
The Number 52 of Way : 5 2 6 1 7 4 8 3
The Number 53 of Way : 5 2 8 1 4 7 3 6
The Number 54 of Way : 5 3 1 6 8 2 4 7
The Number 55 of Way : 5 3 1 7 2 8 6 4
The Number 56 of Way : 5 3 8 4 7 1 6 2
The Number 57 of Way : 5 7 1 3 8 6 4 2
The Number 58 of Way : 5 7 1 4 2 8 6 3
The Number 59 of Way : 5 7 2 4 8 1 3 6
The Number 60 of Way : 5 7 2 6 3 1 4 8
The Number 61 of Way : 5 7 2 6 3 1 8 4
The Number 62 of Way : 5 7 4 1 3 8 6 2
The Number 63 of Way : 5 8 4 1 3 6 2 7
The Number 64 of Way : 5 8 4 1 7 2 6 3
The Number 65 of Way : 6 1 5 2 8 3 7 4
The Number 66 of Way : 6 2 7 1 3 5 8 4
The Number 67 of Way : 6 2 7 1 4 8 5 3
The Number 68 of Way : 6 3 1 7 5 8 2 4
The Number 69 of Way : 6 3 1 8 4 2 7 5
The Number 70 of Way : 6 3 1 8 5 2 4 7
The Number 71 of Way : 6 3 5 7 1 4 2 8
The Number 72 of Way : 6 3 5 8 1 4 2 7
The Number 73 of Way : 6 3 7 2 4 8 1 5
The Number 74 of Way : 6 3 7 2 8 5 1 4
The Number 75 of Way : 6 3 7 4 1 8 2 5
The Number 76 of Way : 6 4 1 5 8 2 7 3
The Number 77 of Way : 6 4 2 8 5 7 1 3
The Number 78 of Way : 6 4 7 1 3 5 2 8
The Number 79 of Way : 6 4 7 1 8 2 5 3
The Number 80 of Way : 6 8 2 4 1 7 5 3
The Number 81 of Way : 7 1 3 8 6 4 2 5
The Number 82 of Way : 7 2 4 1 8 5 3 6
The Number 83 of Way : 7 2 6 3 1 4 8 5
The Number 84 of Way : 7 3 1 6 8 5 2 4
The Number 85 of Way : 7 3 8 2 5 1 6 4
The Number 86 of Way : 7 4 2 5 8 1 3 6
The Number 87 of Way : 7 4 2 8 6 1 3 5
The Number 88 of Way : 7 5 3 1 6 8 2 4
The Number 89 of Way : 8 2 4 1 7 5 3 6
The Number 90 of Way : 8 2 5 3 1 7 4 6
The Number 91 of Way : 8 3 1 6 2 5 7 4
The Number 92 of Way : 8 4 1 3 6 2 7 5
92

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值