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

234
Views
Haz un cuadrado mágico, con cambio mínimo

Entonces, estoy tratando de hacer un cuadrado mágico con cambios mínimos, pero con esto solo obtengo la última opción, quiero decir, es un cuadrado mágico pero no con el mínimo, estaba pensando que de alguna manera podemos usar el valor mínimo de x como un indicador para cambiar a esa matriz específica, pero no estoy seguro de cómo hacerlo.

 function minChange(arr) { let ms = [ [[4, 3, 8], [9, 5, 1], [2, 7, 6]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]] ]; function magic(arr, ms) { let count = 0; for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (arr[i][j] != ms[i][j]) count++; } } return count; } // If all the elements need to be changed, // there would be 9 changes, so we take the // max as 9 let min = 9; for (let i = 0; i < 8; i++) { let x = magic(arr, ms[i]); if (x < min){ x = min; arr = ms[i].slice(); } } return arr; } let arr = [[4, 9, 2], [3, 5, 7], [8, 1, 5]]; console.log(minChange(arr));
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

function magic(s) { const possible_square = [ [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[4, 3, 8], [9, 5, 1], [2, 7, 6]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]], [[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]] ]; let cost = null; let min = 8; for (let a = 0; a < min; a ++) { let temp = 0; temp = Math.abs(s[0][0] - possible_square[a][0][0]) + Math.abs(s[0][1] - possible_square[a][0][1]) + Math.abs(s[0][2] - possible_square[a][0][2]); temp += Math.abs(s[1][0] - possible_square[a][1][0]) + Math.abs(s[1][1] - possible_square[a][1][1]) + Math.abs(s[1][2] - possible_square[a][1][2]); temp += Math.abs(s[2][0] - possible_square[a][2][0]) + Math.abs(s[2][1] - possible_square[a][2][1]) + Math.abs(s[2][2] - possible_square[a][2][2]); if (cost === null) { cost = temp; } if (cost !== null) { if (temp < cost) { cost = temp; } } } return cost; }
about 4 years ago · Santiago Trujillo Report

0

Tienes una asignación incorrecta aquí:

 if (x < min){ x = min; arr = ms[i].slice(); }

Debería ser:

 if (x < min){ min = x; arr = ms[i].slice(); }
about 4 years ago · Santiago Trujillo 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!