标签:分享 思路 span row images com == ret public
一、题目
1、描述
2、题意
根据数独规则添加数独缺失的元素
二、解答
1、思路:
回溯
// 37. Sudoku Solver public void solveSudoku(char[][] board) { helper(board); } private boolean helper(char[][] board) { for(int row = 0; row < 9; row++) { for(int col = 0; col < 9; col++) { if(board[row][col] == ‘.‘) { for(char i = ‘1‘; i <= ‘9‘; i++) { board[row][col] = i; if(isValidSudoku(board, row, col) && helper(board)) return true; // 还原 board[row][col] = ‘.‘; } return false; } } } return true; } public boolean isValidSudoku(char[][] board, int row, int col) { for(int i = 0; i < 9; i++) { if(i != col && board[row][i] == board[row][col]) // row return false; if(i != row && board[i][col] == board[row][col]) // colume return false; if(board[row / 3 * 3 + i / 3][col / 3 * 3 + i % 3] != ‘.‘ && board[row / 3 * 3 + i / 3][col / 3 * 3 + i % 3] == board[row][col]) // cube return false; } return true; }
标签:分享 思路 span row images com == ret public
原文地址:http://www.cnblogs.com/skillking/p/7111148.html