-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_17265.java
More file actions
74 lines (59 loc) · 2.52 KB
/
BOJ_17265.java
File metadata and controls
74 lines (59 loc) · 2.52 KB
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
import java.io.*;
import java.util.*;
public class BOJ_17265 {
// 하 우
static int[] dr = {1, 0};
static int[] dc = {0, 1};
static int N;
static String[][] map;
static int max = Integer.MIN_VALUE;
static int min = Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
map = new String[N][N];
StringTokenizer st;
for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < N; j++) {
map[i][j] = st.nextToken();
}
}
bfs(0, 0, Integer.parseInt(map[0][0]), 4, 1);
System.out.println(max + " " + min);
}
static void bfs(int r, int c, int num, int operator, int dist) {
Deque<int[]> q = new ArrayDeque<>();
q.add(new int[] {r, c, num, operator, dist});
while(!q.isEmpty()) {
// System.out.println("r: " + r + ", c: " + c + ", num: " + num + ", operator: " + operator + ", dist: " + dist);
int[] cur = q.poll();
int cNum = cur[2], cOperator = cur[3], cDist = cur[4];
if(cur[0] == N-1 && cur[1] == N-1) {
max = Math.max(max, cNum);
min = Math.min(min, cNum);
// System.out.println("result -> " + max + " " + min);
}
for(int i = 0; i < 2; i++) {
int nr = cur[0] + dr[i], nc = cur[1] + dc[i];
// System.out.println("nr = " + nr + ", nc = " + nc);
if(nr < 0 || nr >= N || nc < 0 || nc >= N) continue;
if(cDist % 2 == 1) { // 연산자
if(map[nr][nc].equals("*")) {
q.add(new int[] {nr, nc, cNum, 0, cDist+1});
} else if (map[nr][nc].equals("+")) {
q.add(new int[] {nr, nc, cNum, 1, cDist+1});
} else {
q.add(new int[] {nr, nc, cNum, 2, cDist+1});
}
} else { // 숫자(피연산자)
int nextNum;
if(cOperator == 0) nextNum = cNum * Integer.parseInt(map[nr][nc]);
else if(cOperator == 1) nextNum = cNum + Integer.parseInt(map[nr][nc]);
else nextNum = cNum - Integer.parseInt(map[nr][nc]);
q.add(new int[] {nr, nc, nextNum, 4, cDist+1});
}
}
}
}
}