Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools
- import time
- array = [(131, 673, 234, 103, 18, 444, 333),
- (201, 96, 342, 965, 150, 111, 222),
- (630, 803, 746, 422, 111, 44, 77),
- (630, 803, 746, 422, 111, 44, 77),
- (805, 732, 524, 37, 331, 777, 123)]
- ymax = len(array)
- xmax = len(array[0])
- select = ["D"] * (ymax - 1) + ["R"] * (xmax - 1)
- perms = itertools.permutations(select, ymax + xmax - 2)
- st = time.time()
- mintot = float('inf')
- minpath = ""
- for path in perms:
- x, y = 0, 0
- tot = array[y][x]
- for direct in path:
- if direct == "R":
- x += 1
- else:
- y += 1
- tot += array[y][x]
- if tot > mintot:
- break
- if tot < mintot:
- mintot = tot
- minpath = path
- en = time.time()
- print(f"{minpath=}, {mintot=} time= {en-st:.2f}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement