回 帖 发 新 帖 刷新版面

主题:5皇后问题找错,少了一个答案

//5皇后问题
#include<iostream.h>
void main()
{
    const int m=5,n=5;
    int A[m][n],B[18],C[5],l=0;
    for(int i=0;i<5;i++)
    {
        A[0][i]=1;
        for(int j=0;j<5;j++)
        {
            if(j!=i)
            A[0][j]=0;
        }
        for(int o=0;o<5;o++)
        {
            A[1][o]=1;
            for(int p=0;p<5;p++)
            {
                if(o!=p)
                A[1][p]=0;
            }
            for(int a=0;a<5;a++)
            {
                A[2][a]=1;
                for(int b=0;b<5;b++)
                {
                    if(a!=b)
                    A[2][b]=0;
                }
                  for(int g=0;g<5;g++)
                {
                    A[3][g]=1;
                    for(int h=0;h<5;h++)
                    {
                       if(g!=h)
                         A[3][h]=0;
                    }
                    for(int z=0;z<5;z++)
                    {
                      A[4][z]=1;
                       for(int x=0;x<5;x++)
                       {
                            if(z!=x)
                               A[4][x]=0;
                       }

                       B[0]=A[0][0];//每一条斜线元素和
                       B[1]=A[1][0]+A[0][1];
                       B[2]=A[2][0]+A[1][1]+A[0][2];
                       B[3]=A[3][0]+A[2][1]+A[1][2]+A[0][3];
                       B[4]=A[4][0]+A[3][1]+A[2][2]+A[1][3]+A[0][4];
                       B[5]=A[4][1]+A[3][2]+A[2][3]+A[1][4];
                       B[6]=A[4][2]+A[3][3]+A[1][4];
                       B[7]=A[4][3]+A[3][4];
                       B[8]=A[4][4];
                       B[9]=A[4][0];
                       B[10]=A[3][0]+A[4][1];
                       B[11]=A[2][0]+A[3][1]+A[4][2];
                       B[12]=A[1][0]+A[2][1]+A[3][2]+A[4][3];
                       B[13]=A[0][0]+A[1][1]+A[2][2]+A[3][3]+A[4][4];
                       B[14]=A[0][1]+A[1][2]+A[2][3]+A[3][4];
                       B[15]=A[0][2]+A[1][3]+A[2][4];
                       B[16]=A[0][3]+A[1][4];
                       B[17]=A[0][4];
   
                       for(int xx=0;xx<=4;xx++)//每列元素之和
                       {
                           for(int yy=0;yy<=4;yy++)
                            {
                               C[xx]=C[xx]+A[yy][xx];
                            }
                       }
                       
                       if(B[0]<=1&&B[1]<=1&&B[2]<=1&&B[3]<=1&&B[4]<=1&&B[5]<=1&&B[6]<=1&&B[7]<=1&&B[8]<=1//判断同一斜线
                         &&B[9]<=1&&B[10]<=1&&B[11]<=1&&B[12]<=1&&B[13]<=1&&B[14]<=1&&B[15]<=1&&
                          B[16]<=1&&B[17]<=1)
                       {
                          if(i!=o&&i!=a&&i!=g&&i!=z&&o!=a&&o!=g&&o!=z&&a!=g&&a!=z&&g!=z)//判断同一列
                           {
                              l++;
                              cout<<"  ****第"<<l<<"组****"<<endl;
                               for(int k=0;k<5;k++)
                                   for(int l=0;l<5;l++)
                                   {
                                       cout<<"  "<<A[k][l];
                                       if(l==4)
                                       cout<<endl;
                                   }
                                   cout<<endl;
                           }
                       }
                    }
                }
            }     
        }    
    }
}

回复列表 (共3个回复)

沙发

好强大的程序

板凳

-,- 
   看不懂 中间那部分让我想起 Strassen ..

3 楼

贴个一般的8皇后求解
#include <iostream>
#include <string>
#define N 8
using namespace std;

class Queen{
    public:
        Queen();
        ~Queen(){}
        bool IsClash(int row,int col);
        void FindSolve(int row);
        void PrintMap();
        
    private:
        int count;
        string str[N];
};

Queen::Queen(){ //初始化图 
    count=0;
    for(int i=0;i<N;++i)
       str[i]="--------";
}

void Queen::FindSolve(int row){
    for(int col=0;col<N;++col)                
    {
        if(IsClash(row,col))
        {
            str[row].replace(col,1,"Q");     
            
            if(row<N-1)
            FindSolve(row+1);
            else
            PrintMap();
        }
        str[row]="--------"; 
    }
}

void Queen::PrintMap(){
    cout<<N<<"皇后问题第"<<++count<<"种解:"<<endl; 
    for(int i=0;i<N;++i)
        cout<<str[i]<<endl;
    cout<<endl<<endl;
    system("pause");
}
        
bool Queen::IsClash(int row,int col){
    int r,c;
    for(r=0;r<row;++r)
    {
        string st=str[r];
        c=st.find("Q");
        if(r==row||c==col||(c-r)==(col-row)||(c+r)==(col+row))
                        
        return false;
    }
    return true;
}
    
int main(){
    Queen Q;
    Q.FindSolve(0);
    return 0;
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册