-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbcPath.py
56 lines (45 loc) · 1.78 KB
/
AbcPath.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
class AbcPath(object):
def __init__(self):
pass
def maxPath(self, input):
candidates = [0]
for i, eachLine in enumerate(input):
for j, eachChar in enumerate(eachLine):
if eachChar == 'A':
candidates.append(1+self.findPathLength(ord('B'), input, i, j))
return max(candidates)
def checkAndFindNext(self, target, matrix, row, col):
if ord(matrix[row][col]) == target:
if target == ord('Z'):
return 1
else:
return 1+self.findPathLength(target+1, matrix, row, col)
else:
return 0
def findPathLength(self, target, matrix, row, col):
candidates = [0]
if row > 0:
if col > 0:
candidates.append(self.checkAndFindNext(target, matrix, row - 1, col - 1))
candidates.append(self.checkAndFindNext(target, matrix, row - 1, col))
if col < len(matrix[0]) - 1:
candidates.append(self.checkAndFindNext(target, matrix, row - 1, col + 1))
if col > 0:
candidates.append(self.checkAndFindNext(target, matrix, row, col - 1))
if col < len(matrix[0]) - 1:
candidates.append(self.checkAndFindNext(target, matrix, row, col + 1))
if row < len(matrix) - 1:
candidates.append(self.checkAndFindNext(target, matrix, row + 1, col))
if col > 0:
candidates.append(self.checkAndFindNext(target, matrix, row + 1, col - 1))
if col < len(matrix[0]) - 1:
candidates.append(self.checkAndFindNext(target, matrix, row + 1, col + 1))
return max(candidates)
myTest = AbcPath()
pathLength = myTest.maxPath([
"ABG",
"CFE",
"BDH",
"ABC"
])
print(pathLength)