Skip to content

Instantly share code, notes, and snippets.

@unsign3d
Created April 11, 2013 18:05
Show Gist options
  • Save unsign3d/5365716 to your computer and use it in GitHub Desktop.
Save unsign3d/5365716 to your computer and use it in GitHub Desktop.
#!/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