題目連結- /* 判斷一個九宮格數字是不是一個數獨的正解 */
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- int main(void){
- const int x = 9; //數獨大小 x*x
- int a[x][x], b[x][x];
- int total, no;
- while (true){
- total = 0;
- no = 0;
- /* 開始儲存9 * 9陣列的值 */
- for (int i = 0; i < x; i++){
- for (int j = 0; j < x; j++){
-
- if (cin >> a[i][j])
- ;
- else
- goto END;
- }
- }
-
- /* 開始檢查每一列是否有重複之值 1+...+9應等於45 */
- for (int i = 0; i < x; i++){
- .
- .
- .(部分程式碼)
- if (total != 45){ //若!=45表示有重複之數字
- no++;
- }
- total = 0; //每次檢查完將 total 歸零
- }
- /* 在將陣列的行列交換 */
- for (int i = 0; i < x; i++){
- for (int j = 0; j < x; j++){
- b[j][i] = a[i][j];
- }
- }
-
- /* 開始檢查每一行是否有重複之值 1+...+9應等於45 */
- for (int i = 0; i < x; i++){
- for (int j = 0; j < x; j++){
- total += b[i][j];
- }
- .
- .
- .(部分程式碼)
- total = 0; //每次檢查完將 total 歸零
- }
- /* 開始檢查九宮格裡是否有重複之值 1+...+9應等於45 */
- for (int i = 0; i < 9; i+=3){
- for (int j = 0; j < 9; j+=3){
- for (int x = i; x < i+3; x++){
- for (int y = j; y < j+3; y++){
- total += a[x][y];
- }
- }
- .
- .
- .
- (部分程式碼)
- total = 0; //每次檢查完將 total 歸零
- }
- }
- /* 最後判斷 no 的值 */
- if (no > 0){
- cout << "no" << endl;
- }else{
- cout << "yes" << endl;
- }
- }
- //system("pause");
- END:
- return 0;
- }
複製代碼 |