Project Euler #96: Su Doku
Question
Answer : 24702
Hacker Rank Problem
Solution
Su Doku (Japanese meaning number place) is the name given to a
popular puzzle concept. Its origin is unclear, but credit must be
attributed to Leonhard Euler who invented a similar, and much more
difficult, puzzle idea called Latin Squares. The objective of Su Doku
puzzles, however, is to replace the blanks (or zeros) in a 9 by 9 grid
in such that each row, column, and 3 by 3 box contains each of the
digits 1 to 9. Below is an example of a typical starting puzzle grid and
its solution grid.
A well constructed Su Doku puzzle has a unique solution and can be
solved by logic, although it may be necessary to employ "guess and test"
methods in order to eliminate options (there is much contested opinion
over this). The complexity of the search determines the difficulty of
the puzzle; the example above is considered easy because it can be solved by straight forward direct deduction.
The 6K text file, sudoku.txt (right click and 'Save Link/Target As...'), contains fifty different Su Doku puzzles ranging in difficulty, but all with unique solutions (the first puzzle in the file is the example above).
By solving all fifty puzzles find the sum of the 3-digit numbers found in the top left corner of each solution grid; for example, 483 is the 3-digit number found in the top left corner of the solution grid above.
|
|
The 6K text file, sudoku.txt (right click and 'Save Link/Target As...'), contains fifty different Su Doku puzzles ranging in difficulty, but all with unique solutions (the first puzzle in the file is the example above).
By solving all fifty puzzles find the sum of the 3-digit numbers found in the top left corner of each solution grid; for example, 483 is the 3-digit number found in the top left corner of the solution grid above.
Answer : 24702
Hacker Rank Problem
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import java.util.Scanner; public class Solution { private static final int EMPTY = 0; private static boolean solve(int[][] board) { for(int y = 0; y < 9; y++) { for(int x = 0; x < 9; x++) { if(board[x][y] == EMPTY) { boolean[] available = new boolean[10]; for(int i = 1; i < available.length; i++) { available[i] = true; } for(int i = 0; i < 9; i++) { if(board[i][y] != EMPTY) { available[board[i][y]] = false; } if(board[x][i] != EMPTY) { available[board[x][i]] = false; } } int rx = (x / 3) * 3; int ry = (y / 3) * 3; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { if(board[i + rx][j + ry] != EMPTY) { available[board[i + rx][j + ry]] = false; } } } for(int i = 1; i <= 9; i++) { if(available[i]) { board[x][y] = i; if(solve(board)) { return true; } } } board[x][y] = EMPTY; return false; } } } return true; } public static void main(String[] args) { try(Scanner sc = new Scanner(System.in)) { int[][] board = new int[9][9]; for(int y = 0; y < 9; y++) { char[] data = sc.nextLine().toCharArray(); for(int x = 0; x < 9; x++) { board[x][y] = data[x] - '0'; } } solve(board); for(int y = 0; y < 9; y++) { for(int x = 0; x < 9; x++) { System.out.print(board[x][y]); } System.out.println(); } } } } |
Comments
Post a Comment