Tuesday, December 24, 2013

Google Code Jam Notes - Sudoku Checker - Java

Problem:

Sudoku is a popular single player game. The objective is to fill a 9x9 matrix with digits so that each column, each row, and all 9 non-overlapping 3x3 sub-matrices contain all of the digits from 1 through 9. Each 9x9 matrix is partially completed at the start of game play and typically has a unique solution.
Given a completed N2xN2 Sudoku matrix, your task is to determine whether it is a validsolution. A valid solution must satisfy the following criteria:
  • Each row contains each number from 1 to N2, once each.
  • Each column contains each number from 1 to N2, once each.
  • Divide the N2xN2 matrix into N2 non-overlapping NxN sub-matrices. Each sub-matrix contains each number from 1 to N2, once each.
You don't need to worry about the uniqueness of the problem. Just check if the given matrix is a valid solution.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case starts with an integer N. The next N2 lines describe a completed Sudoku solution, with each line contains exactly N2 integers. All input integers are positive and less than 1000.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is "Yes" (quotes for clarity only) if it is a valid solution, or "No" (quotes for clarity only) if it is invalid. Note that the judge is case-sensitive, so answers of "yes" and "no" will not be accepted.

Limits

1 ≤ T ≤ 100.

Small dataset

N = 3.

Large dataset

3 ≤ N ≤ 6.

Sample


Input
 

Output
 
3
3
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
3
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
3
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 999 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
Case #1: Yes
Case #2: No
Case #3: No

Analysis:
Use Set to check whether each element is just appearing once either in a line, a row, or a subMatrix. Time Complexity O(N^2 * N^2).

My solution: (Your opinion is highly appreciated)


[java] view plaincopy
  1. package codeJam.google.com;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileWriter;  
  5. import java.io.IOException;  
  6. import java.util.HashSet;  
  7. import java.util.Scanner;  
  8.   
  9. /** 
  10.  * @author Zhenyi 2013 Dec 24, 2013 1:35:59 PM 
  11.  */  
  12. public class SudokuChecker {  
  13.     public static void main(String[] args) throws IOException {  
  14.         Scanner in = new Scanner(new File(  
  15.                 "C:/Users/Zhenyi/Downloads/A-small-practice.in"));  
  16.         FileWriter out = new FileWriter(  
  17.                 "C:/Users/Zhenyi/Downloads/A-small-practice.out");  
  18.         // Scanner in = new Scanner(new  
  19.         // File("C:/Users/Zhenyi/Downloads/A-large-practice.in"));  
  20.         // FileWriter out = new  
  21.         // FileWriter("C:/Users/Zhenyi/Downloads/A-large-practice.out");  
  22.   
  23.         int T = in.nextInt();  
  24.   
  25.         for (int cases = 1; cases <= T; cases++) {  
  26.             int N = in.nextInt();  
  27.             int[][] sArray = new int[N * N][N * N];  
  28.             for (int i = 0; i < N * N; i++) {  
  29.                 for (int j = 0; j < N * N; j++) {  
  30.                     sArray[i][j] = in.nextInt();  
  31.                 }  
  32.             }  
  33.   
  34.             HashSet<Integer> hs = new HashSet<Integer>();  
  35.             for (int i = 1; i <= N * N; i++) {  
  36.                 hs.add(i);  
  37.             }  
  38.   
  39.             boolean check = true;  
  40.             // horizontal  
  41.             for (int i = 0; i < N * N && check; i++) {  
  42.                 @SuppressWarnings("unchecked")  
  43.                 HashSet<Integer> tmp = (HashSet<Integer>) hs.clone();  
  44.                 for (int j = 0; j < N * N; j++) {  
  45.                     tmp.remove(sArray[i][j]);  
  46.                 }  
  47.                 if (tmp.size() != 0) {  
  48.                     check = false;  
  49.                 }  
  50.             }  
  51.   
  52.             // vertical  
  53.   
  54.             for (int j = 0; j < N * N && check; j++) {  
  55.                 @SuppressWarnings("unchecked")  
  56.                 HashSet<Integer> tmp = (HashSet<Integer>) hs.clone();  
  57.                 for (int i = 0; i < N * N; i++) {  
  58.                     tmp.remove(sArray[i][j]);  
  59.                 }  
  60.                 if (tmp.size() != 0) {  
  61.                     check = false;  
  62.                 }  
  63.             }  
  64.   
  65.             // subMatrix  
  66.             for (int iShift = 0; iShift < N * N && check; iShift += N) {  
  67.                 for (int jShift = 0; jShift < N * N && check; jShift += N) {  
  68.                     @SuppressWarnings("unchecked")  
  69.                     HashSet<Integer> tmp = (HashSet<Integer>) hs.clone();  
  70.                     for (int i = 0; i < N; i++) {  
  71.                         for (int j = 0; j < N; j++) {  
  72.                             tmp.remove(sArray[i + iShift][j + jShift]);  
  73.                         }  
  74.                     }  
  75.                     if (tmp.size() != 0) {  
  76.                         check = false;  
  77.                     }  
  78.                 }  
  79.             }  
  80.   
  81.             if (check) {  
  82.                 out.write("Case #" + cases + ": Yes\n");  
  83.             } else {  
  84.                 out.write("Case #" + cases + ": No\n");  
  85.             }  
  86.   
  87.         }  
  88.         in.close();  
  89.         out.flush();  
  90.         out.close();  
  91.     }  
  92. }  

No comments:

Post a Comment