#!/bin/ruby #require 'pry' def printShortestPath(n, i_start, j_start, i_end, j_end) # Print the distance along with the sequence of moves. i_change = i_end - i_start if i_change % 2 != 0 puts "Impossible" return end moves = "" count = 0 j_change = j_end - j_start if i_change < 0 # move up until on the same row while i_change != 0 if j_change <= 0 moves += " UL" count+=1 i_change += 2 j_change += 1 else moves += " UR" count+=1 i_change += 2 j_change -= 1 end end elsif i_change > 0 # move down until on the same row while j_change > (i_change.abs + 3) count+=1 moves += " R" j_change -= 2 end while i_change != 0 if j_change <= 0 count+=1 moves += " LR" i_change -= 2 j_change += 1 else # j_change > 0 count+=1 moves += " LL" i_change -= 2 j_change -= 1 end end end # binding.pry if j_change != 0 if j_change % 2 != 0 puts "Impossible" return else while j_change != 0 if j_change > 0 count += 1 moves += " R" j_change -= 2 else count += 1 moves += " L" j_change += 2 end end end end puts count puts moves.slice(1..-1) return end n = gets.strip.to_i i_start, j_start, i_end, j_end = gets.strip.split(' ') i_start = i_start.to_i j_start = j_start.to_i i_end = i_end.to_i j_end = j_end.to_i printShortestPath(n, i_start, j_start, i_end, j_end)