Solve Sudoku on the basis of the given irregular regions | Grid dimension ; Function to check if the number to be present in the current cell is safe or not ; Check if the number is present in i - th row or j - th column or not ; Check if the number to be filled is safe in current region or not ; Initialize the queue for the BFS ; Insert the current cell into queue ; Check if the neighbours cell is visited or not ; Mark current cell is visited ; Performing the BFS technique Checking for 4 neighbours at a time ; Stores front element of the queue ; Pop top element of the queue ; Check for neighbours cell ; If already contains the same number ; Mark as neighbour cell as visited ; Checking for 2 nd neighbours ; If neighbours contains the same number ; Insert neighbour cell into queue ; Mark neighbour cell as visited ; Checking for 3 rd neighbours ; If neighbours contains the same number ; Insert neighbour cell into queue ; Mark neighbour cell as visited ; Checking for 4 th neighbours ; If neighbours contains the same number ; Insert neighbour cell into queue ; Mark neighbour cell as visited ; Recursive function to solve the sudoku ; If the given sudoku already solved ; Prlet the solution of sudoku ; If the numbers in the current row already filled ; If current cell is not empty ; Iterate over all possible value of numbers ; If placing the current number is safe in the current cell ; Update sudoku [ i ] [ j ] ; Fill the ramining cells of the sudoku ; If remaining cells has been filled ; Otherwise No Solution ; Given sudoku array ; Given region array ; Function call ; No answer exist
const N = 2 ; function issafe ( sudoku , i , j , n , number , region ) { for ( let x = 0 ; x < n ; x ++ ) { if ( sudoku [ x ] [ j ] == number sudoku [ i ] [ x ] == number ) { return false ; } } let r = region [ i ] [ j ] ; let q = [ ] ; q . push ( [ i , j ] ) ; let visited = new Array ( N ) . fill ( 0 ) . map ( ( ) => new Array ( N ) . fill ( 0 ) ) ; visited [ i ] [ j ] = 1 ; while ( ! q . length ) { let front = q . front ( ) ; q . pop ( ) ; if ( front [ 0 ] + 1 < N && region [ front [ 0 ] + 1 ] [ front [ 1 ] ] == r && ! visited [ front [ 0 ] + 1 ] [ front [ 1 ] ] ) { if ( sudoku [ front [ 0 ] + 1 ] [ front [ 1 ] ] == number ) { return false ; } q . push ( [ front [ 0 ] + 1 , front [ 1 ] ] ) ; visited [ front [ 0 ] + 1 ] [ front [ 1 ] ] = 1 ; } if ( front [ 0 ] - 1 >= 0 && region [ front [ 0 ] - 1 ] [ front [ 1 ] ] == r && ! visited [ front [ 0 ] - 1 ] [ front [ 1 ] ] ) { if ( sudoku [ front [ 0 ] - 1 ] [ front [ 1 ] ] == number ) { return false ; } q . push ( [ front [ 0 ] - 1 , front [ 1 ] ] ) ; visited [ front [ 0 ] - 1 ] [ front [ 1 ] ] = 1 ; } if ( front [ 1 ] + 1 < N && region [ front [ 0 ] ] [ front [ 1 ] + 1 ] == r && ! visited [ front [ 0 ] ] [ front [ 1 ] + 1 ] ) { if ( sudoku [ front [ 0 ] ] [ front [ 1 ] + 1 ] == number ) { return false ; } q . push ( [ front [ 0 ] , front [ 1 ] + 1 ] ) ; visited [ front [ 0 ] ] [ front [ 1 ] + 1 ] = 1 ; } if ( front [ 1 ] - 1 >= 0 && region [ front [ 0 ] ] [ front [ 1 ] - 1 ] == r && ! visited [ front [ 0 ] ] [ front [ 1 ] - 1 ] ) { if ( sudoku [ front [ 0 ] ] [ front [ 1 ] - 1 ] == number ) { return false ; } q . push ( [ front [ 0 ] , front [ 1 ] - 1 ] ) ; visited [ front [ 0 ] ] [ front [ 1 ] - 1 ] = 1 ; } } return true ; } function solveSudoku ( sudoku , i , j , n , region ) { if ( i == n ) { for ( let a = 0 ; a < n ; a ++ ) { for ( let b = 0 ; b < n ; b ++ ) { document . write ( sudoku [ a ] [ b ] + " " ) ; } document . write ( " " ) ; } return true ; } if ( j == n ) { return solveSudoku ( sudoku , i + 1 , 0 , n , region ) ; } if ( sudoku [ i ] [ j ] != 0 ) { return solveSudoku ( sudoku , i , j + 1 , n , region ) ; } else { for ( let number = 1 ; number <= n ; number ++ ) { if ( issafe ( sudoku , i , j , n , number , region ) ) { sudoku [ i ] [ j ] = number ; let rest = solveSudoku ( sudoku , i , j + 1 , n , region ) ; if ( rest == true ) { return true ; } } } sudoku [ i ] [ j ] = 0 ; return false ; } } let sudoku = [ [ 0 , 1 ] , [ 0 , 0 ] , ] ; let region = [ [ " " , " " ] , [ " " , " " ] , ] ; let ans = solveSudoku ( sudoku , 0 , 0 , N , region ) ; if ( ans == 0 ) { document . write ( cout + " " ) ; }