* Computer Science/Algorithm
Unit 8. basic problem in dynamic programming
soicem
2017. 3. 20. 12:21
경로 찾기, 그냥 재미로 ~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import random def matrixShortestPath(row, col): # sets = [(row+1, col), (row, col+1), (row-1, col), (row, col-1)] sets = [(row + 1, col), (row, col + 1)] visitedList[row] = visitedList[row] | (2 ** col) print(lists[row][col]) # if row == 0 and col == 0: # sets = sets[0:2] # elif row == 0: # sets = sets[0:2] + [sets[3]] # elif col == 0: # sets = sets[0:3] # print(sets) max = None for set in sets: row, col = set if row < len(lists[0]) and col < len(lists): if visitedList[row] & (2 ** col) == 0: if lists[row][col] > max: max = lists[row][col] mem = set if max == None: return 0 row, col = mem matrixShortestPath(row, col) lists = [] size = 15 visitedList = [0 for _ in range(0, size)] for _ in range(0, size): lists.append([random.randint(1, 100) for _ in range(0, size)]) for list in lists: print list matrixShortestPath(0, 0) print visitedList visited2x2List = [] for _ in range(0, size): visited2x2List.append([]) for i in range(0, size): for j in range(0, size): if visitedList[i] & 2**j: visited2x2List[i].append(1) else: visited2x2List[i].append(0) for alist in visited2x2List: print alist | cs |