fork download
  1. function movimientos() {
  2. let input = readline();
  3. let splited = input.split(',');
  4. const x1 = Number(splited[0]);
  5. const y1 = Number(splited[1]);
  6. const x2 = Number(splited[2]);
  7. const y2 = Number(splited[3]);
  8. //console.log('inputs', splited);
  9.  
  10. if (x1 === x2 && y1 === y2) {
  11. return 0;
  12. }
  13.  
  14. const moves = [
  15. [-2, -1], [-1, -2], [1, -2], [2, -1],
  16. [2, 1], [1, 2], [-1, 2], [-2, 1]
  17. ];
  18.  
  19. const queue = [[x1, y1, 0]];
  20. const marked = new Set([`${x1},${y1}`]);
  21.  
  22. while (queue.length > 0) {
  23. const [x, y, jumps] = queue.shift();
  24.  
  25. for (const [dx, dy] of moves) {
  26. const nx = x + dx;
  27. const ny = y + dy;
  28.  
  29. if (nx === x2 && ny === y2) {
  30. return jumps + 1;
  31. }
  32.  
  33. const key = `${nx},${ny}`;
  34. if (nx >= -200 && nx <= 200 && ny >= -200 && ny <= 200 && !marked.has(key)) {
  35. marked.add(key);
  36. queue.push([nx, ny, jumps + 1]);
  37. }
  38. }
  39. }
  40.  
  41. return "IMPOSSIBLE";
  42. }
  43.  
  44. console.log(movimientos());
Success #stdin #stdout 0.05s 16984KB
stdin
1, 1, 1, 1
stdout
0