1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| import sys sys.setrecursionlimit(100000) file = open("sctfmaze.txt") maze = [[0 for j in range(0, 100)] for i in range(0, 100)] vis = [[0 for j in range(0, 100)] for i in range(0, 100)] class Node: t = 0 r = 0 b = 0 l = 0
for line in file: a = line[:-1].split(" ") n = Node() for i in range(2,len(a)): if a[i] == '0' : n.t = 1 if a[i] == '1' : n.r = 1 if a[i] == '2' : n.b = 1 if a[i] == '3' : n.l = 1 maze[int(a[0])][int(a[1])] = n
def check(i,j): if i>=100 or i<0 or j>=100 or j<0: return False if vis[i][j] == 1: return False return True def printmap(): global vis for i in range(0,100): for j in range(0,100): if vis[i][j] == 1: print "%2d%2d" % (i,j) print " " maxx = 0 print maxx,i,j def dfs(i,j,n): global maxx global vis global maze n += 1 if n>maxx: print n,i,j maxx = n if check(i-1,j) and maze[i][j].t == 0: vis[i-1][j] = 1 dfs(i-1,j,n) vis[i-1][j] = 0 if check(i,j+1) and maze[i][j].r == 0: vis[i][j+1] = 1 dfs(i,j+1,n) vis[i][j+1] = 0 if check(i+1,j) and maze[i][j].b == 0: vis[i+1][j] = 1 dfs(i+1,j,n) vis[i+1][j] = 0 if check(i,j-1) and maze[i][j].l == 0: vis[i][j-1] = 1 dfs(i,j-1,n) vis[i][j-1] = 0 vis[70][22] = 1 dfs(70,22,0) exit() for i in range(0,100): for j in range(0,100): vis[i][j] = 1 dfs(i,j,0) vis[i][j] = 0
|