Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.lang.*;
- import java.util.*;
- /**
- * Write a program to perform verification for a Sudoku board.
- *
- * @author your name here
- * @version 0.1
- */
- public class DefectiveSudoku
- {
- /**
- * Determine if a Sudoku board is in a legal configuration.
- * The board may be partially complete, in which case empty
- * cells are represented by a dot (.).
- * @param board a string containing the symbols in a 9x9 or
- * 16x16 Sudoku board in row-major order.
- * @pre board.length() is 81 or 256
- * @pre board 9x9 board uses symbols 1-9, 16x16 board uses A-P (uppercase).
- * @return true if the board configuration is legal, false otherwise.
- * That is, no row,
- * column or square contains a duplicate symbol.
- */
- public boolean verify(String board)
- {
- if (board.length() == 81 || board.length() == 256)
- {
- int save = board.length();
- int stop = 0;
- int k = 0;
- save = (int) Math.sqrt(save);
- for (int i = save; i < save * save + save; i = i + save)
- {
- if (!verifyNumberOnce(board.substring(i - save, i), save))
- {
- return false;
- }
- }
- for (int i = 0; i < save; i++)
- {
- String boardCol = "";
- for (int j = i; j < save * save; j = j + save)
- {
- boardCol = boardCol + board.charAt(j);
- }
- if (!verifyNumberOnce(boardCol, save))
- {
- return false;
- }
- }
- if (save == 9)
- {
- if (!verifyNine(board))
- {
- return false;
- }
- }
- else if (save == 16)
- {
- if (!verifySixteen(board))
- {
- return false;
- }
- }
- }
- else
- {
- return false;
- }
- return true;
- }
- /**
- * When Sudoku board is 9x9.
- *
- * @pre The data holding the data for the boxes part of the Sudoku.
- */
- public boolean verifyNine(String board)
- {
- for (int i = 0; i < 63; i = i + 3)
- {
- if (i == 9) i = 27;
- if (i == 36) i = 54;
- String boardThrees = "";
- boardThrees = boardThrees + board.charAt(i)
- + board.charAt(i + 1) + board.charAt(i + 2);
- boardThrees = boardThrees + board.charAt(i + 9)
- + board.charAt(i + 1 + 9) + board.charAt(i + 2 + 9);
- boardThrees = boardThrees + board.charAt(i + 18)
- + board.charAt(i + 1 + 18) + board.charAt(i + 2 + 18);
- if (verifyNumberOnce(boardThrees, 9))
- {
- return true;
- }
- }
- return true;
- }
- /**
- * When Sudoku board is 16x16.
- *
- * @pre The data holding the data for the boxes part of the Sudoku.
- */
- public boolean verifySixteen(String board)
- {
- for (int i = 0; i < 208; i = i + 4)
- {
- if (i == 16)
- {
- i = 64;
- }
- if (i == 80)
- {
- i = 128;
- }
- if (i == 144)
- {
- i = 192;
- }
- String boardFours = "";
- boardFours = boardFours + board.charAt(i)
- + board.charAt(i + 1) + board.charAt(i + 2) + board.charAt(i + 3);
- boardFours = boardFours + board.charAt(i + 16)
- + board.charAt(i + 1 + 16) + board.charAt(i + 2 + 16)
- + board.charAt(i + 3 + 16);
- boardFours = boardFours + board.charAt(i + 32)
- + board.charAt(i + 1 + 32) + board.charAt(i + 2 + 32)
- + board.charAt(i + 3 + 32);
- boardFours = boardFours + board.charAt(i + 48)
- + board.charAt(i + 1 + 48) + board.charAt(i + 2 + 48)
- + board.charAt(i + 3 + 48);
- if (!verifyNumberOnce(boardFours, 16))
- {
- return false;
- }
- }
- return true;
- }
- /**
- * When Sudoku board is split up. Check to see if split up part is valid.
- *
- * @pre The data holding the data for the split part of the Sudoku.
- */
- public boolean verifyNumberOnce(String board, int save)
- {
- for (int i = 0; i < board.length(); i++)
- {
- for (int j = 0; j < board.length(); j++)
- {
- if (board.charAt(i) == board.charAt(j)
- && board.charAt(j) != '.'
- && i != j)
- {
- return false;
- }
- }
- }
- return true;
- }
- /**
- * A local main for console testing.
- * Get the data to calculate if Sudoku is valid.
- *
- * @pre The data holding the data of the Sudoku.
- */
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- while (sc.hasNextLine())
- {
- String str = sc.nextLine();
- DefectiveSudoku app = new DefectiveSudoku();
- System.out.println(app.verify(str));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement