/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.util.Scanner; /** * * @author Pc-01-Pc */ class Node { int i,j; Node UL, UR,R,LR,LL,L; public Node(int im,int jm) { i = im; j=jm; UL = UR =R=LR=LL=L= null; } } public class Solution { static int [] [] places; static int cuenta=0; /** * @param args the command line arguments */ static String visit(Node x,int n, int prof,int i_end, int j_end){ int a=0,b=0; String r=""; if(prof>0){ prof=prof-1; if(x.UL!=null){ r=visit(x.UL,n,prof,i_end,j_end); if(!r.isEmpty()){ r="UL "+r; return r; } } if(x.UR!=null){ r=visit(x.UR,n,prof,i_end,j_end); if(!r.isEmpty()){ r="UR "+r; return r; } } if(x.R!=null){ r=visit(x.R,n,prof,i_end,j_end); if(!r.isEmpty()){ r="R "+r; return r; } } if(x.LR!=null){ r=visit(x.LR,n,prof,i_end,j_end); if(!r.isEmpty()){ r="LR "+r; return r; } } if(x.LL!=null){ r=visit(x.LL,n,prof,i_end,j_end); if(!r.isEmpty()){ r="LL "+r; return r; } } if(x.L!=null){ r=visit(x.L,n,prof,i_end,j_end); if(!r.isEmpty()){ r="L "+r; return r; } } } else{ a=x.i-2; b=x.j-1; if(a>=0 && b>=0){ if(places[a][b]==0){ cuenta=1; x.UL= new Node(a,b); places[a][b]=1; if(a==i_end && b==j_end){ r="UL"; } } } a=x.i-2; b=x.j+1; if(a>=0 && b>=0 && a<=n-1 && b<=n-1){ if(places[a][b]==0){ cuenta=1; x.UR= new Node(a,b); places[a][b]=1; if(a==i_end && b==j_end){ r="UR"; } } } a=x.i; b=x.j+2; if(a>=0 && b>=0 && a<=n-1 && b<=n-1){ if(places[a][b]==0){ cuenta=1; x.R= new Node(a,b); places[a][b]=1; if(a==i_end && b==j_end){ r="R"; } } } a=x.i+2; b=x.j+1; if(a>=0 && b>=0 && a<=n-1 && b<=n-1){ if(places[a][b]==0){ cuenta=1; x.LR= new Node(a,b); places[a][b]=1; if(a==i_end && b==j_end){ r="LR"; } } } a=x.i+2; b=x.j-1; if(a>=0 && b>=0 && a<=n-1 && b<=n-1){ if(places[a][b]==0){ cuenta=1; x.LL= new Node(a,b); places[a][b]=1; if(a==i_end && b==j_end){ r="LL"; } } } a=x.i; b=x.j-2; if(a>=0 && b>=0 && a<=n-1 && b<=n-1){ if(places[a][b]==0){ cuenta=1; x.L= new Node(a,b); places[a][b]=1; if(a==i_end && b==j_end){ r="L"; } } } } return r; } static void printShortestPath(int n, int i_start, int j_start, int i_end, int j_end) { places=new int[n][n]; places[i_start][j_start]=1; Node root; root = new Node(i_start,j_start); int a=1,c=0; String r=""; while(a==1){ r=visit(root,n,c,i_end,j_end); if(places[i_end][j_end]==1){ a=0; } if (cuenta==0){ r="Impossible"; a=0; } else{ cuenta=0; } c++; } if(!r.equals("Impossible")){ System.out.println(c); } System.out.println(r); // Print the distance along with the sequence of moves. } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int i_start = in.nextInt(); int j_start = in.nextInt(); int i_end = in.nextInt(); int j_end = in.nextInt(); printShortestPath(n, i_start, j_start, i_end, j_end); in.close(); } }