Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Dslr {
  4.    
  5.     static int T;
  6.     static int A;
  7.     static int B;
  8.     static int[] d = {1,2,3,4};
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.        
  12.         T = sc.nextInt();
  13.        
  14.         for(int i=1;i<=T;i++){
  15.             A = sc.nextInt();
  16.             B = sc.nextInt();
  17.            
  18.             bfs(A);
  19.            
  20.         }
  21.    
  22.     }
  23.     public static void bfs(int n){
  24.         Queue <Node> q = new LinkedList<Node>();
  25.        
  26.        
  27.         q.add(new Node(n,0,""));
  28.        
  29.         while(!q.isEmpty()){
  30.             Node node = q.poll();
  31.             int x = node.x;
  32.             String str = node.str;
  33.             int direction = node.direction;
  34.            
  35.             if(x == B){
  36.                 System.out.println(str);
  37.                 return;
  38.             }
  39.                
  40.             for(int i=0;i<4;i++){
  41.                 direction = d[i];
  42.                 int nx = 0;
  43.                 String result = str;
  44.                 if(direction==1){
  45.                     nx = x*2;
  46.                     if(nx>9999){
  47.                         nx = nx%10000;
  48.                     }
  49.                     result+="D";
  50.                 }
  51.                 else if(direction==2){
  52.                     nx = x-1;
  53.                     if(nx==0){
  54.                         nx = 9999;
  55.                     }
  56.                     result+="S";
  57.                 }
  58.                 else if(direction==3){
  59.                     nx = rotate(x,1);
  60.                     result+="L";
  61.                 }
  62.                 else if(direction==4){
  63.                     nx = rotate(x,2);
  64.                     result+="R";
  65.                 }
  66.                 q.add(new Node(nx,direction,result));
  67.             }
  68.         }
  69.     }
  70.     public static int rotate(int input, int LR){
  71.         int d1 = input/1000;
  72.         int m1 = input%1000;
  73.        
  74.         int d2 = m1/100;
  75.         int m2 = m1%100;
  76.        
  77.         int d3 = m2/10;
  78.         int d4 = m2%10;
  79.        
  80.         if(LR==1){//left
  81.             int temp = d1;
  82.             d1 = d2;
  83.             d2 = d3;
  84.             d3 = d4;
  85.             d4 = temp;
  86.         }
  87.         else if(LR==2){//right
  88.             int temp = d4;
  89.             d4 = d3;
  90.             d3 = d2;
  91.             d2 = d1;
  92.             d1 = temp;
  93.         }
  94.        
  95.         int sum = d1*1000+d2*100+d3*10+d4;
  96.        
  97.         return sum;
  98.     }
  99.     static class Node{
  100.         int x;
  101.         int direction;
  102.         String str;
  103.        
  104.         Node(int x, int direction, String str){
  105.             this.x = x;
  106.             this.direction = direction;
  107.             this.str = str;
  108.            
  109.         }
  110.     }
  111.    
  112.    
  113.  
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement