2015-04-05 30 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