Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- f = open('data.txt','r')
- a = []
- for line in f.readlines():
- #a.append(list(map(int,line.split())))
- row = []
- for x in line.split():
- row.append(int(x))
- a.append(row)
- f.close()
- for r in a:
- print(r)
- nr = len(a)
- nc = len(a[0])
- s_max = [[None for r in range(nr)] for c in range(nc)]
- s_min = [[None for r in range(nr)] for c in range(nc)]
- def get_max_sum(r,c):
- global k
- k += 1
- right = None
- down = None
- if c < nc-1:
- if s_max[r][c+1] != None:
- right = s_max[r][c+1]
- else:
- right = get_max_sum(r,c+1)
- if r < nr-1:
- if s_min[r+1][c] != None:
- down = s_max[r+1][c]
- else:
- down = get_max_sum(r+1,c)
- if right != None and down != None:
- ss = max(right,down)
- elif right != None and down == None:
- ss = right
- elif right == None and down != None:
- ss = down
- else:
- ss = 0
- s_max[r][c] = a[r][c] + ss
- return s_max[r][c]
- def get_min_sum(r,c):
- global k
- k += 1
- right = None
- down = None
- if c < nc-1:
- if s_min[r][c+1] != None:
- right = s_min[r][c+1]
- else:
- right = get_min_sum(r,c+1)
- if r < nr-1:
- if s_min[r+1][c] != None:
- down = s_min[r+1][c]
- else:
- down = get_min_sum(r+1,c)
- if right != None and down != None:
- ss = min(right,down)
- elif right != None and down == None:
- ss = right
- elif right == None and down != None:
- ss = down
- else:
- ss = 0
- s_min[r][c] = a[r][c] + ss
- return s_min[r][c]
- k = 0
- print(get_max_sum(0,0))
- print(k)
- k = 0
- print(get_min_sum(0,0))
- print(k)
Advertisement
Add Comment
Please, Sign In to add comment