# -*- coding: utf-8 -*- """ Created on Sun Apr 07 01:11:30 2013 @author: Matthew Cordrey """ #Starting in the top left corner of a 2x2 grid, and only being able #to move to the right and down, there are exactly 6 routes to the bottom right corner. #How many such routes are there through a 20x20 grid? class Board: def __init__(self,width,depth): self.width = width self.depth = depth self.nodes = ((width + 1) * (depth + 1)) - 1 self.moves = width + depth self.goodpathlog = [] self.matrix = [] for y in range(0,width+1): for x in range(0,depth+1): vals = {"xpos": x, "ypos": y, "locid": ((y)*(width+1))+x} self.matrix.append(vals) def getLocidValue(self,x,y): for d in self.matrix: if d['xpos']==x and d['ypos']==y: value = d['locid'] return value return False def getPosX(self,locid): for l in range(self.nodes): if self.matrix['locid']==locid: x = self.matrix['xpos'] return x def getPosY(self,locid): for l in range(self.nodes): if l['locid']==locid: y = l['ypos'] return y def moveX(self,position): if position['xpos'] < self.width: position['xpos'] += 1 position['locid'] += 1 return position return False def moveY(self,position): if position['ypos'] < self.depth: position['ypos'] += 1 position['locid'] += self.width return position return False def chooseXY(self,position): valRight = 0 valDown = 0 currentX = self.getPosX(position) currentY = self.getPosY(position) if currentX < self.width: valRight = self.getLocidValue((currentX+1),currentY) if currentY < self.depth: valDown = self.getLocidValue(currentX,(currentY+1)) nextMove = "x" if valRight > valDown else "y" return nextMove def tryPath(self): position = 0 #path = [position['locid']] #list of locids print self.chooseXY(position) # print path # while self.moveX(position): # path.append(position['locid']) # print path return position def main(): b = Board(3,3) print b.width print b.depth print b.nodes print b.moves #board = b.createBoard(3,3) #print board m = b.matrix print m for x in range(4): for y in range(4): print b.getLocidValue(x,y) print b.tryPath() if __name__ == '__main__': main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question