代码[C]中的语法错误 - 八皇后拼图

时间:2015-04-05 23:48:24

标签: c arrays n-queens

对于模糊的标题感到抱歉,我不确定如何解释。我正在努力解决八皇后谜题。

对于那些不熟悉八皇后谜题的人:


这个程序应该找到8个皇后可以的方式 放在一个8x8的棋盘上,这样女王就不能 互相捕获 - 也就是说,没有列,行或 对角线被一个以上的女王占据。


我在脑海中绘制的方式是:

1)我将设置整个数组:chess[8][8] = {2}

2)转到数组的开头,只要chess[i][j] == 2,它就会被重新分配给1。然后,只要发生这种情况,我就会有另一个名为set_illegal的程序块,它会将对角线,行和列设置为0.(因此我必须让chess[8][8]成为全局变量。)

3)set_illegal完成后,测试程序将跳回assign_q的循环,并且该过程将重新开始。

4)之后它将打印出解决方案。不幸的是我没有编写这个找到多个解决方案...所以它只会显示1个解决方案(一种nooby编程lol ...)

非常感谢任何输入!

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


    #define N 8

    int chess[N][N] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, row1, column1; 
//the reason for row1 and column1 is so that set_row, set_column, and set_diagonal 
//will have a spot to start. Also I could have assigned all the elements in the array to 2 using a loop, but I was feeling a little lazy...

    void assign_q(int **chess[N][N]**);

    int main()
    {
        int row, column;

        assign_q(chess);

        for (row = 0; row < N; row++) //prints the whole table
        {
            for (column = 0; column < N; column++)
                printf("%d ", chess[row][column]);

            printf("\n");
        }

        return 0;
    }

    void set_illegal(void);

    void assign_q(int chess[N][N])
    {
        int row, column;

        for (column = 0; column < N; column++)
        {
            for (row = 0; row < N; row++)
            {
                if (chess[row][column] == 2) //if the element of the array is equal to 2, then it will set it to 1
                {
                    chess[row][column] = 1;
                    row1 = column;
                    column1 = column;
                    set_illegal(); //goes through the column, row, and diagonal to set them all illegal
                    break;
                }
            }
        }
    }

    void set_column(void);
    void set_row(void);
    void set_diagonal(void);

    void set_illegal()
    {
        set_column();
        set_row();
        set_diagonal();
    }

    void set_column()
    {
        int row;

        for (row = 0; row < N; row++)
            chess[row][column1] = 0; //sets the column illegal
    }

    void set_row()
    {
        int column;

        for (column = 0; column < N; column++)
            chess[row1][column] = 0; //sets the row illegal
    }

    void set_diagonal()
    {
        int row, column;

        for (row = row1 + 1, column = column1 + 1; row < N && column < N; row++, column++)
            chess[row][column] = 0; //sets diagonals in the slope of -1 downwards illegal

        for (row = row1 - 1, column = column1 - 1; row >= 0 && column >= 0; row--, column--)
            chess[row][column] = 0; //sets diagonals in the slope of -1 upwards illegal

        for (row = row1 - 1, column = column1 + 1; row >= 0 && column < N; row--, column++)
            chess[row][column] = 0; //sets diagonals in the slope of +1 upwards illegal

        for (row = row1 + 1, column = column1 - 1; row < N && column >= 0; row++, column--)
            chess[row][column] = 0; //sets diagonals in the slope of +1 downwards illegal
    }

以粗体更改后,我得到的唯一错误是程序实际上没有工作ahahaha。无论如何,我会想出那一个。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

void assign_q(int chess) - 我想你想要一块板子,而不是一块板子。