-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave-princess.py
More file actions
48 lines (39 loc) · 1.28 KB
/
save-princess.py
File metadata and controls
48 lines (39 loc) · 1.28 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
#!/usr/bin/python
# Find where the player is
def find_player_index(n, grid):
for _ in range(0, n):
for __ in range(0, n):
if grid[_][__] == 'm':
return _, __
# Find where the princess is
# Shortcut - Only need to check the 4 corners
def find_princess_index(n, grid):
for _ in [0, n - 1]:
for __ in [0, n - 1]:
if grid[_][__] == 'p':
return _, __
# Find player position
# Find princess position
# Determine moves based on relative postions
def displayPathtoPrincess(n, grid):
player_index = find_player_index(n, grid)
princess_index = find_princess_index(n, grid)
# Find moves in vertical axis
if player_index[0] > princess_index[0]:
for _ in range(0, player_index[0] - princess_index[0]):
print('UP')
else:
for _ in range(0, princess_index[0] - player_index[0]):
print('DOWN')
# Find moves in horizontal axis
if player_index[1] > princess_index[1]:
for _ in range(0, player_index[1] - princess_index[1]):
print('LEFT')
else:
for _ in range(0, princess_index[1] - player_index[1]):
print('RIGHT')
m = int(input())
grid = []
for i in range(0, m):
grid.append(input().strip())
displayPathtoPrincess(m, grid)