Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells.

A sudoku solution must satisfy all of the following rules:

  1. Each of the digits 1-9 must occur exactly once in each row.
  2. Each of the digits 1-9 must occur exactly once in each column.
  3. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.

Empty cells are indicated by the character '.'.


A sudoku puzzle...


...and its solution numbers marked in red.

Note:

  • The given board contain only digits 1-9 and the character '.'.
  • You may assume that the given Sudoku puzzle will have a single unique solution.
  • The given board size is always 9x9.

	public static void main(String[] args) {

		 char[][] board = new char[][] { { '5', '3', '.', '.', '7', '.', '.', '.', '.'},
										 { '6', '.', '.', '1', '9', '5', '.', '.', '.' },
										 { '.', '9', '8', '.', '.', '.', '.', '6', '.' },
										 { '8', '.', '.', '.', '6', '.', '.', '.', '3' },
										 { '4', '.', '.', '8', '.', '3', '.', '.', '1' },
										 { '7', '.', '.', '.', '2', '.', '.', '.', '6' },
										 { '.', '6', '.', '.', '.', '.', '2', '8', '.' },
										 { '.', '.', '.', '4', '1', '9', '.', '.', '5' },
										 { '.', '.', '.', '.', '8', '.', '.', '7', '9' } };
//		char[][] board = new char[][]{{'.','.','9','7','4','8','.','.','.'},{'7','.','.','.','.','.','.','.','.'},{'.','2','.','1','.','9','.','.','.'},{'.','.','7','.','.','.','2','4','.'},{'.','6','4','.','1','.','5','9','.'},{'.','9','8','.','.','.','3','.','.'},{'.','.','.','8','.','3','.','2','.'},{'.','.','.','.','.','.','.','.','6'},{'.','.','.','2','7','5','9','.','.'}};

		print(board);
		solveSudoku(board);
		print(board);
	}

	private static void print(char[][] board) {
		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board.length; j++) {
				System.out.print(board[i][j] + " ");
			}
			System.out.println();
		}
		
		System.out.println("==========================================");
	}

	public static boolean isValidSudoku(char[][] board) {

		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board.length; j++) {
				if (board[i][j] != '.') {
					for (int i0 = i + 1; i0 < board.length; i0++) {
						if (board[i0][j] == board[i][j])
							return false;
					}
					for (int j0 = j + 1; j0 < board.length; j0++) {
						if (board[i][j0] == board[i][j])
							return false;
					}

					int gi = i / 3;
					for (int i0 = gi * 3; i0 / 3 == gi; i0++) {
						if (i0 == i)
							continue;
						int gj = j / 3;
						for (int j0 = gj * 3; j0 / 3 == gj; j0++) {
							if (j0 == j)
								continue;
							if (board[i0][j0] == board[i][j])
								return false;
						}
					}
				}
			}
		}

		return true;

	}
	
	public static boolean isValidSudoku(char[][] board, int i, int j) {
		for (int i0 = 0; i0 < board.length; i0++) {
			if(i0 == i)
				continue;
			
			if (board[i0][j] == board[i][j])
				return false;
		}
		for (int j0 = 0; j0 < board.length; j0++) {
			if(j0 == j)
				continue;
			if (board[i][j0] == board[i][j])
				return false;
		}

		int gi = i / 3;
		for (int i0 = gi * 3; i0 / 3 == gi; i0++) {
			if (i0 == i)
				continue;
			int gj = j / 3;
			for (int j0 = gj * 3; j0 / 3 == gj; j0++) {
				if (j0 == j)
					continue;
				if (board[i0][j0] == board[i][j])
					return false;
			}
		}
		return true;
	}

	public static char[][] solveSudoku(char[][] board) {

		if(!isValidSudoku(board)) {
			return null;
		}
		
		if(fillSudoku(board, 0, 0))
			return board;
		else
			return null;
	}

	private static boolean fillSudoku(char[][] board, int i, int j) {
		if(i == board.length && j == 0) {
			return true;
		}
		if (board[i][j] == '.') {
			char tryNum = '1';
			while (tryNum <= '9') {
				board[i][j] = tryNum;
				if(isValidSudoku(board, i, j)){
					if(j == board.length - 1) {
						if(fillSudoku(board, i + 1, 0)) {
							return true;
						}
					} else {
						if(fillSudoku(board, i, j + 1)) {
							return true;
						}
					}
				}
				tryNum ++;
			}
			board[i][j] = '.';
		} else {
			if(j == board.length - 1) {
				return fillSudoku(board, i + 1, 0);
			} else {
				return fillSudoku(board, i, j + 1);
			}
		}
		return false;
	}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李文区

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值