邓心一 6 anos atrás
pai
commit
bfdf3de92a
1 arquivos alterados com 51 adições e 1 exclusões
  1. 51 1
      hard/37.go

+ 51 - 1
hard/37.go

@@ -1,7 +1,57 @@
 package main
 
-func solveSudoku(board [][]byte) {
+// IntMap ...
+type IntMap []int
+
+func (im IntMap) set(id, num int) {
+	im[id] |= 1 << uint(num)
+}
+
+func (im IntMap) get(id, num int) bool {
+	return im[id]&(1<<uint(num)) == 1
+}
+
+func getAnswer(occur int) (ans byte, ok bool) {
+	cnt := 0
+	for i, bit := 1, 1; i <= 9; i++ {
+		if occur&bit == 0 {
+			cnt++
+			ans = byte(i + '0')
+		}
+		bit <<= 1
+	}
+	return ans, cnt == 1
+}
 
+func solveSudoku(board [][]byte) {
+	var rowMap, colMap, blockMap IntMap
+	rowMap = make([]int, 9)
+	colMap = make([]int, 9)
+	blockMap = make([]int, 9)
+	for y, row := range board {
+		for x, col := range row {
+			if col == '.' {
+				continue
+			}
+			num := int(col - '0')
+			rowMap.set(y, num)
+			colMap.set(x, num)
+			blockMap.set(y/3*3+x/3, num)
+		}
+	}
+	for y, row := range board {
+		for x, col := range row {
+			if col != '.' {
+				continue
+			}
+			blockID := y/3*3 + x/3
+			occur := rowMap[y] & colMap[x] & blockMap[blockID]
+			ans, ok := getAnswer(occur)
+			if ok {
+				board[y][x] = ans
+			}
+		}
+	}
 }
 
 func main() {