Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
¿Cómo imprimir "-1" si sudoku no tiene solución?

Resolví el sudoku usando JavaScript pero quiero imprimir -1 si el sudoku dado no tiene solución. Lo he hecho usando recursividad y he agotado todas las formas que se me ocurrieron. Por favor, ayúdame a resolver esta pregunta para los sudokos irresolubles.

 let row = 0; let col = 0; let matrix = [ [0, 4, 0, 0, 0, 0, 1, 7, 9], [0, 0, 2, 0, 0, 8, 0, 5, 4], [0, 0, 6, 0, 0, 5, 0, 0, 8], [0, 8, 0, 0, 7, 0, 9, 1, 0], [0, 5, 0, 0, 9, 0, 0, 3, 0], [0, 1, 9, 0, 6, 0, 0, 4, 0], [3, 0, 0, 4, 0, 0, 7, 0, 0], [5, 7, 0, 1, 0, 0, 2, 0, 0], [9, 2, 8, 0, 0, 0, 0, 6, 0] ]; function sudoku(matrix, row, col) { if (row == 9) { console.log(matrix); return; } let next_row = 0; let next_col = 0; if (col == 8) { next_col = 0; next_row = row + 1; } else { next_col = col + 1; next_row = row; } if (matrix[row][col] != 0) { sudoku(matrix, next_row, next_col); } else { for (let i = 0; i <= 9; i++) { if (isSafe(matrix, row, col, i) == true) { matrix[row][col] = i; sudoku(matrix, next_row, next_col); matrix[row][col] = 0; } } } } function isSafe(matrix, row, col, value) { for (let i = 0; i < matrix.length; i++) { if (matrix[i][col] == value) { return false; } } for (let i = 0; i < matrix.length; i++) { if (matrix[row][i] == value) { return false; } } let x = Math.floor(row / 3) * 3; let y = Math.floor(col / 3) * 3; for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (matrix[x + i][y + j] == value) { return false; } } } return true; } sudoku(matrix, row, col);

Ejemplo de sudoku sin solución:

 let matrix = [ [0, 0, 0, 0, 5, 4, 3, 0, 6], [0, 0, 0, 0, 0, 3, 2, 7, 0], [0, 0, 0, 7, 2, 0, 0, 0, 1], [9, 0, 0, 0, 7, 0, 0, 5, 3], [0, 0, 0, 0, 0, 0, 0, 0, 0], [8, 2, 0, 0, 1, 0, 0, 0, 9], [3, 0, 0, 0, 6, 1, 0, 0, 0], [0, 4, 6, 9, 0, 0, 0, 0, 0], [7, 0, 1, 5, 4, 0, 0, 0, 6] ];
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede devolver falso si el Sudoku no es válido y devolver verdadero si es válido.

 let row = 0; let col = 0; let matrix = [ [0, 0, 0, 0, 5, 4, 3, 0, 6], [0, 0, 0, 0, 0, 3, 2, 7, 0], [0, 0, 0, 7, 2, 0, 0, 0, 1], [9, 0, 0, 0, 7, 0, 0, 5, 3], [0, 0, 0, 0, 0, 0, 0, 0, 0], [8, 2, 0, 0, 1, 0, 0, 0, 9], [3, 0, 0, 0, 6, 1, 0, 0, 0], [0, 4, 6, 9, 0, 0, 0, 0, 0], [7, 0, 1, 5, 4, 0, 0, 0, 6] ]; function sudoku(matrix, row, col) { if (row == 9) { console.log(matrix); return true; } let next_row = 0; let next_col = 0; if (col == 8) { next_col = 0; next_row = row + 1; } else { next_col = col + 1; next_row = row; } if (matrix[row][col] != 0) { // Return the result from next empty box return sudoku(matrix, next_row, next_col); } else { for (let i = 0; i <= 9; i++) { if (isSafe(matrix, row, col, i) == true) { matrix[row][col] = i; // If found a valid sudoku, then return true. No need to check further. if (sudoku(matrix, next_row, next_col)) return true; matrix[row][col] = 0; } } } // No valid sudoku found after trying all numbers return false; } function isSafe(matrix, row, col, value) { for (let i = 0; i < matrix.length; i++) { if (matrix[i][col] == value) { return false; } } for (let i = 0; i < matrix.length; i++) { if (matrix[row][i] == value) { return false; } } let x = Math.floor(row / 3) * 3; let y = Math.floor(col / 3) * 3; for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (matrix[x + i][y + j] == value) { return false; } } } return true; } const isValid = sudoku(matrix, row, col); console.log(isValid ? "Valid Sudoku" : "Ivalid Sudoku");

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!