-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadjacentPosGrid.py
More file actions
60 lines (45 loc) · 1.42 KB
/
adjacentPosGrid.py
File metadata and controls
60 lines (45 loc) · 1.42 KB
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
# python code to implement the approach
# Function to check whether
# position is valid or not
def isValidPos(i, j, n, m):
if (i < 0 or j < 0 or i > n - 1 or j > m - 1):
return 0
return 1
# Function that returns all adjacent elements
def getAdjacent(arr, i, j):
# Size of given 2d array
n = len(arr)
m = len(arr[0])
# Initialising a vector array
# where adjacent element will be stored
v = []
# Checking for all the possible adjacent positions
if (isValidPos(i - 1, j - 1, n, m)):
v.append(arr[i - 1][j - 1])
if (isValidPos(i - 1, j, n, m)):
v.append(arr[i - 1][j])
if (isValidPos(i - 1, j + 1, n, m)):
v.append(arr[i - 1][j + 1])
if (isValidPos(i, j - 1, n, m)):
v.append(arr[i][j - 1])
if (isValidPos(i, j + 1, n, m)):
v.append(arr[i][j + 1])
if (isValidPos(i + 1, j - 1, n, m)):
v.append(arr[i + 1][j - 1])
if (isValidPos(i + 1, j, n, m)):
v.append(arr[i + 1][j])
if (isValidPos(i + 1, j + 1, n, m)):
v.append(arr[i + 1][j + 1])
# Returning the vector
return v
if __name__ == "__main__":
# Given vector array
arr = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
x, y = 0,0
# Function call
ans = getAdjacent(arr, x, y)
# Print all the adjacent elements
for i in range(0, len(ans)):
print(ans[i], end=" ")