Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Dslr {
- static int T;
- static int A;
- static int B;
- static int[] d = {1,2,3,4};
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- T = sc.nextInt();
- for(int i=1;i<=T;i++){
- A = sc.nextInt();
- B = sc.nextInt();
- bfs(A);
- }
- }
- public static void bfs(int n){
- Queue <Node> q = new LinkedList<Node>();
- q.add(new Node(n,0,""));
- while(!q.isEmpty()){
- Node node = q.poll();
- int x = node.x;
- String str = node.str;
- int direction = node.direction;
- if(x == B){
- System.out.println(str);
- return;
- }
- for(int i=0;i<4;i++){
- direction = d[i];
- int nx = 0;
- String result = str;
- if(direction==1){
- nx = x*2;
- if(nx>9999){
- nx = nx%10000;
- }
- result+="D";
- }
- else if(direction==2){
- nx = x-1;
- if(nx==0){
- nx = 9999;
- }
- result+="S";
- }
- else if(direction==3){
- nx = rotate(x,1);
- result+="L";
- }
- else if(direction==4){
- nx = rotate(x,2);
- result+="R";
- }
- q.add(new Node(nx,direction,result));
- }
- }
- }
- public static int rotate(int input, int LR){
- int d1 = input/1000;
- int m1 = input%1000;
- int d2 = m1/100;
- int m2 = m1%100;
- int d3 = m2/10;
- int d4 = m2%10;
- if(LR==1){//left
- int temp = d1;
- d1 = d2;
- d2 = d3;
- d3 = d4;
- d4 = temp;
- }
- else if(LR==2){//right
- int temp = d4;
- d4 = d3;
- d3 = d2;
- d2 = d1;
- d1 = temp;
- }
- int sum = d1*1000+d2*100+d3*10+d4;
- return sum;
- }
- static class Node{
- int x;
- int direction;
- String str;
- Node(int x, int direction, String str){
- this.x = x;
- this.direction = direction;
- this.str = str;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement