返回列表 發帖

a016: 數獨(SUDOKU)

題目連結
  1. /* 判斷一個九宮格數字是不是一個數獨的正解 */
  2. #include <iostream>
  3. #include <cstdlib>
  4. using namespace std;
  5. int main(void){
  6.     const int x = 9; //數獨大小 x*x
  7.     int a[x][x], b[x][x];
  8.     int total, no;
  9.     while (true){
  10.           total = 0;
  11.           no = 0;
  12.           /* 開始儲存9 * 9陣列的值 */
  13.           for (int i = 0; i < x; i++){
  14.               for (int j = 0; j < x; j++){
  15.                   
  16.                   if (cin >> a[i][j])
  17.                   ;
  18.                   else
  19.                       goto END;
  20.               }
  21.           }
  22.          
  23.           /* 開始檢查每一列是否有重複之值 1+...+9應等於45 */
  24.           for (int i = 0; i < x; i++){
  25.               .
  26.               .
  27.               .(部分程式碼)
  28.               if (total != 45){ //若!=45表示有重複之數字
  29.                  no++;
  30.               }
  31.               total = 0; //每次檢查完將 total 歸零
  32.           }

  33.           /* 在將陣列的行列交換 */
  34.           for (int i = 0; i < x; i++){
  35.               for (int j = 0; j < x; j++){
  36.                   b[j][i] = a[i][j];
  37.               }
  38.           }
  39.          
  40.           /* 開始檢查每一行是否有重複之值 1+...+9應等於45 */
  41.           for (int i = 0; i < x; i++){
  42.               for (int j = 0; j < x; j++){
  43.                   total += b[i][j];
  44.               }
  45.                .
  46.               .
  47.               .(部分程式碼)
  48.               total = 0; //每次檢查完將 total 歸零
  49.           }

  50.           /* 開始檢查九宮格裡是否有重複之值 1+...+9應等於45 */
  51.           for (int i = 0; i < 9; i+=3){
  52.               for (int j = 0; j < 9; j+=3){
  53.                   for (int x = i; x < i+3; x++){
  54.                       for (int y = j; y < j+3; y++){
  55.                           total += a[x][y];
  56.                       }
  57.                   }
  58.                    .
  59.               .
  60.               .
  61. (部分程式碼)
  62.                   total = 0; //每次檢查完將 total 歸零
  63.               }   
  64.           }
  65.           /* 最後判斷 no 的值 */         
  66.           if (no > 0){
  67.              cout << "no" << endl;      
  68.           }else{
  69.                 cout << "yes" << endl;   
  70.           }
  71.     }
  72.     //system("pause");
  73. END:
  74.     return 0;
  75. }
複製代碼
我是來去無蹤的..

                                ..士豪(Alen)黑輪

我是           「士豪」!!!
我是來去無蹤的..

                                ..士豪(Alen)黑輪

TOP

返回列表