Advertisement
Vassil_Iliev

matrix max sum

Apr 1st, 2022
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. Matrix Max Sum
  2. Description
  3. Write a program that finds the maximum sum between two given coordinates in a matrix. The coordinates are provided as a list of pairs, such as 2 3 -4 -2 where 2 3 is the first pair and -4 -2 is the next one. The first number of the pair is the row coordinate R and the second one is the column coordinate C.
  4.  
  5. You need to follow a path from R to C and sum up all the values you encounter in cells. For example, with coordinates 2 3 you start from the beginning of the 2nd row and move towards the 3rd column. When you reach the column, you go up because the column coordinate 3 is positive.
  6.  
  7. With coordinates -4 -2 you start from the end of the 4th row (because -4 is negative) and move towards the 2nd column. When you reach it, you go down (-2 is negative).
  8.  
  9. Check the following picture for a clearer idea.
  10.  
  11. table
  12.  
  13. The path 2 3 yields a sum of 17 which is higher than the sum you obtain by following -4 -2 (15)
  14.  
  15. Print the maximum sum you find to the standard output.
  16.  
  17. Note
  18. You always have to move horizontally in rows and vertically in columns. For example, in the above picture, the correct path with coordinates -4 -2 is 3 -> 2 -> 5 -> 3 -> 2 and NOT 3 -> 4 -> 3 -> 6 -> 2.
  19.  
  20. Input
  21. On the first line, you receive an integer N - the number of rows in the matrix
  22. On the next N lines, each row of the matrix is given, with columns separated by a space
  23. On the last line, the R and C coordinates are given, separated by spaces
  24. Output
  25. On the only line of output, print the maximum sum found.
  26. Constraints
  27. N will be an integer between 5 and 20, inclusive.
  28. All rows have the exact same length, also between 5 and 20, inclusive.
  29. The R and C coordinates will always be valid and inside the matrix.
  30. The R C pairs will be at least 1 and no more than 20.
  31. Matrix elements will have values in range -5000 and 5000.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement