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 相关文章推荐
JavaScript中的History历史对象
Jan 16 Javascript
js实现的捐赠管理完整实例
Jan 20 Javascript
js实现类似于add(1)(2)(3)调用方式的方法
Mar 04 Javascript
整理AngularJS中的一些常用指令
Jun 16 Javascript
js按条件生成随机json:randomjson实现方法
Apr 07 Javascript
JavaScript实现打地鼠小游戏
Apr 23 Javascript
Angular4学习笔记之准备和环境搭建项目
Aug 01 Javascript
vue 本地环境跨域请求proxyTable的方法
Sep 19 Javascript
layui清空,重置表单数据的实例
Sep 12 Javascript
NProgress显示顶部进度条效果及使用详解
Sep 21 Javascript
JavaScript实现左右滚动电影画布
Feb 06 Javascript
微信小程序实现点击生成随机验证码
Sep 09 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下批量挂马和批量清马代码
2011/02/27 PHP
php数组函数序列之array_slice() - 在数组中根据条件取出一段值,并返回
2011/11/07 PHP
php mail to 配置详解
2014/01/16 PHP
两级联动select刷新后其值保持不变的实现方法
2014/01/27 PHP
PHP+MYSQL实现读写分离简单实战
2017/03/13 PHP
TP5(thinkPHP5)框架基于ajax与后台数据交互操作简单示例
2018/09/03 PHP
利用jQuery 实现GridView异步排序、分页的代码
2010/02/06 Javascript
javascript 同时在IE和FireFox获取KeyCode的代码
2010/02/07 Javascript
jQuery)扩展jQuery系列之一 模拟alert,confirm(一)
2010/12/04 Javascript
关于query Javascript CSS Selector engine
2013/04/12 Javascript
js设置cookie过期及清除浏览器对应名称的cookie
2013/10/24 Javascript
JavaScript中解析JSON数据的三种方法
2015/07/03 Javascript
jquery.fastLiveFilter.js实现输入自动过滤的方法
2015/08/11 Javascript
js实现三张图(文)片一起切换的banner焦点图
2015/08/25 Javascript
javascript实现一个网页加载进度loading
2017/01/04 Javascript
React Native开发封装Toast与加载Loading组件示例
2018/09/08 Javascript
vue-cli安装使用流程步骤详解
2018/11/08 Javascript
jquery css实现流程进度条
2020/03/26 jQuery
关于vue3默认把所有onSomething当作v-on事件绑定的思考
2020/05/15 Javascript
vue项目或网页上实现文字转换成语音播放功能
2020/06/09 Javascript
Web服务器框架 Tornado简介
2014/07/16 Python
Python使用BeautifulSoup库解析HTML基本使用教程
2016/03/31 Python
python动态加载包的方法小结
2016/04/18 Python
python中快速进行多个字符替换的方法小结
2016/12/15 Python
Python爬虫框架scrapy实现downloader_middleware设置proxy代理功能示例
2018/08/04 Python
python实现将多个文件分配到多个文件夹的方法
2019/01/07 Python
windows系统中Python多版本与jupyter notebook使用虚拟环境的过程
2019/05/15 Python
Python使用指定端口进行http请求的例子
2019/07/25 Python
python GUI模拟实现计算器
2020/06/22 Python
世界上最大的家庭自动化公司:Smarthome
2017/12/20 全球购物
Harman Audio官方商店:购买JBL、Harman Kardon、Infinity和AKG
2019/12/05 全球购物
Haggar官网:美国男装品牌
2020/02/16 全球购物
UNIX文件系统常用命令
2012/05/25 面试题
和领导吃饭祝酒词
2015/08/11 职场文书
python 爬取京东指定商品评论并进行情感分析
2021/05/27 Python
Html5同时支持多端sdk的小技巧
2021/11/17 HTML / CSS