fork download
  1. import java.util.*;
  2. public class Main {
  3. static boolean[] check;
  4. static String[] visited;
  5.  
  6. public static int getL(int n) {
  7.  
  8. int value = (n % 1000) * 10;
  9. return value + n / 1000;
  10. }
  11.  
  12. public static int getR(int n) {
  13.  
  14. int value = (n % 10) * 1000;
  15. return value + n / 10;
  16.  
  17. }
  18.  
  19. public static boolean visit(int n) {
  20. if(check[n] == false) return true;
  21. else return false;
  22. }
  23. public static void makeVisited(Queue<Integer> q, String[] visited, int current, int next, char order) {
  24.  
  25. q.add(next);
  26. visited[next] = visited[current] + order;
  27. // System.out.println(next);
  28. check[next] = true;
  29.  
  30.  
  31.  
  32. }
  33.  
  34. public static void main(String[] args) {
  35. // TODO Auto-generated method stub
  36.  
  37. Scanner sc = new Scanner(System.in);
  38. int n = sc.nextInt();
  39.  
  40.  
  41. for(int i = 0; i < n ; i++) {
  42. Queue<Integer> q = new LinkedList<Integer>();
  43. check = new boolean[100001];
  44. visited = new String[100001];
  45. int start = sc.nextInt();
  46. int end = sc.nextInt();
  47.  
  48.  
  49. q.add(start);
  50. visited[start] = "";
  51.  
  52. while(!q.isEmpty()) {
  53. int current = q.poll();
  54.  
  55. if(current == end) break;
  56.  
  57. // Case of D
  58. int D = current * 2 > 10000 ? (current *2) % 10000 : current * 2;
  59. if(visit(D)) makeVisited(q, visited, current, D, 'D');
  60.  
  61. int S = current == 0 ? 10000 : current;
  62. S = S - 1;
  63. // int S = current == 0 ? 9999 : current - 1;
  64. if(visit(S)) makeVisited(q, visited, current, S, 'S');
  65.  
  66. int L = getL(current);
  67. if(visit(L)) makeVisited(q, visited, current, L, 'L');
  68.  
  69. int R = getR(current);
  70. if(visit(R)) makeVisited(q, visited, current, R, 'R');
  71.  
  72. }
  73.  
  74. for(int j = 0; j <visited[end].length(); j++) {
  75. System.out.print(visited[end].charAt(j));
  76. }
  77. System.out.println();
  78. }
  79.  
  80. sc.close();
  81.  
  82. }
  83.  
  84. }
  85.  
Success #stdin #stdout 0.13s 37964KB
stdin
1
5000 1000
stdout
DR