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 相关文章推荐
js静态作用域的功能。
Dec 25 Javascript
让div层随鼠标移动的实现代码 ie ff
Dec 18 Javascript
javascript学习笔记(五)正则表达式
Apr 08 Javascript
js sort 二维数组排序的用法小结
Jan 24 Javascript
JavaScript简单实现鼠标拖动选择功能
Mar 06 Javascript
JavaScript代码判断点击第几个按钮
Dec 13 Javascript
不得不分享的JavaScript常用方法函数集(下)
Dec 25 Javascript
三种带箭头提示框总结实例
Jun 14 Javascript
原生ajax处理json格式数据的实例代码
Dec 25 Javascript
JavaScript显式数据类型转换详解
Mar 18 Javascript
vue微信分享插件使用方法详解
Feb 18 Javascript
解决Vue keep-alive 调用 $destory() 页面不再被缓存的情况
Oct 30 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
PHP 第二节 数据类型之转换
2012/04/28 PHP
Zend Guard使用指南及问题处理
2015/01/07 PHP
js常见表单应用技巧
2008/01/09 Javascript
javascript当onmousedown、onmouseup、onclick同时应用于同一个标签节点Element
2010/01/05 Javascript
从零开始学习jQuery (十) jQueryUI常用功能实战
2011/02/23 Javascript
JS定时刷新页面及跳转页面的方法
2013/07/04 Javascript
JavaScript中for-in遍历方式示例介绍
2014/02/11 Javascript
DOM 事件流详解
2015/01/20 Javascript
JavaScript中的条件判断语句使用详解
2015/06/03 Javascript
jquery实现的淡入淡出下拉菜单效果
2015/08/25 Javascript
由简入繁实现Jquery树状结构的方法(推荐)
2016/06/10 Javascript
JS常用函数和常用技巧小结
2016/10/15 Javascript
jquery插件treegrid树状表格的使用方法详解(.Net平台)
2017/01/03 Javascript
jQuery插件FusionCharts实现的2D柱状图效果示例【附demo源码下载】
2017/03/06 Javascript
js表单序列化判断空值的实例
2017/09/22 Javascript
Vue-Router实现组件间跳转的三种方法
2017/11/07 Javascript
利用JQUERY实现多个AJAX请求等待的实例
2017/12/14 jQuery
小程序获取周围IBeacon设备的方法
2018/10/31 Javascript
Element中的Cascader(级联列表)动态加载省\市\区数据的方法
2019/03/27 Javascript
对Layer弹窗使用及返回数据接收的实例详解
2019/09/26 Javascript
Python datetime时间格式化去掉前导0
2014/07/31 Python
Python socket C/S结构的聊天室应用实现
2014/11/30 Python
通过Python来使用七牛云存储的方法详解
2015/08/07 Python
使用Python下载歌词并嵌入歌曲文件中的实现代码
2015/11/13 Python
python写入文件自动换行问题的方法
2019/07/05 Python
Python编译为二进制so可执行文件实例
2019/12/23 Python
python单元测试框架pytest的使用示例
2020/10/07 Python
利用CSS3实现平移动画效果示例代码
2016/10/12 HTML / CSS
美国饼干礼物和美食甜点购买网站:Cheryl’s
2020/05/28 全球购物
linux下进程间通信的方式
2014/12/23 面试题
创先争优标语
2014/06/27 职场文书
总经理助理岗位职责范本
2014/07/20 职场文书
小学生一分钟演讲稿
2014/08/26 职场文书
云台山导游词
2015/02/03 职场文书
新学期小学班主任工作计划
2019/06/21 职场文书
mysql中DCL常用的用户和权限控制
2022/03/31 MySQL