I've written in Python Sudoku solver and backtarcing problems... And in the second Layer soduko is disabled.
function find(){
for (let x=0;x<mat.length;x++){
let y
y = mat[x].findIndex((e)=>e===0)
if (y!=-1){
return [x,y]
}
}
}
function is_valid(x,y,num){
if (mat[x].filter(target => target===num).length >=2){
return 0
}
for(let i=0;i<mat.length;i++){
if (i === x){
continue
}
if (mat[i][y] === num ){
return 0
}
}
let X = x - x % 3
let Y = y - y % 3
let N = 0
for (let i =0;i<3;i++){
for (let j =0;j<3;j++){
if(mat[i+X][j+Y]== num){
N+=1
}
if (N==2){
return 0
}
}
}
return 1
}
function solve(){
try {
[x,y] = find()
}catch{
alert('solve')
return 1
}
for (let i = 1 ;i<10;i++){
mat[x][y] = i;
console.log(mat[x])
if (is_valid(x,y,i)){
solve()
}
}
mat[x][y]=0
}
solve()
What's my code problem? I want first put the value in the soduko and then check if the value is valid or not.. and solve it