• 八皇后问题


    #include <stdio.h>
    #include <stdlib.h>
    static char Queen[8][8];
    static int a[8];
    static int b[15];
    static int c[15];
    static int iQueenNum = 0;//record state
    void qu(int i);

    int main(int argc, char *argv[])
    {
    //init
    int line, column;
    for(line = 0;line < 8;line++)
    {
    a[line] = 0;
    for(column = 0;column < 8;column++)
    {
    Queen[line][column] = '*';
    }

    }

    for(line = 0;line < 15;line++)
    {
    b[line] = c[line] = 0;;
    }

    qu(0);
    system("PAUSE");
    return 0;
    }

    void qu(int i)//columm
    {
    int iColumn;
    for (iColumn = 0;iColumn < 8;iColumn++)
    {
    //no conflict
    if(a[iColumn]==0 && b[i-iColumn+7]==0 && c[i+iColumn]==0)
    {
    //put a queen
    Queen[i][iColumn] = '@';
    //record confilict
    a[iColumn] = 1;
    b[i-iColumn+7] = 1;
    c[i+iColumn] = 1;
    if(i<7) qu(i+1);
    else//out put
    {
    int line, column;
    printf("第%d种状态为:\n", ++iQueenNum);
    for(line = 0;line < 8;line++)
    {
    for(column = 0;column < 8;column++)
    {
    printf("%c ", Queen[line][column]);
    }
    printf("\n");
    }
    printf("\n\n");
    }

    //back
    Queen[i][iColumn] = '*';
    a[iColumn] = 0;
    b[i-iColumn+7] = 0;
    c[i+iColumn] = 0;
    }
    }
    }

  • 相关阅读:
    第五,六章
    第三,四章
    第一,二章
    20131019作业 2 分支、循环结构
    20131016课堂实验4
    20131014课堂实验3
    20131007国庆作业例7-11,7-12,7-13,7-14
    20131006国庆作业例7-7,7-8,7-9
    20131006国庆作业例7-4,7-5,7-6
    20131006国庆作业第七章例7-1,7-2,7-3
  • 原文地址:https://www.cnblogs.com/hbf369/p/2673655.html
Copyright © 2020-2023  润新知