반응형
    
    
    
  https://www.acmicpc.net/problem/9019
9019번: DSLR
문제 네 개의 명령어 D, S, L, R 을 이용하는 간단한 계산기가 있다. 이 계산기에는 레지스터가 하나 있는데, 이 레지스터에는 0 이상 10,000 미만의 십진수를 저장할 수 있다. 각 명령어는 이 레지스터에 저장된 n을 다음과 같이 변환한다. n의 네 자릿수를 d1, d2, d3, d4라고 하자(즉 n = ((d1 × 10 + d2) × 10 + d3) × 10 + d4라고 하자) D: D 는 n을 두 배로 바꾼다. 결과 값이 9999 보다 큰 경
www.acmicpc.net
BFS 탐색 문제이지만 이동방향이 D S R L 네가지로 주어진다.
문자열을 처리하기위해 뒤로가는것은 앞으로 9999번가고 나머지연산을 하는것과 같닫는것을 이용하면
소스코드를 많이 줄일수가 있다.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | package com.ssay.algo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; public class B9019 {     static int T;     static int A,B;     static boolean visit[];     static class Pair{         int n;         String sb;         public Pair(int n, String sb) {             super();             this.n = n;             this.sb = sb;         }     }     public static void main(String[] args) throws NumberFormatException, IOException {         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         T = Integer.parseInt(br.readLine());         for(int t=1;t<=T;t++) {             String str[] = br.readLine().split(" ");             A = Integer.parseInt(str[0]);             B = Integer.parseInt(str[1]);             visit = new boolean[10000];             String Answer = null;             Queue<Pair> q = new LinkedList<Pair>();             q.add(new Pair(A, ""));             visit[A] = true;             while(!q.isEmpty()) {                 boolean isEscape = false;                 int size = q.size();                 for(int s=0;s<size;s++) {                     Pair tmp = q.poll();                     if(B == tmp.n) {                         isEscape = true;                         Answer = tmp.sb;                         break;                     }                     char c[] = new char[4];                     int next;                     //D                         next = (tmp.n*2)%10000;                         if(!visit[next]) {                             visit[next] = true;                         }                     //S                         next = (tmp.n + 9999) %10000;                         if(!visit[next]) {                             visit[next] = true;                         }                     int nn = tmp.n;                     for(int j=3;j>=0;j--) {                         c[j] = (char) (nn %10 +'0');                         nn = nn /10;                     }                     char copy[] = new char[4];                     //L                     for(int j=0;j<4;j++) {                         copy[j] = c[(j+1)%4];                     }                     nn = Integer.parseInt(String.valueOf(copy));                     if(!visit[nn]) {                         visit[nn] = true;                     }                     //R                     for(int j=0;j<4;j++) {                         copy[j] = c[(j+3)%4];                     }                     nn = Integer.parseInt(String.valueOf(copy));                     if(!visit[nn]) {                         visit[nn] = true;                     }                 }                 if(isEscape) break;             }             System.out.println(Answer);         }     } } http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter | 
반응형
    
    
    
  'ProgramSoliving' 카테고리의 다른 글
| 백준 : 1967 (0) | 2020.02.24 | 
|---|---|
| 백준 : 2250 (0) | 2020.02.24 | 
| 백준 : 9466 (텀 프로젝트) (0) | 2020.02.23 | 
| 백준 : 1707 (0) | 2020.02.22 | 
| 백준 : 4179 (0) | 2020.02.20 | 
