Advertisement
kananmahammadli

ROBOT

May 27th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. from typing import List, Tuple, Any
  2. import random
  3.  
  4. def print_mx(mx:List[List[int]]):
  5.     for i in mx:
  6.         for j in i:
  7.             print(f"{j:3d}", end = " ")
  8.         print()
  9.  
  10. def main():
  11.  
  12.     mx = [
  13.             [0,0,0,20,0,0],
  14.             [1,0,3,0,5,0],
  15.             [0,0,0,0,5,0],
  16.             [10,1,0,0,0,0],
  17.             [0,0,11,7,0,0],
  18.         ]
  19.     print_mx(mx)
  20.     print()
  21.     dp = [[0] * len(mx[0]) for _ in range(len(mx))]
  22.  
  23.     dp[0][0] = mx[0][0]
  24.  
  25.     for i in range(1, len(mx[0])):
  26.         dp[0][i] = dp[0][i-1] + mx[0][i]
  27.  
  28.     for i in range(1, len(mx)):
  29.         dp[i][0] = dp[i-1][0] + mx[i][0]
  30.  
  31.     for i in range(1, len(mx)):
  32.         for j in range(1, len(mx[0])):
  33.             dp[i][j] = mx[i][j] + max(dp[i-1][j], dp[i][j-1])
  34.  
  35.     print_mx(dp)
  36.     ls = []
  37.     i,  j = len(mx) - 1, len(mx[0]) - 1
  38.    
  39.     while i > 0 and j > 0:
  40.  
  41.         if dp[i-1][j] >= dp[i][j-1]:
  42.  
  43.             ls.append("Down")
  44.             i -= 1
  45.  
  46.         else:
  47.  
  48.             ls.append("Right")
  49.             j -= 1
  50.  
  51.     while i == 0 and j > 0:
  52.  
  53.         ls.append("Right")
  54.         j -= 1
  55.  
  56.     while j == 0 and i > 0:
  57.         ls.append("Down")
  58.         i -= 1
  59.  
  60.     ls = ls[::-1]
  61.  
  62.     for step in ls:
  63.         print(step, end = " ")
  64.  
  65.     print(dp[-1][-1])
  66.  
  67.  
  68.  
  69.  
  70. if __name__ == "__main__":
  71.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement