Created
April 11, 2013 18:05
-
-
Save unsign3d/5365716 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Head ends here | |
def displayPathtoPrincess(m,grid) | |
#find the princess | |
mid = (m/2).ceil + 1 | |
man = [mid,mid] | |
m -=1 | |
if grid[0][0] == 'p' | |
#walk up | |
while man[0] > 0 do | |
man[0] -= 1 | |
print "UP\n" | |
end | |
#walk left | |
while man[1] > 0 do | |
man[1] -= 1 | |
print "LEFT\n" | |
end | |
elsif grid[0][m] == 'p' | |
while man[0] > 0 do | |
man[0] -= 1 | |
print "UP\n" | |
end | |
#walk right | |
while man[1] <= m do | |
man[1] += 1 | |
print "RIGHT\n" | |
end | |
elsif grid[m][0] == 'p' | |
while man[0] <= m do | |
man[0] += 1 | |
print "DOWN\n" | |
end | |
#walk left | |
while man[1] > 0 do | |
man[1] -= 1 | |
print "LEFT\n" | |
end | |
end | |
if grid[m][m] == 'p' | |
while man[0] <= m do | |
man[0] += 1 | |
print "DOWN\n" | |
end | |
while man[1] <= m do | |
man[1] += 1 | |
print "RIGHT\n" | |
end | |
end | |
end | |
# Tail starts here | |
m = gets.to_i | |
grid = Array.new(m) | |
(0...m).each do |i| | |
grid[i] = gets.strip | |
end | |
displayPathtoPrincess(m,grid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment