-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveprincess.py
More file actions
102 lines (86 loc) · 1.65 KB
/
saveprincess.py
File metadata and controls
102 lines (86 loc) · 1.65 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
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
96
97
98
99
100
101
102
# Bot saves Princess
# https://www.hackerrank.com/challenges/saveprincess
def getDirection(before,after):
if (after[0]<before[0] and after[1]==before[1]):
return "UP"
if (after[0]>before[0] and after[1]==before[1]):
return "DOWN"
if (after[0]==before[0] and after[1]<before[1]):
return "LEFT"
if (after[0]==before[0] and after[1]>before[1]):
return "RIGHT"
##################### Logic #####################
def getSteps(p,m):
x = p[0]
y = p[1]
a = m[0]
b = m[1]
if (b > y):
before = [a,b]
b -= 1
after = [a,b]
while (b >= y):
print getDirection(before,after)
b -= 1
if (a > x):
b += 1
before = [a,b]
a -= 1
after = [a,b]
while (a >= x):
print getDirection(before,after)
a -= 1
elif (a < x):
b += 1
before = [a,b]
a += 1
while (a <= x):
after = [a,b]
print getDirection(before,after)
a += 1
elif (b < y):
before = [a,b]
b += 1
after = [a,b]
while (b <= y):
print getDirection(before,after)
b += 1
if (a > x):
b += 1
before = [a,b]
a -= 1
after = [a,b]
while (a >= x):
print getDirection(before,after)
a -= 1
elif (a < x):
b += 1
before = [a,b]
a += 1
after = [a,b]
while (a <= x):
print getDirection(before,after)
a += 1
##########################################
def displayPathtoPrincess(n,grid):
a = x = 0
for g in grid:
b = g.find('m')
if (b != -1):
break;
else:
a += 1
for g in grid:
y = g.find('p')
if (y != -1):
break;
else:
x += 1
p = [x,y]
m = [a,b]
getSteps(p,m)
m = input()
grid = []
for i in xrange(0, m):
grid.append(raw_input().strip())
displayPathtoPrincess(m,grid)