Problem Euler #18
10 Novembre 2012
Ultimamente mi sto cimentando con i problemi di Project Euler, per migliorare il mio codice, ottimizzarlo sui tempi di esecuzione ed imparare un sacco di quella matematica che non ho mai potuto studiare.
Il testo:
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom of the triangle below: 75 95 64 17 47 82 18 35 87 10 20 04 82 47 65 19 01 23 75 03 34 88 02 77 73 07 63 67 99 65 04 28 06 16 70 92 41 41 26 56 83 40 80 70 33 41 48 72 33 47 32 37 16 94 29 53 71 44 65 25 43 91 52 97 51 14 70 11 33 28 77 73 17 78 39 68 17 57 91 71 52 38 17 14 91 43 58 50 27 29 48 63 66 04 68 89 53 67 30 73 16 69 87 40 31 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
Sconsigliato dalle note di approcciarmi alla risoluzione con un Brute Force, ho fatto fatica a venirne fuori (scarsa esperienza ahime), poi ho trovato con google, il suggerimento di partire dall’ultima riga, andando verso l’alto.
Il trucco è di prendere i 2 numeri adiacenti della stessa riga, tenere il più grande, e sommarlo al numero superiore, sostituendolo.
Così facendo, in cima, otterremo il risultato per forza di cose.
Chomp says: The key insight I had was that the maximum score on a triangle of height N is the same as the maximum score of the same triangle minus its last row, with each element of the (N-1)th row being replaced by itself plus the greater of its two row-N neighbors. Consider the smaller example: 3 7 4 2 4 6 8 5 9 3 Going through row N-1: 2's greater row-N neighbor (between 8 or 5) is 8, so we replace 2 with 2+8=10. 4's greater row-N neighbor (between 5 or 9) is 9, so we replace 4 with 4+9=13. Finally, 6's greater row-N neighbor (between 9 or 3) is 9, so we replace 6 with 6+9=15. This yields: 3 7 4 10 13 15 Taking the same approach to reduce the triangle once more, we have: 3 20 19 The result of 23 is now obvious.
Ecco il mio codice Python adattato:
import time ts = time.clock() def get_triangle(input): file_in = open(input) nums = [line.split(' ') for line in file_in.readlines()] file_in.close() triangle = [[int(n.strip()) for n in line if n!= ''] for line in nums] triangle.reverse() return triangle def get_total_sum(triangle): for n in range(len(triangle)): for j in range(len(triangle[n])): try: a = triangle[n][j] b = triangle[n][j+1] triangle[n+1][j] += max([a,b]) except IndexError: pass return triangle[n][0] if __name__ == '__main__': res = get_total_sum(get_triangle('triangle.txt')) print res print time.clock() - ts
risultato:
>>> 1074 0.02
Anche sul problema 67, l’esecuzione è veloce:
>>> 7273 0.01
Categorie:Project Euler, python
Commenti recenti