2015-04-05 62 views
0

对不起,模糊的标题,我真的不知道如何解释。我正在努力解决八皇后难题。代码中的语法错误[C] - 八皇后拼图

对于那些不熟悉的八个皇后问题:


这个程序应该找到一个可行的办法是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编程大声笑...)

任何输入是非常感谢!

#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。无论如何,我会找出一个。感谢所有的帮助!

+0

那么,实际的错误是什么? – MSalters 2015-04-05 23:51:43

+0

@MSalters只是将它们添加进去,对不起, – 2015-04-05 23:54:56

+0

'int chess [N] [N] = {2}'这只会设置为2棋[0] [0]。其他设置为0 – 2015-04-05 23:56:40

回答

0

void assign_q(int chess) - 我想你想在这里有一个董事会,而不是一个领域。

+0

你究竟是什么意思?我应该有'void assign_q(int chess [] [])''? – 2015-04-06 00:10:35

+0

多数民众赞成在我主要有麻烦,是调用该数组的语法 – 2015-04-06 00:10:57

+0

@NicolasDiken:'void assign_q(int chess [N] [N])' - 在技术上最后N是多余的,但它是很好的文档来说明什么你的期望。第一个N是必须弄清楚数据是如何组织的。 – MSalters 2015-04-06 00:46:23