JS实现判断有效的数独算法示例


Posted in Javascript onFebruary 25, 2019

本文实例讲述了JS实现判断有效的数独算法。分享给大家供大家参考,具体如下:

判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。

1.数字 1-9 在每一行只能出现一次。
2.数字 1-9 在每一列只能出现一次。
3.数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。

JS实现判断有效的数独算法示例

数独部分空格内已填入了数字,空白格用 ‘.' 表示。

示例 1:

输入:

[
["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"]
]

输出: true

示例 2:

输入:

[
["8","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"]
]

输出: false

解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。

但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。

说明:

1.一个有效的数独(部分已被填充)不一定是可解的。
2.只需要根据以上规则,验证已经填入的数字是否有效即可。
3.给定数独序列只包含数字 1-9 和字符 ‘.' 。
4.给定数独永远是 9x9 形式的。

<script>
var isValidSudoku = function(board) {
 const boxes = [{}, {}, {}, {}, {}, {}, {}, {}, {}];
 const cols = [{}, {}, {}, {}, {}, {}, {}, {}, {}];
 const rows = [{}, {}, {}, {}, {}, {}, {}, {}, {}];
 let test = [{}, {}, {}, {}, {}, {}, {}, {}, {}];
 for (let i = 0; i < 9; i++) {
  for (let j = 0; j < 9; j++) {
   const digit = board[i][j];
   if (digit !== '.') {
    const k = Math.floor(j / 3) + (Math.floor(i / 3) * 3);
    if (boxes[k][digit] || cols[j][digit] || rows[i][digit]) {
     return false;
    }
    boxes[k][digit] = cols[j][digit] = rows[i][digit] = true;
   }
  }
 }
 return true;
};
const 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"]
]
console.log(isValidSudoku(board))
</script>

使用在线HTML/CSS/JavaScript代码运行工具:http://tools.3water.com/code/HtmlJsRun测试上述代码,可得如下运行结果:

JS实现判断有效的数独算法示例

希望本文所述对大家JavaScript程序设计有所帮助。

Javascript 相关文章推荐
使用jquery为table动态添加行的实现代码
Mar 30 Javascript
jQuery学习笔记 获取jQuery对象
Sep 19 Javascript
javascript获取元素偏移量的方法有哪些
Jun 24 Javascript
js判断浏览器是否支持html5
Aug 17 Javascript
jQuery中的jQuery()方法用法分析
Dec 27 Javascript
详解使用Vue.Js结合Jquery Ajax加载数据的两种方式
Jan 10 Javascript
easyui combogrid实现本地模糊搜索过滤多列
May 13 Javascript
谈谈对vue响应式数据更新的误解
Aug 01 Javascript
AngularJS中下拉框的高级用法示例
Oct 11 Javascript
一步一步的了解webpack4的splitChunk插件(小结)
Sep 17 Javascript
微信小程序公用参数与公用方法用法示例
Jan 09 Javascript
使用JavaScript获取扫码枪扫描得到的条形码的思路代码详解
Jun 10 Javascript
js实现通过开始结束控制的计时器
Feb 25 #Javascript
js实现倒计时器自定义时间和暂停
Feb 25 #Javascript
JS module的导出和导入的实现代码
Feb 25 #Javascript
js实现多个倒计时并行 js拼团倒计时
Feb 25 #Javascript
js实现网页同时进行多个倒计时功能
Feb 25 #Javascript
js实现一个页面多个倒计时的3种方法
Feb 25 #Javascript
Vue自定义指令上报Google Analytics事件统计的方法
Feb 25 #Javascript
You might like
数据库的日期格式转换
2006/10/09 PHP
php使用get_class_methods()函数获取分类的方法
2016/07/20 PHP
PHP实现微信公众号验证Token的示例代码
2019/12/16 PHP
阻止子元素继承父元素事件具体思路及实现
2013/05/02 Javascript
DOM基础教程之模型中的模型节点
2015/01/19 Javascript
jQuery+AJAX实现无刷新下拉加载更多
2015/07/03 Javascript
全面解析Bootstrap图片轮播效果
2015/12/03 Javascript
javascript正则表达式总结
2016/02/29 Javascript
JS获取当前脚本文件的绝对路径
2016/03/02 Javascript
Easyui Treegrid改变默认图标的方法
2016/04/29 Javascript
confirm确认对话框的实现方法总结
2016/06/17 Javascript
JavaScript中省略元素对数组长度的影响
2016/10/26 Javascript
老生常谈js数据类型
2017/08/03 Javascript
vue2使用keep-alive缓存多层列表页的方法
2018/09/21 Javascript
angularJs复选框checkbox选中进行ng-show显示隐藏的方法
2018/10/08 Javascript
webpack4+express+mongodb+vue实现增删改查的示例
2018/11/08 Javascript
AngularJS上传文件的示例代码
2018/11/10 Javascript
JavaScript设计模式之观察者模式实例详解
2019/01/16 Javascript
深入探讨JavaScript的最基本部分之执行上下文
2019/02/12 Javascript
从0到1搭建Element的后台框架的方法步骤
2019/04/10 Javascript
JS实现按比例缩小图片宽高
2020/08/24 Javascript
[03:36]2015国际邀请赛第二日现场精彩集锦
2015/08/06 DOTA
Python卸载模块的方法汇总
2016/06/07 Python
详解python中sort排序使用
2019/03/23 Python
selenium+PhantomJS爬取豆瓣读书
2019/08/26 Python
matplotlib 对坐标的控制,加图例注释的操作
2020/04/17 Python
详解如何在PyCharm控制台中输出彩色文字和背景
2020/08/17 Python
Django-Scrapy生成后端json接口的方法示例
2020/10/06 Python
CSS3之边框多颜色Border-color属性使用示例
2013/10/11 HTML / CSS
Spartoo瑞典:鞋子、包包和衣服
2018/09/15 全球购物
英国手机零售商:Metrofone
2019/03/18 全球购物
邮政员工辞职信
2014/01/16 职场文书
授权收款委托书
2014/09/23 职场文书
php TP5框架生成二维码链接
2021/04/01 PHP
一文读懂go中semaphore(信号量)源码
2021/04/03 Golang
python三子棋游戏
2022/05/04 Python