-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboj_14888.java
124 lines (86 loc) · 2.84 KB
/
boj_14888.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Took 1 hr
// DFS + backTracking
// State: calculated result so far, calculated number count, remained operator number
// Check the state vertex, so don't enter that vertex again.......
// isV : N-1 (total operator number) * 4 (+, -, *, /)
// At first, while loop which is not needed should be coded, i think.....
// that causes inifinite loop
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.util.Stack;
import java.util.LinkedList;
class boj_14888{
static int N;
static int[] A;
static int[] ops;
static int[][] isV;
static final int PLUS = 0;
static final int MINUS = 1;
static final int MULTI = 2;
static final int DEVIS = 3;
static int ANSWER_MIN;
static int ANSWER_MAX;
static Stack<Integer> stack;
public static void main(String[] args) throws IOException{
BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter BW = new BufferedWriter(new OutputStreamWriter(System.out));
N = Integer.parseInt(BR.readLine());
A = new int[N+1];
isV = new int[N][4];
ops = new int[4];
stack = new Stack<>();
ANSWER_MIN = Integer.MAX_VALUE;
ANSWER_MAX = Integer.MIN_VALUE;
String as = BR.readLine();
String[] asSplit = as.split(" ");
for (int n=1; n<=N; n++) {
A[n] = Integer.parseInt(asSplit[n-1]);
} // we got As.......
String operStr = BR.readLine();
String[] operStrSplit = operStr.split(" ");
for (int n=0; n<4; n++) {
ops[n] = Integer.parseInt(operStrSplit[n]);
}// we got operators
stack.push(A[1]); // result
stack.push(1); // caculated
DFS();
System.out.println(ANSWER_MAX);
System.out.println(ANSWER_MIN);
}// end of main method
public static void DFS(){
// end condition
int calculated = stack.pop();
int result = stack.pop();
if(calculated==N){
//System.out.println("Came here??");
ANSWER_MIN = result < ANSWER_MIN? result : ANSWER_MIN;
ANSWER_MAX = result > ANSWER_MAX? result : ANSWER_MAX;
return;
}
for (int i=0; i<4; i++) {
int newResult;
if(ops[i] > 0){
// if we have additional operator
// plus
if(i==0 && isV[calculated][i] != 1) newResult = result + A[calculated+1];
// minus
else if(i==1 && isV[calculated][i] != 1 ) newResult = result - A[calculated+1];
// multiply
else if(i==2 && isV[calculated][i] != 1 ) newResult = result * A[calculated+1];
// division
else if(i==3 && isV[calculated][i] != 1) newResult = result / A[calculated+1];
else continue;
ops[i]--;
stack.push(newResult);
stack.push(calculated+1);
isV[calculated][i] = 1;
DFS();
isV[calculated][i] = 0;
ops[i]++;
} // end of additional operator
}// end of for loop, adjacent vertex DFS
} // end of DFS method
}// end of class