LIMIT = (20, 20)
def solve():
point = (0, 0)
valid_paths = 0
path = [point]
tried_directions = {}
solutions = []
while True:
# Display
#print_map(point, path)
#print 'PATH', path
#print 'POINT', point
#print 'DIRECTIONS', tried_directions.get(point)
#print
#res = raw_input()
#if res == 'd':
#import pdb; pdb.set_trace()
direction = tried_directions.get(point, -1) + 1
if direction == 2:
direction = None
if direction is None:
# If all moves from this place were tried
if point == (0, 0):
return valid_paths
else:
if point in tried_directions:
del tried_directions[point]
path.pop()
point = path[-1]
continue
else:
tried_directions[point] = direction
if direction == 0:
target = (point[0] + 1, point[1])
else:
target = (point[0], point[1] + 1)
if (target[0] < 0 or target[0] > LIMIT[0]
or target[1] < 0 or target[1] > LIMIT[1]
or target in path):
# Outside exception or path crossing
pass
elif target == LIMIT:
valid_paths += 1
else:
# If target is ok then
# replace point with target and update path
point = target
path.append(point)