DFS&BFS - 37. Sudoku Solver
时间:2022-07-25
本文章向大家介绍DFS&BFS - 37. Sudoku Solver,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
- 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:
- Each of the digits
1-9
must occur exactly once in each row. - Each of the digits
1-9
must occur exactly once in each column. - Each of the the digits
1-9
must occur exactly once in each of the 93x3
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
.
思路:
题目是让求解数组,就是每一行每一列每一个3*3的小方格不允许重复,做法和n皇后问题一样,就是使用dfs来枚举每一种填法,然后回溯。
代码:
go:
var col, row, box [][]bool
func initGrid(board [][]byte) {
col = make([][]bool, 10)
row = make([][]bool, 10)
box = make([][]bool, 10)
for i := range col {
col[i] = make([]bool, 10)
row[i] = make([]bool, 10)
box[i] = make([]bool, 10)
}
for i := 0; i < 9; i++ {
for j := 0; j < 9; j++ {
c := board[i][j]
if c != '.' {
col[j][c - '0'] = true
row[i][c - '0'] = true
box[j/3 + i/3*3][c - '0'] = true
}
}
}
}
func dfs(board [][]byte, x int, y int) bool {
if y == 9 {
return true
}
nextX := (x + 1 ) % 9
nextY := y
if nextX == 0 {
nextY = y + 1
}
if board[y][x] != '.' {
return dfs(board, nextX, nextY)
}
// 枚举
for num := byte(1); num <= 9; num++ {
if !col[x][num] && !row[y][num] && !box[x/3 + y/3*3][num] {
col[x][num] = true
row[y][num] = true
box[x/3 + y/3*3][num] = true
board[y][x] = num + '0'
if dfs(board, nextX, nextY) {
return true
}
col[x][num] = false
row[y][num] = false
box[x/3 + y/3*3][num] = false
board[y][x] = '.'
}
}
return false
}
func solveSudoku(board [][]byte) {
initGrid(board)
dfs(board, 0, 0)
}
- jsp中在href中传递参数
- Druid-目前最好的连接池
- 【算法】哈希表的诞生
- java中获得文件大小代码
- 【Java】泛型学习笔记
- 读写文件具体操作
- 【算法】赫夫曼树(Huffman)的构建和应用(编码、译码)
- ERROR 1396 (HY000): Operation CREATE USER faile...
- hive安装后测试
- 【算法】论平衡二叉树(AVL)的正确种植方法
- 【JavaScript】 JS面向对象的模式与实践
- log4j:ERROR Failed to load driver
- 前端MVC Vue2学习总结(七)——ES6与Module模块化、Vue-cli脚手架搭建、开发、发布项目与综合示例
- 前端MVC Vue2学习总结(六)——axios与跨域HTTP请求、Lodash工具库
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Python数据可视化 热力图
- python 中文情感分析 Snownlp库的使用
- Python数据分析实战(1) 成都土地市场分析
- 51单片机学习(1) LED点亮、闪烁以及流水灯实现
- python pyecharts地理数据可视化 绘制地理图表
- Python 爬虫+tkinter界面 实现历史天气查询
- python opencv+pytesseract 验证码识别
- python+opencv 实现图像人脸检测及视频中的人脸检测
- 手搓一个分布式大气监测系统(五)基于物联网开发平台的云架构延伸
- 关于Python的前后、单双下划线作用,看完这篇文章,吊打面试官!
- 图解EfficientNet模型的完整细节
- (⊙o⊙)?markdown文档中插入萌萌的emoji表情
- C语言中的字符串可以怎么处理?
- 一个简单的C语言测试框架
- Let's Encrypt实践指北