八皇后之穷举法

#include <stdio.h>
#include <stdlib.h>

int q[8][8],count;
FILE *f;

void show()
{
    int i,j;
    count = count + 1;
    printf("%d\n",count);
    fprintf(f,"%d\n",count);
    for(i = 0; i < 8; i++)
    {
        for(j = 0; j < 8; j++)
        {
            printf("%-3d",q[i][j]);            
            fprintf(f,"%-3d",q[i][j]);
        }
        fprintf(f,"%c",'\n');
        printf("\n");
    }
    printf("\n\n");
    fprintf(f,"%c%c",'\n','\n');
}
//禁用函数,2表示皇后,0表示初始可用,1表示禁用
void forbid(int x,int y)
{
    int i,j;
    
    for(j = 0; j < 8; j++)
        if(j != y)
            q[x][j] = 1;
        
    for(i = 0; i < 8; i++)
        if(i != x)
            q[i][y] = 1;

    for(i = 1; x - i >= 0 && y - i >= 0; i++)
        q[x-i][y-i] = 1;
    
    for(i = 1; x + i >= 0 && y + i < 8; i++)
        q[x+i][y+i] = 1;

    for(i = 1; x - i >= 0 && y + i < 8; i++)
        q[x-i][y+i] = 1;
    
    for(i = 1; x + i < 8 && y - i >= 0; i++)
        q[x+i][y-i] = 1;

}

int main()
{
    int row1,row2,row3,row4,row5,row6,row7,row8,i,j;    //row1表示第1行的列变量
    f = fopen("八皇后.txt","w");
    count = 0;
    for(row1 = 0; row1 < 8; row1++)
        for(row2 = 0; row2 < 8; row2++)
            for(row3 = 0; row3 < 8; row3++)
                for(row4 = 0; row4 < 8; row4++)
                    for(row5 = 0; row5 < 8; row5++)
                        for(row6 = 0; row6 < 8; row6++)
                            for(row7 = 0; row7 < 8; row7++)
                                for(row8 = 0; row8 < 8; row8++)
                                {
                                    for(i = 0; i <8; i++)
                                        for(j = 0; j < 8; j++)
                                            q[i][j] = 0;
                                        
                                        q[0][row1] = 2;
                                        forbid(0,row1);
                                        
                                        if(q[1][row2] == 0)
                                        {
                                            q[1][row2] = 2;
                                            forbid(1,row2);
                                            if(q[2][row3] == 0)
                                            {
                                                q[2][row3] = 2;
                                                forbid(2,row3);

                                                if(q[3][row4] == 0)
                                                {
                                                    q[3][row4] = 2;
                                                    forbid(3,row4);

                                                    if(q[4][row5] == 0)
                                                    {
                                                        q[4][row5] = 2;
                                                        forbid(4,row5);
                                                                
                                                        if(q[5][row6] == 0)
                                                        {
                                                            q[5][row6] = 2;
                                                            forbid(5,row6);
                                                        
                                                            if(q[6][row7] == 0)
                                                            {
                                                                q[6][row7] = 2;
                                                                forbid(6,row7);

                                                                if(q[7][row8] == 0)
                                                                {
                                                                    q[7][row8] = 2;
                                                                    show();
                                                                }                                                        
                                                            }                                                    
                                                        }                                                            
                                                    }                                            
                                                }                                                                                            
                                            }
                                        }
                                }
    fclose(f);
    getchar();
    return 0;
}


你可能感兴趣的:(算法,C语言,八皇后)