A sudoku verifier

public class SudokuVerifier {

	private boolean is1To9Row(int[][] a, int r) {

		int[] pos = new int[9];
		for (int i = 0; i < 9; i++) {
			if (pos[a[r][i]] == 1)
				return false;
			pos[a[r][i]] = 1;
		}
		for (int i : pos) {
			if (i == 0)
				return false;
		}
		return true;
	}

	private boolean is1To9Column(int[][] a, int c) {

		int[] pos = new int[9];
		for (int i = 0; i < 9; i++) {
			if (pos[a[i][c]] == 1)
				return false;
			pos[a[i][c]] = 1;
		}
		for (int i : pos) {
			if (i == 0)
				return false;
		}
		return true;
	}

	private boolean is1To9Sec(int[][] a, int x, int y)
	{
		int[] pos = new int[9];
		for (int i = 0 ; i < 3 ; i ++)
		{
			for (int j = 0 ; j < 3 ; j ++)
			{
				if (pos[a[x + i][x + j]] == 1)
					return false;
				pos[a[x + i][x + j]] = 1;
			}
		}
		for (int i : pos) {
			if (i == 0)
				return false;
		}
		return true;
	}

	// O(n)
	private boolean isValid(int[][] a)
	{
		// Assumes...

		for (int i = 0 ; i < 9 ; i ++)
		{
			// Rows
			if (!is1To9Row(a, i))
				return false;

			// Columns
			if (!is1To9Column(a, i))
				return false;
		}

		// Secs
		for (int i = 0 ; i < 9 ; i += 3)
		{
			for (int j = 0 ; j < 9 ; j += 3)
			{
				if (!is1To9Sec(a, i, j))
					return false;
			}
		}
		return true;
	}
}
时间: 2024-11-07 01:31:33

A sudoku verifier的相关文章

LeetCode37 Sudoku Solver

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red.  (Hard)

*Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. public clas

Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board (partially

[LeetCode]Valid Sudoku

检测数独是否合格. 思路: 填充一遍就知道是否合格. 基本暴力搜索的思想. 1 /*************************************************************************************************** 2 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 3 The Sudoku board could be parti

POJ Sudoku 数独填数 DFS

题目链接:Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18105   Accepted: 8772   Special Judge Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Fig

Valid Sudoku leetcode

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. 判断九宫格的合理性(并不一定有解),只需要依次判断行.列.9个子九宫格是否

LeetCode OJ:Valid Sudoku(有效数独问题)

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. 注意这里的有效数独并非指的是可以解出来,只要存在的数满足数独的条件就可以了. 原理很简单,但是判定在同一个blocks的时候出了点问题,没想到判定方法,看了下

Java [leetcode 37]Sudoku Solver

题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 解题思路:

2015南阳CCPC H - Sudoku 暴力

H - Sudoku Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller. Actually, Yi Sima was playing it different. F