2016-10-05 27 views
-1

我想做一個數獨檢查器函數來查看輸入的棋盤是否有效。要做到這一點,我會生成一個棋盤,然後檢查每個單獨的行,列和9個方格的集合,看它們是否包含數字1-9。如果他們不包含該特定區域中的每個數字,該功能將會在數獨板的行上進行檢查。我不想在檢查編譯版本時輸入全部9行,所以我現在只關注單行。我的循環語句有什麼問題?

該程序檢查該行是否包含全部9個數字。如果該行缺少其中一個數字(1-9),則該函數將顯示return false;,並顯示「無效的電路板!」。

但是,程序總是說它是一個有效的板,即使它缺少一些所需的數字。

這裏是我的代碼的副本至今:

#include <iostream> 
using namespace std; 

const int DIMEN = 9; 

bool is_valid(int []); 

int main() 
{ 
    int board[DIMEN]; 
    cout << "Enter values for array.\n"; 
    for (int arraynumber = 0; arraynumber < DIMEN; arraynumber++) 
    { 
     cout << "\nArray [" << arraynumber << "]? "; 
     cin >> board[arraynumber]; 
    } 
    bool valid = is_valid(board); 
    if (valid) 
    { 
     cout << "\nValid Board!\n"; 
    } 
    else 
    { 
     cout << "\nInvalid Board!\n"; 
    } 
    return 0; 
} 

bool is_valid(int isvalid[]) 
{ 
    bool check_row = false; 
    //Checks to see if the row has all required numbers 
    bool check_number = false; 
    //Checks to see if the row contains a specific number in it 

     while (!(check_row)) 
     //While the row doesn't yet have all required numbers in it 
     { 
      for (int number = 1; number <= DIMEN; number++) 
      // Goes through each # (1-9) to see if the row contains that # 
      { 
       while (!(check_number)) 
       //While the row doesn't yet have the number in it 
       { 
        for (int i = 0; i < DIMEN; i++) 
        //Move across the row from column 0 to 8 
        { 
         if (isvalid[i] == number) 
         /* If the value for this specific element of the array 
         equals the number */ 
         { 
          check_number = true; 
          //The row has the number in it 
         } 
        } 
        if (!(check_number)) 
        /* If the number was not found in the row by the 
        end of the for loop */ 
        { return false; 
         //End the program saying the board is invalid 
        } 
       } 
      } 
      check_row = true; 
     } 
    return true; 
} 
+1

解決此類問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –

+1

當我正在閱讀這篇文章時,我想起了一個想法。閱讀有關程序的循環複雜性。爲了節目減少它是一個好習慣。如果在一段時間內,你有一段時間。無論如何,這不是你的問題。我會看看我能否找到一些 – Makketronix

回答

0

你從來沒有check_number回假,當你開始檢查新號碼。

話雖如此,你的代碼相當複雜,難以閱讀。你可以這樣做:

bool is_valid(int row[]) 
{ 
    for(int number = 1; number <= 9; number++) 
    { 
     bool found = false; 
     for(int i = 0; i < DIMEN; i++) { 
      found = (row[i] == number); 
      if (found) break; 
     } 
     // We've looked at each spot in the row and not found number 
     // so we know the row isn't valid. 
     if (!found) return false; 
    } 
    // If we get here we must have found every number so the row is valid 
    return true; 
} 
+0

非常感謝!這完全解決了我的問題! –

0

你的功能似乎不必要的複雜。我不清楚它應該如何工作。這是一個應該起作用的簡化版本。

bool is_valid(int board[]) 
{ 
    // start with board_state = {0, 0, 0, 0, 0, 0, 0, 0, 0} 
    // if the board is valid, we will end up with 
    // board_state = {1, 1, 1, 1, 1, 1, 1, 1, 1} 
    int board_state[DIMEN] = {0}; 
    for (int i = 0; i < DIMEN; ++i) 
    { 
     int n = board[i]; 
     ++board_state[n-1]; 
    } 

    for (int i = 0; i < DIMEN; ++i) 
    { 
     if (board_state[i] != 1) 
     { 
     return false; 
     } 
    } 

    return true; 
} 
0

問題是這樣的:

if (isvalid[i] == number) 
        /* If the value for this specific element of the array 
        equals the number */ 
        { 
         check_number = true; 
         //The row has the number in it 
        } 

一旦你做出這個屬實,這將是真正的永遠的該功能。

因此,如果你找到一個數字,你將返回true!

注: 當我做了一個數獨我這樣做之前:

  • 造成了短路,X,這是16位,將其設置爲0
  • 當我發現一個數n ,左移1(n-1)
  • 按位或操作:移位的數字| short x
  • 如果short爲0b111111111,即511十進制或1FF(十六進制),則只有完整的行,或列或方塊。

這將是一個更簡單的實現:循環一次設置位,然後驗證一次。