fork download
  1. function minGeppoJumps(x1, y1, x2, y2) {
  2. //movimientos
  3. const directions = [
  4. [1, 2], [2, 1], [-1, 2], [-2, 1],
  5. [1, -2], [2, -1], [-1, -2], [-2, -1]
  6. ];
  7.  
  8.  
  9. const maxDistance = 300;
  10. const xMin = Math.min(x1, x2) - maxDistance;
  11. const xMax = Math.max(x1, x2) + maxDistance;
  12. const yMin = Math.min(y1, y2) - maxDistance;
  13. const yMax = Math.max(y1, y2) + maxDistance;
  14.  
  15.  
  16. const queue = [[x1, y1, 0]]; // [currentX, currentY, steps]
  17. const visited = new Set();
  18. visited.add(`${x1},${y1}`);
  19.  
  20. while (queue.length > 0) {
  21. const [currentX, currentY, steps] = queue.shift();
  22.  
  23.  
  24. if (currentX === x2 && currentY === y2) {
  25. return steps;
  26. }
  27.  
  28.  
  29. for (const [dx, dy] of directions) {
  30. const nextX = currentX + dx;
  31. const nextY = currentY + dy;
  32.  
  33.  
  34. if (
  35. nextX >= xMin &&
  36. nextX <= xMax &&
  37. nextY >= yMin &&
  38. nextY <= yMax &&
  39. !visited.has(`${nextX},${nextY}`)
  40. ) {
  41. visited.add(`${nextX},${nextY}`);
  42. queue.push([nextX, nextY, steps + 1]);
  43. }
  44. }
  45. }
  46.  
  47. return "IMPOSSIBLE";
  48. }
  49.  
  50. /*const input = require('fs').readFileSync(0, 'utf8').trim().split('\n');
  51. const [x1, y1, x2, y2] = input[0].split(' ').map(Number);
  52.  
  53. console.log(minGeppoJumps(x1, y1, x2, y2));*/
  54. console.log(minGeppoJumps(0, 0, 2, 1)); // Output: 1
  55. //console.log(minGeppoJumps(-2, -1, 198, 99)); // Output: 100
  56. console.log(minGeppoJumps(0, 0, 1, 1)); // Output: 2
  57.  
  58.  
  59.  
Success #stdin #stdout 0.05s 17236KB
stdin
aasas
stdout
1
2