编写一个程序,通过填充空格来解决数独问题。
数独的解法需 遵循如下规则:
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
数独部分空格内已填入了数字,空白格用 ‘.’ 表示。
示例 1:
输入:board = [[“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”]]
输出:[[“5”,“3”,“4”,“6”,“7”,“8”,“9”,“1”,“2”],[“6”,“7”,“2”,“1”,“9”,“5”,“3”,“4”,“8”],[“1”,“9”,“8”,“3”,“4”,“2”,“5”,“6”,“7”],[“8”,“5”,“9”,“7”,“6”,“1”,“4”,“2”,“3”],[“4”,“2”,“6”,“8”,“5”,“3”,“7”,“9”,“1”],[“7”,“1”,“3”,“9”,“2”,“4”,“8”,“5”,“6”],[“9”,“6”,“1”,“5”,“3”,“7”,“2”,“8”,“4”],[“2”,“8”,“7”,“4”,“1”,“9”,“6”,“3”,“5”],[“3”,“4”,“5”,“2”,“8”,“6”,“1”,“7”,“9”]]
解释:输入的数独如上图所示,唯一有效的解决方案如下所示:
提示:
board.length == 9
board[i].length == 9
board[i][j] 是一位数字或者 ‘.’
题目数据 保证 输入数独仅有一个解
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/sudoku-solver
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法:
首先这种棋盘搜索、摆放的问题,大都是深度搜索或者回溯,关键看怎么剪枝。
对于这个题,我不知道直接暴力搜索能不能过,但我能想到的剪枝有下面2个。
剪枝1:根据行,列,所在方块已经出现的数字进行剪枝。因为一个格子最多有1~9中填法,数独游戏是行列所在方块不能有重复,所以很容易想到,先把已经出现过的数字剔除,降低搜索空间。【这个剪枝大都能想得到】
剪枝2:根据剪枝1的思路,可以发现对于有些格子,他的可能填充的数字只剩下一个了【不可能一个没有,因为题目保证有解。而且因为题目保证有唯一解,所以会有大量格子都只有一个填充的数字了】比如样例输入当中坐标为(4,4)【即:输入正中央,坐标用0开始】的地方就只能填5。
两个剪枝怎么用:
首先,我们根据剪枝1,将棋盘当中所有格子的可行解全部计算出,然后根据剪枝2,将棋盘的所有只有唯一一种填法的位置直接填充。
这时注意,我们又可以进行剪枝1,进一步降低搜索空间,执行完剪枝1,又可以执行剪枝2。
是的,我们可以递归调用剪枝1和剪枝2,直到整个棋盘剩下未填写的格子都没有唯一可行解的状态。此时我们的搜索空间已经大大降低。
接下来我们再使用DFS,枚举一个空位的所有可行方案,看下填哪个数值的时候是可行的。因为题目必有解,所以只需要对任意空位进行枚举可行解即可。同时枚举一个完一个数字之后,可以立即使用上述的两个剪枝,如果发现存在一个点的可行解方案数为0,说明当前位置枚举的值不可能有解,不要再往下尝试其他空位了,直接换下一个数字填进来。
其他的可以看一下代码,我用的java,耗时3MS:
import java.util.HashSet;
class Solution
{
private boolean baseFill(int board[][])
{
boolean loop=false;
for(int i=0;i<9;++i)
for(int j=0;j<9;++j)
if(board[i][j]==0)
{
int[] feasibleNumbers=feasibleSearch(board,i,j);
if(feasibleNumbers.length==1)
{
board[i][j]=feasibleNumbers[0];
loop=true;
}
else if(feasibleNumbers.length==0)
{
return false;
}
}
if(loop)
return baseFill(board);
return true;
}
private int[][] transformBoard(char[][] board)
{
int[][] intBoard=new int[9][9];
for(int i=0;i<9;++i)
for(int j=0;j<9;++j)
{
if(board[i][j]!='.')
intBoard[i][j]=board[i][j]-'0';
else
intBoard[i][j]=0;
}
return intBoard;
}
private int[] feasibleSearch(int[][] board,int x,int y)
{
HashSet<Integer> feasibleSet=new HashSet<>();
for(int i=1;i<=9;++i)
feasibleSet.add(i);
for(int i=0;i<9;++i)
{
int colRemove=board[x][i];
int rowRemove=board[i][y];
if(colRemove!=0)
feasibleSet.remove(colRemove);
if(rowRemove!=0)
feasibleSet.remove(rowRemove);
}
int blockX=x/3*3,blockY=y/3*3;
for(int i=blockX;i<blockX+3;++i)
{
for(int j=blockY;j<blockY+3;++j)
{
int blockRemove=board[i][j];
if(blockRemove!=0)
feasibleSet.remove(blockRemove);
}
}
int[] feasibleNumbers=new int[feasibleSet.size()];
int pos=0;
for(Integer num:feasibleSet)
{
feasibleNumbers[pos]=num;
pos=pos+1;
}
return feasibleNumbers;
}
private void copyBoard(int[][] orcBoard,int[][] destBoard)
{
for(int i=0;i<9;++i)
for(int j=0;j<9;++j)
destBoard[i][j]=orcBoard[i][j];
}
private boolean deepFill(int board[][])
{
int[][] tempBoard=new int[9][9];
for(int i=0;i<9;++i)
{
for(int j=0;j<9;++j)
{
if(board[i][j]==0)
{
int[] feasibleNumbers=feasibleSearch(board,i,j);
//因为题目保证有解,所以这里不可能所有可行解都不能走
for(int k=0;k<feasibleNumbers.length;++k)
{
this.copyBoard(board,tempBoard);
tempBoard[i][j]=feasibleNumbers[k];
if(baseFill(tempBoard))
{
if(deepFill(tempBoard))
{
this.copyBoard(tempBoard,board);
return true;
}
}
}
return false;
}
}
}
return true;
}
public void solveSudoku(char[][] board)
{
int[][] intBoard=this.transformBoard(board);
this.baseFill(intBoard);
this.deepFill(intBoard);
for(int i=0;i<9;++i)
for(int j=0;j<9;++j)
board[i][j]=(char)('0'+intBoard[i][j]);
}
}