Advertisement
uopspop

Untitled

Apr 28th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <iostream>
  2. #define ROW 9
  3. #define COL 9
  4. using namespace std;
  5.  
  6.  
  7.  
  8. bool submatrixJudge(int matrix[ROW][COL], int startRow, int endRow, int startCol, int endCol){
  9.     int examiner[10] = {0};
  10.     bool sudoku = true;
  11.  
  12.     for (int i = startRow - 1; i <= endRow -1; i++){
  13.         for (int j = startCol - 1; j <= endCol -1; j++) {
  14.             examiner[matrix[i][j]] -= 1;
  15.             if (examiner[matrix[i][j]] == -2) {
  16.                 sudoku = false;
  17.                 return sudoku;
  18.             }
  19.         }
  20.     }
  21.     return sudoku;
  22. }
  23.  
  24.  
  25. int main() {
  26.  
  27.  
  28.     int matrix[ROW][COL];
  29.     while(cin >> matrix[0][0]){
  30.     // input the matrix
  31.         for (int i = 0; i < ROW; i++) {
  32.             for (int j = 0; j < COL; j++) {
  33.                 if (i == 0 && j == 0) {
  34.                     continue;
  35.                 }
  36.                 cin >> matrix[i][j];
  37.             }
  38.         }      
  39.    
  40.     // get rid of the extra line
  41.         char c[100];
  42.         cin.getline(c,20);
  43.            
  44.     // 每列只能有1~9數字各出現一次
  45.         try{
  46.             for (int i = 0; i < ROW; i++) {
  47.                 int examiner[10] = {0};
  48.                 for (int j = 0; j < COL; j++) {
  49.                     examiner[(matrix[i][j])] -= 1;
  50.                     if (examiner[(matrix[i][j])] == -2) {
  51.                         throw 0;
  52.                     }// end if
  53.                 }// end for j      
  54.             }// end for
  55.         }// end try
  56.         catch(int){
  57.                 cout << "no" << endl;
  58.                 continue;  
  59.         }// end catch
  60.            
  61.     // 每行只能有1~9數字各出現一次
  62.         try{
  63.             for (int i = 0; i < COL; i++) {
  64.                 int examiner[10] = {0};
  65.                 for (int j = 0; j < ROW; j++) {
  66.                     examiner[(matrix[j][i])] -= 1;
  67.                     if (examiner[(matrix[j][i])] == -2) {
  68.                         throw 0;
  69.                     }// end if
  70.                 }// end for j      
  71.             }// end for
  72.         }// end try
  73.         catch(int){
  74.                 cout << "no" << endl;
  75.                 continue;
  76.         }// end catch
  77.    
  78.        
  79.     // subMatrix examining
  80.         try{
  81.             if (!(submatrixJudge(matrix, 1, 3, 1, 3))) throw 0;
  82.             if (!(submatrixJudge(matrix, 1, 3, 4, 6))) throw 0;
  83.             if (!(submatrixJudge(matrix, 1, 3, 7, 9))) throw 0;
  84.             if (!(submatrixJudge(matrix, 4, 6, 1, 3))) throw 0;
  85.             if (!(submatrixJudge(matrix, 4, 6, 4, 6))) throw 0;
  86.             if (!(submatrixJudge(matrix, 4, 6, 7, 9))) throw 0;
  87.             if (!(submatrixJudge(matrix, 7, 9, 1, 3))) throw 0;
  88.             if (!(submatrixJudge(matrix, 7, 9, 4, 6))) throw 0;
  89.             if (!(submatrixJudge(matrix, 7, 9, 7, 9))) throw 0;
  90.         }//end try
  91.         catch(int){
  92.             cout << "no" << endl;
  93.             continue;
  94.         }// end catch
  95.  
  96.         cout << "yes" << endl;
  97.     }// end while
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement