Zhorzh

matrixMaxSum

Jan 8th, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. let input = [
  2. '6',
  3. '1 2 3 4 5 6',
  4. '2 3 4 5 6 7',
  5. '6 5 4 3 2 1',
  6. '3 4 5 6 7 8',
  7. '4 5 6 7 8 9',
  8. '9 8 7 6 5 4',
  9. '3 5 3 -5 -4 -2',
  10.  
  11. ];
  12.  
  13. let print = this.print || console.log;
  14. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  15. let numberOfRowsAndCols = +gets()
  16. let emptyMatrix = []
  17. let filledMatrix = []
  18. let sum = 0
  19. let sumMax = 0
  20. for(let i = 0;i<numberOfRowsAndCols;i++){
  21. emptyMatrix.push(gets(i).split(' ').map(Number))
  22. }
  23.  
  24. let coordinates = gets().split(' ').map(Number)
  25. for(let r = 0 , c = 1 ; c<coordinates.length ;r+=2,c+=2){
  26. if(coordinates[r]>0 && coordinates[c]>0){
  27. for(let col = 0 ; col <= coordinates[c] ; col++){
  28. sum += emptyMatrix[coordinates[r]][col]
  29. sum+= emptyMatrix[coordinates[r]-1][coordinates[c]]
  30. }
  31. }else if(coordinates[r]>0 && coordinates[c]<0){
  32. for(let col = 0 ; col <= Math.abs(coordinates[c]) ; col++){
  33. sum += emptyMatrix[coordinates[r]][col]
  34. sum+= emptyMatrix[coordinates[r]+1][Math.abs(coordinates[c])]
  35. }
  36. }else if(coordinates[r]<0 && coordinates[c]>0){
  37. for(let col = numberOfRowsAndCols-1 ; col >=coordinates[c] ; col--){
  38. sum += emptyMatrix[Math.abs(coordinates[r])][col]
  39. sum+= emptyMatrix[Math.abs(coordinates[r])-1][coordinates[c]]
  40. }
  41. }else if(coordinates[r]<0 && coordinates[c]<0){
  42. for(let col = numberOfRowsAndCols-1 ; col >=Math.abs(coordinates[c]) ; col--){
  43. sum += emptyMatrix[Math.abs(coordinates[r])][col]
  44. sum+= emptyMatrix[Math.abs(coordinates[r])+1][Math.abs(coordinates[c])]
  45. }
  46. }
  47.  
  48. if(sum > sumMax){
  49. sumMax = sum
  50. }
  51. }
  52.  
  53. print(sumMax)
Advertisement
Add Comment
Please, Sign In to add comment