From 1d4187b8c7eec98c4491b8b6ab88ed9b9f2fe037 Mon Sep 17 00:00:00 2001 From: baici1 <249337001@qq.com> Date: Thu, 16 Sep 2021 19:46:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A037.=E8=A7=A3=E6=95=B0?= =?UTF-8?q?=E7=8B=AC=EF=BC=8Cgo=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0037.解数独.md | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/problems/0037.解数独.md b/problems/0037.解数独.md index b6fa0d6e..48e1f2f2 100644 --- a/problems/0037.解数独.md +++ b/problems/0037.解数独.md @@ -376,6 +376,61 @@ class Solution: Go: +```go +func solveSudoku(board [][]byte) { + var backtracking func(board [][]byte) bool + backtracking=func(board [][]byte) bool{ + for i:=0;i<9;i++{ + for j:=0;j<9;j++{ + //判断此位置是否适合填数字 + if board[i][j]!='.'{ + continue + } + //尝试填1-9 + for k:='1';k<='9';k++{ + if isvalid(i,j,byte(k),board)==true{//如果满足要求就填 + board[i][j]=byte(k) + if backtracking(board)==true{ + return true + } + board[i][j]='.' + } + } + return false + } + } + return true + } + backtracking(board) +} +//判断填入数字是否满足要求 +func isvalid(row,col int,k byte,board [][]byte)bool{ + for i:=0;i<9;i++{//行 + if board[row][i]==k{ + return false + } + } + for i:=0;i<9;i++{//列 + if board[i][col]==k{ + return false + } + } + //方格 + startrow:=(row/3)*3 + startcol:=(col/3)*3 + for i:=startrow;i