-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridWalk.py
95 lines (78 loc) · 1.46 KB
/
GridWalk.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'''
Created on Mar 20, 2014
@author: vatsa
'''
import sys
sys.setrecursionlimit(10000)
#print sys.getrecursionlimit()
def numsum(n):
abs(n)
l = []
while n!= 0:
l.append(n % 10)
n/=10
return sum(l)
def check (i,j):
if numsum(i)+numsum(j) <= 19:
return 0
else:
return 1
def left(x,y):
if x-1 < 0:
return 0
if m[x-1][y] == 0:
return 1
else:
return 0
def right(x,y):
if x+1 >= len(m[0]):
return 0
if m[x+1][y] == 0:
return 1
else:
return 0
def up(x,y):
if y-1 < 0:
return 0
if m[x][y-1] == 0:
return 1
else:
return 0
def down(x,y):
if y+1 >= len(m):
return 0
if m[x][y+1] == 0:
return 1
else:
return 0
def valid (x,y):
if x < 0 or x >= len(m[0]):
return False
if y < 0 or y >= len(m):
return False
return True
def floodfill(x,y):
global count
if valid(x,y):
m[x][y] = -1
count +=1
else:
return
if left(x,y) == 1:
floodfill(x-1,y)
if right(x,y) == 1:
floodfill(x+1,y)
if up(x,y) == 1:
floodfill(x,y-1)
if down(x,y) == 1:
floodfill(x,y+1)
m = [[check(i,j) for i in range(300)] for j in range(300)]
#m = [[0 for i in range(3)] for j in range(3)]
count = 0
#for x in m:
# print x
# m[1][1] = 1
# m[1][0] = 1
# m[1][2] = 1
floodfill(0,0)
print (count*4) - 3