Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
¿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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda