-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboj_2529.java
62 lines (58 loc) · 1.74 KB
/
boj_2529.java
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
package backtracking;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class boj_2529 {
static int n;
static char[] arr;
static boolean[] visit=new boolean[10]; //0~9 방문 여부
static List<String> result=new ArrayList<>();
static String minNum="9999999999";
static String maxNum="0";
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
arr=new char[n];
StringTokenizer st=new StringTokenizer(br.readLine());
for(int i=0;i<n;i++){
arr[i]=st.nextToken().charAt(0);
}
for(int i=0;i<10;i++){
visit[i]=false;
}
dfs(0,"");
System.out.println(maxNum);
System.out.println(minNum);
}
public static void dfs(int level,String num){
if (level==n+1){
if(Long.parseLong(num)>Long.parseLong(maxNum)){
maxNum=num;
}
if(Long.parseLong(num)<Long.parseLong(minNum)){
minNum=num;
}
return;
}
for(int i=0;i<10;i++){
if(!visit[i]){
if(level==0 || possible(num.charAt(level-1)-'0',i,arr[level-1])){
visit[i]=true;
dfs(level+1,num+i);
visit[i]=false;
}
}
}
}
public static boolean possible(int a, int b, char operator) {
if(operator=='<'){
return a<b;
}
else{ //< 일 경우
return a>b;
}
}
}