λ°μν
https://www.acmicpc.net/problem/16943
λ¬Έμ
λ μ μ Aμ Bκ° μμ λ, Aμ ν¬ν¨λ μ«μμ μμλ₯Ό μμ΄μ μλ‘μ΄ μ Cλ₯Ό λ§λ€λ €κ³ νλ€. μ¦, Cλ Aμ μμ΄ μ€ νλκ° λμ΄μΌ νλ€.
κ°λ₯ν C μ€μμ Bλ³΄λ€ μμΌλ©΄μ, κ°μ₯ ν° κ°μ ꡬν΄λ³΄μ. Cλ 0μΌλ‘ μμνλ©΄ μ λλ€.
μ λ ₯
첫째 μ€μ λ μ μ Aμ Bκ° μ£Όμ΄μ§λ€.
μΆλ ₯
Bλ³΄λ€ μμ Cμ€μμ κ°μ₯ ν° κ°μ μΆλ ₯νλ€. κ·Έλ¬ν Cκ° μλ κ²½μ°μλ -1μ μΆλ ₯νλ€.
νμ΄
DFSλ₯Ό μ΄μ©
DFS λ‘ Aλ₯Ό λ€μν μλ‘ μ‘°ν©νκ³ λͺ¨λ μ¬μ©νλ€λ©΄ Bμ λΉκ΅ ν, μμ κ°μ΄λ©΄ κΈ°μ‘΄μ κ°κ³Ό λ ν° κ°μ μ ν βΌοΈ
μ½λ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main_BOJ_16943_μ«μμ¬λ°°μΉ {
static String A, B, C;
static int a,b,c;
static boolean[] isVisited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stringTokenizer = new StringTokenizer(br.readLine());
A = stringTokenizer.nextToken();
B = stringTokenizer.nextToken();
C = "";
a = Integer.parseInt(A);
b = Integer.parseInt(B);
c = -1;
isVisited = new boolean[A.length()];
DFS();
System.out.println(c);
}
private static void DFS() {
if(C.length() == A.length()){
if(Integer.parseInt(C) < b)
c = Math.max(c, Integer.parseInt(C));
return;
}
for(int i = 0 ; i < A.length(); i++){
if((C.length() == 0 && A.charAt(i) == '0') || isVisited[i]) continue;
isVisited[i] = true;
C += A.charAt(i);
DFS();
isVisited[i] = false;
C = C.substring(0, C.length()-1);
}
}
}
728x90
λ°μν
'μκ³ λ¦¬μ¦ > λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[ λ°±μ€ / BOJ 3190 ] λ± ( μλ° / JAVA ) (0) | 2021.11.02 |
---|---|
[ λ°±μ€ / BOJ 9461 ] νλλ° μμ΄ ( μλ° / JAVA ) (0) | 2021.11.02 |
[ λ°±μ€ / BOJ 18405 ] κ²½μμ μ μΌ ( μλ° / JAVA ) (0) | 2021.10.28 |
[ λ°±μ€ / BOJ 1715 ] μΉ΄λ μ λ ¬νκΈ° ( μλ° / JAVA ) (0) | 2021.10.28 |
[ λ°±μ€ / BOJ 1717 ] μ§ν©μ νν ( μλ° / JAVA ) (0) | 2021.10.27 |