Vassil_Iliev

jump around

Mar 31st, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const line1 = gets().split(' ').map(Number);
  2. const rows = line1[0];
  3. const cols = line1[1];
  4. const countJumps = line1[2];
  5. const line2 = gets().split(' ').map(Number);
  6. let curRow = line2[0];
  7. let curCol = line2[1];
  8. let arrMoves = []
  9.  
  10. for(let i = 0; i < countJumps;i++){
  11. arrMoves.push(gets().split(' ').map(Number))
  12. }
  13.  
  14. let index = 1;
  15. let matrix = [];
  16. let jumps = 0;
  17. let arrCollected = [];
  18. let sumOfCollected = 0;
  19.  
  20. for(let i = 0; i<rows;i++){
  21.     matrix[i]=[]
  22.     for(let j = 0; j<cols;j++){
  23.         matrix[i][j]=index++
  24.     }
  25. }
  26. let counter = 0
  27.  
  28. outer:
  29. while(true){
  30.  
  31.     if(curRow< 0 || curCol< 0 || curRow >= rows || curCol>= cols){
  32.             jumps++
  33.             print(`escaped ${sumOfCollected}`)
  34.             break outer;
  35.         }else if(arrCollected.includes(matrix[curRow][curCol])){
  36.             jumps++
  37.             print(`caught ${jumps}`)
  38.             break outer;
  39.         }else{
  40.             arrCollected.push(matrix[curRow][curCol]);
  41.             sumOfCollected +=matrix[curRow][curCol]
  42.             jumps++;
  43.             curRow = curRow + arrMoves[counter][0]
  44.             curCol = curCol + arrMoves[counter][1]
  45.         }
  46.    
  47.         if(counter < arrMoves.length-1){
  48.         counter++
  49.     }else{
  50.         counter = 0;
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment