momo3141

Scrooge McDuck

Jan 7th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let size = gets().split(' ').map(Number);
  2. fieldY = size[0];
  3. fieldX = size[1];
  4. let matrix = [];
  5. let coins = 0
  6. let position = [0,0] //coordinates
  7. for (let i = 1 ; i <= fieldY; i++){
  8.     matrix.push(gets().split(' ').map(Number));
  9.    
  10. }
  11.  
  12. // тука се намира нулевата позиция
  13. loop:
  14. for(let i = 0 ; i <= fieldX-1; i++){
  15.     for (let j = 0; j <= fieldY-1; j++){
  16.         if(matrix[i][j] === 0 ){
  17.             position[0] = i
  18.             position[1] = j
  19.             break loop;
  20.         }
  21.     }
  22. }
  23. //тази функция определя максималната посока според правилото
  24. // "he chooses a cell (always the largest) from the order left, right, up, down"
  25. let maxLRUD = (dir) =>{
  26.     let max = dir.slice();
  27.     max = max.sort((a,b) => a-b)
  28.     let maximum = max[3]
  29.     if(dir[0] >= maximum){
  30.         return 'left';
  31.     } else if(dir[1] >= maximum){
  32.         return 'right';
  33.     } else if(dir[2] >= maximum){
  34.         return 'up'
  35.     } else if(dir[3] >= maximum){
  36.         return 'down'
  37.     }
  38. }
  39. //тука се определа посоката според монетите и накрая се извиква горната функция
  40. let maxCoinsDirection = (position) =>{
  41. let left = 0;
  42. let right = 0 ;
  43. let up = 0 ;
  44. let down = 0;
  45.     if (position[0]>0){
  46.             up = matrix[position[0]-1][position[1]]
  47.     }
  48.     if (position[0]<fieldY-1){
  49.             down = matrix[position[0]+1][position[1]]
  50.     }
  51.     if (position[1]<fieldX-1){
  52.             right = matrix[position[0]][position[1]+1]
  53.     }
  54.     if (position[1]>0){
  55.             left = matrix[position[0]][position[1]-1]
  56.     }
  57.  
  58. let dir = [left, right, up, down]
  59.  
  60. return maxLRUD(dir)
  61.  
  62. }
  63. // тази функция определя дали е свършила играта
  64. let endCheck = (position) => {
  65.     let endLeft, endDown, endRight, endUp = false
  66.    if( position[1] === 0 ){
  67.     endLeft = true;
  68.    } else if (matrix[position[0]][position[1]-1] === 0){
  69.     endLeft = true;
  70.    }
  71.  
  72.    if(position[1] === fieldX-1){
  73.     endRight =true
  74.    } else if (matrix[position[0]][position[1]+1] === 0 ){
  75.     endRight = true;
  76.    }
  77.    if(position[0] === 0){
  78.     endUp =true
  79.    } else if (matrix[position[0]-1][position[1]] === 0 ){
  80.     endUp = true;
  81.    }
  82.    if(position[0] === fieldY-1){
  83.     endDown =true
  84.    } else if( matrix[position[0]+1][position[1]] === 0 ){
  85.     endDown = true;
  86.    }
  87.    if(endLeft && endRight && endUp && endDown){
  88.     return true
  89.    } else {
  90.     return false
  91.    }
  92.  
  93. }
  94. let direction = ''
  95.  
  96. //тука се събират монетите
  97. while(true){
  98.     if(endCheck(position)){
  99.         break;
  100.     }
  101.     direction = maxCoinsDirection(position)
  102.     if(direction === 'left'){
  103.         matrix[position[0]][position[1]-1] --;
  104.         position[1]--;
  105.     } else if (direction === 'right'){
  106.         matrix[position[0]][position[1]+1] --;
  107.         position[1]++;
  108.     } else if (direction === 'up') {
  109.         matrix[position[0]-1][position[1]] --;
  110.         position[0] --;
  111.     } else if (direction === 'down') {
  112.         matrix[position[0]+1][position[1]] --;
  113.         position[0]++;
  114.     }
  115.     coins++
  116.    
  117. }
  118.  
  119.  
  120. console.log(coins);
  121.  
Advertisement
Add Comment
Please, Sign In to add comment