Javascript 如何动态查找数独子框值
我正在尝试做一个算法来动态检查一个常规数独网格是否被解决。我有一个硬编码的解决方案,通过使用框内所需值的坐标来获得每个子框的值。然而,我似乎不知道如何编写一个函数,仅仅通过观察数独矩阵的大小,以及我想要的方框,它就会输出方框部分中的值 我的代码:Javascript 如何动态查找数独子框值,javascript,dynamic-programming,sudoku,Javascript,Dynamic Programming,Sudoku,我正在尝试做一个算法来动态检查一个常规数独网格是否被解决。我有一个硬编码的解决方案,通过使用框内所需值的坐标来获得每个子框的值。然而,我似乎不知道如何编写一个函数,仅仅通过观察数独矩阵的大小,以及我想要的方框,它就会输出方框部分中的值 我的代码: const sudoku = [ [1, 2, 3, 4], [3, 4, 1, 2], [2, 3, 4, 1], [4, 1, 2, 3] ] class SudokuChecker { getCheckList (leng
const sudoku = [
[1, 2, 3, 4],
[3, 4, 1, 2],
[2, 3, 4, 1],
[4, 1, 2, 3]
]
class SudokuChecker {
getCheckList (length) {
const nums = {}
for (let i=1; i<=length; i++) nums[i] = 0
return nums
}
isCorrect (list) {
const checkList = this.getCheckList(list.length)
list.forEach(num => checkList[num]++)
return Object.values(checkList).every(x => x == 1)
}
getRow (matrix, row) {
return matrix[row]
}
getColumn (matrix, col) {
return matrix.map(row => row[col])
}
getBox (matrix, box) {
const procNum = Math.sqrt(matrix.length)
// given some box ID number (starting at 0), all I need :
// a) index of that box's top row
const topRow = 'find_me' // calculated with "box" and procNum somehow...
// b) the index of that box's first num at that row.
const sliceFrom = 'find_me' // no idea at the moment how to calculate...
let boxNums = []
for (let i=0; i<procNum; i++) {
boxNums = boxNums.concat(matrix[topRow + i].slice(sliceFrom, sliceFrom + procNum))
}
return boxNums
}
checkLists (matrix, type, unit=0) {
const pass = this.isCorrect(this['get'+type](matrix, unit))
if (!pass) return false
if (unit == matrix.length-1) return pass
return this.checkLists(matrix, type, ++unit)
}
checkAll (matrix) {
const types = ['Row', 'Column', 'Box']
return types.map(t => this.checkLists(matrix, t)).every(x => x === true)
}
}
const数独=[
[1, 2, 3, 4],
[3, 4, 1, 2],
[2, 3, 4, 1],
[4, 1, 2, 3]
]
类SudokuChecker{
检查清单(长度){
常量nums={}
for(设i=1;i检查表[num]++)
返回Object.values(检查表).every(x=>x==1)
}
getRow(矩阵,行){
返回矩阵[行]
}
getColumn(矩阵,列){
返回矩阵.map(行=>行[col])
}
getBox(矩阵,box){
const procNum=Math.sqrt(matrix.length)
//给定某个盒子ID号(从0开始),我所需要的是:
//a)该框顶行的索引
const topRow='find_me'//以某种方式使用“box”和procNum计算。。。
//b)该框在该行的第一个num的索引。
const sliceFrom='find_me'//目前不知道如何计算。。。
设boxNums=[]
对于(设i=0;i这个.检查表(矩阵,t)).every(x=>x==true)
}
}
我在朋友的帮助下得到的答案:
getBox (matrix, box) {
const procNum = Math.sqrt(matrix.length)
const topRow = Math.floor(box/procNum) * procNum
const sliceFrom = (box % procNum) * procNum
let boxNums = []
for (let i=0; i<procNum; i++) {
boxNums = boxNums.concat(matrix[topRow + i].slice(sliceFrom, sliceFrom + procNum))
}
return boxNums
}
getBox(矩阵,框){
const procNum=Math.sqrt(matrix.length)
const topRow=数学楼层(方框/程序)*procNum
常量sliceFrom=(框%procNum)*procNum
设boxNums=[]
对于(让我=0;我在朋友的帮助下得到的答案:
getBox (matrix, box) {
const procNum = Math.sqrt(matrix.length)
const topRow = Math.floor(box/procNum) * procNum
const sliceFrom = (box % procNum) * procNum
let boxNums = []
for (let i=0; i<procNum; i++) {
boxNums = boxNums.concat(matrix[topRow + i].slice(sliceFrom, sliceFrom + procNum))
}
return boxNums
}
getBox(矩阵,框){
const procNum=Math.sqrt(matrix.length)
const topRow=数学楼层(方框/程序)*procNum
常量sliceFrom=(框%procNum)*procNum
设boxNums=[]
for(设i=0;i