Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Forced Indentation of Paths

Name: Anonymous 2010-07-26 7:34

Hey /prog/ Can you help me out with this plox? I'm a noob and don't understand why this isn't working. :( I just want it to return the paths, but it only does one right one and then two horribly wrong ones and then stops.


area = [[1,0,0,0],
    [0,0,0,0],
    [0,0,0,0],
    [0,0,0,1]]

start = { "x" : 0, "y" : 0}
end = { "x" : 3, "y" : 3}
path = []

def findPaths(start, end, path, area):
    # Goal Reached
    if end == start:
        path.append(start)
        drawPath(path, area)
        return True
   
    # Validate Move
    if start["x"] < 0:
        return False
    if start["y"] < 0:
        return False
    if start["y"] >= len(area):
        return False
    if start["x"] >= len(area[0]):
        return False
    if start in path:
        return False
   
    # Append Path
    path.append(start)
   
    # Paths - Clockwise
    findPaths({"x" : start["x"], "y" : start["y"] + 1}, end, path, area)
    findPaths({"x" : start["x"] + 1, "y" : start["y"] + 1}, end, path, area)
    findPaths({"x" : start["x"] + 1, "y" : start["y"]}, end, path, area)
    findPaths({"x" : start["x"] + 1, "y" : start["y"] - 1}, end, path, area)
    findPaths({"x" : start["x"], "y" : start["y"] - 1}, end, path, area)
    findPaths({"x" : start["x"] - 1, "y" : start["y"] - 1}, end, path, area)
    findPaths({"x" : start["x"] - 1, "y" : start["y"]}, end, path, area)
    findPaths({"x" : start["x"] - 1, "y" : start["y"] - 1}, end, path, area)

def drawPath(path, area):
    for n in range(len(path)):
        area[path[n]["x"]][path[n]["y"]] = n
    for y in range(len(area)):
        for x in range(len(area[0])):
            print area[x][y],
        print
   
    print "\n".join([str(x) for x in path])
    print "===="

findPaths(start, end, path, area)



0 0 0 0
1 0 0 0
2 0 0 0
3 4 5 6
{'y': 0, 'x': 0}
{'y': 1, 'x': 0}
{'y': 2, 'x': 0}
{'y': 3, 'x': 0}
{'y': 3, 'x': 1}
{'y': 3, 'x': 2}
{'y': 3, 'x': 3}
====
0 0 0 0
1 0 0 0
2 0 0 7
3 4 5 8
{'y': 0, 'x': 0}
{'y': 1, 'x': 0}
{'y': 2, 'x': 0}
{'y': 3, 'x': 0}
{'y': 3, 'x': 1}
{'y': 3, 'x': 2}
{'y': 3, 'x': 3}
{'y': 2, 'x': 3}
{'y': 3, 'x': 3}
====
0 0 11 10
1 0 12 9
2 0 13 7
3 4 5 14
{'y': 0, 'x': 0}
{'y': 1, 'x': 0}
{'y': 2, 'x': 0}
{'y': 3, 'x': 0}
{'y': 3, 'x': 1}
{'y': 3, 'x': 2}
{'y': 3, 'x': 3}
{'y': 2, 'x': 3}
{'y': 3, 'x': 3}
{'y': 1, 'x': 3}
{'y': 0, 'x': 3}
{'y': 0, 'x': 2}
{'y': 1, 'x': 2}
{'y': 2, 'x': 2}
{'y': 3, 'x': 3}
====

Name: Anonymous 2010-07-27 13:01

I'll be slightly helpful and tell you how to write a program that does it. That's not the best way, although you might actually learn something.

f(x, y) is the number of possible routes to get from (x, y) to (20, 20).

f(20, 20) = 1
f(x, y) = f(x + 1, y) + f(x, y + 1)
(but you need to make sure that x < 20 and y < 20)

You should be able to figure it out from that.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List