princeofsudan

ProjectEuler#15 Lattice paths

Mar 8th, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. #Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly #6 routes to the bottom right corner.
  2.  
  3. #How many such routes are there through a 20×20 grid?
  4.  
  5. n = 20
  6. array = []
  7.  
  8. for i in range(n+1):
  9.     array.append([])
  10.  
  11. #only one way to get to each of these points(first row), so we mark each of those with a 1
  12. for x in range(0,n+1):
  13.     array[0].append(1)
  14.  
  15. #only one way to get to each of these points(first col), so we mark each of those with a 1
  16. for y in range(1,n+1):
  17.     array[y].append(1)
  18.  
  19. #starting at element element (1,1) we add above element mark and left element mark.
  20. for x in range(1,n+1):
  21.     for y in range(1,n+1):
  22.         array[x].append(array[x][y-1]+array[x-1][y])
  23.  
  24. print array[20][20]
Advertisement
Add Comment
Please, Sign In to add comment