momo3141

Jump Around

Jan 7th, 2023
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let data = gets().split(' ').map(Number);
  2. let N = data[0];
  3. let M = data[1];
  4. let jumps = data[2];
  5. let matrix = [];
  6. let counter = 1
  7. let jumpCount = 0
  8. //създава полето
  9. for( let i = 0; i<=N-1;i++){
  10.     matrix.push([]);
  11.     for(let j = 0; j <= M-1; j++){
  12.         matrix[i][j]=counter
  13.         counter++
  14.     }
  15. }
  16.  
  17. let currentPosition = gets().split(' ').map(Number);
  18. let sumOfnumbers = matrix[currentPosition[0]][currentPosition[1]];
  19. matrix[currentPosition[0]][currentPosition[1]] = 0
  20. let moves = []
  21.  
  22. for (let i = 1; i<=jumps; i++){
  23.   moves.push(gets().split(' ').map(Number))
  24. }
  25. let newPos = (move,pos) => {
  26.     let result = []
  27.     let row = move[0];
  28.     let col = move[1];
  29.     result.push(row + pos[0]);
  30.     result.push(col + pos[1]);
  31.     return result
  32. }
  33. hasEscaped = true
  34. outer:
  35. while(true){
  36. for (let i = 0 ; i <= jumps-1; i++){
  37.    currentPosition = newPos(moves[i],currentPosition)
  38.    if(matrix[currentPosition[0]][currentPosition[1]] === 0){
  39.     hasEscaped = false
  40.     break outer;
  41.    } else if (currentPosition[0] < 0 || currentPosition[0] > M-1 || currentPosition[1] < 0 || currentPosition[1] > N-1){
  42.     hasEscaped = true
  43.     break outer;
  44.    }
  45.    sumOfnumbers =  sumOfnumbers + matrix [currentPosition[0]][currentPosition[1]];
  46.    matrix[currentPosition[0]][currentPosition[1]]=0
  47.    jumpCount++
  48. }
  49. }
  50. if(hasEscaped){
  51.     console.log(`escaped ${sumOfnumbers}`);
  52. } else {
  53.     console.log(`caught ${jumpCount}`);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment