-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhill_aniket.py
84 lines (74 loc) · 1.98 KB
/
hill_aniket.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
def print_matrix(mat):
for i in range(len(mat)):
print(mat[i])
def main():
n = int(input("enter size of matrix"))
mat = get_input_matrix(n)
x_start = int(input("enter starting x coordinate"))
y_start = int(input("enter starting y coordinate"))
print_matrix(mat)
currx = x_start
curry = y_start
prevx = -1
prevy = -1
while True:
next_step_cords = climb_one_step(mat, currx, curry)
# print(next_step_cords)
prevx = currx
prevy = curry
currx = next_step_cords[0]
curry = next_step_cords[1]
if currx == prevx and curry == prevy:
print(
f"stabilized at {next_step_cords[0]}, {next_step_cords[1]} with value {mat[next_step_cords[0]][next_step_cords[1]]}")
break
else:
continue
def get_input_matrix(n):
mat = list()
for i in range(n):
row_str = input().split()
row = list()
for string in row_str:
row.append(int(string))
mat.append(row)
return mat
def climb_one_step(mat, x, y):
maxx = x
maxy = y
maxval = mat[x][y]
if is_valid(mat, x+1, y):
if mat[x+1][y] > maxval:
maxx = x+1
maxy = y
maxval = mat[x+1][y]
if is_valid(mat, x - 1, y):
if mat[x - 1][y] > maxval:
maxx = x - 1
maxy = y
maxval = mat[x - 1][y]
if is_valid(mat, x, y+1):
if mat[x][y+1] > maxval:
maxx = x
maxy = y + 1
maxval = mat[x][y+1]
if is_valid(mat, x, y-1):
if mat[x][y-1] > maxval:
maxx = x
maxy = y - 1
maxval = mat[x][y-1]
# print("returning",maxx,maxy)
return [maxx, maxy]
def is_valid(mat, x, y):
rows = len(mat)
cols = len(mat[0])
if x < 0:
return False
if y < 0:
return False
if x >= rows:
return False
if y >= cols:
return False
return True
main()