๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/17265)
< ๋์ ์ธ์์๋ ์ํ๊ณผ ํจ๊ป >
๋ฌธ์ ํ์ด
bfs๋ฅผ ํ์ฉํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
โ ๋ฐฐ์ด์ ์ ์ด๋ ๊ฒ ๋ง์ด ์ผ๋๊ฐ?
์ต๋๊ฐ๊ณผ ์ต์๊ฐ์ ์ ์ฅํ๋ ๋ถ๋ถ๊ณผ ๋ฐฉ๋ฌธ ์ฌ๋ถ๋ฅผ ํ์ํ ๋ ๊ฐ๊ฐ ๋ฐ๋ก ๋ฐฐ์ด์ ๋ฌ์ ํ์ธํ๋ ๊ฒ์ด ํธํ๋ค๊ณ ์๊ฐํ๊ธฐ ๋๋ฌธ์ด๋ค.
โ ์ต๋๊ฐ๊ณผ ์ต์๊ฐ์ ๋ฐฐ์ด์ ํน์ ๊ฐ์ผ๋ก ์ค์ ํ์ฌ ๋ฐฉ๋ฌธ ๋ฐฐ์ด์ ๋ฐ๋ก ๋ง๋ค์ง ์๊ณ ๋ฐฉ๋ฌธ ํ์ ์ฌ๋ถ๋ฅผ ํ์ธํด๋ ๋์ง ์๋๊ฐ?
์ฒ์์๋ ๊ฐ ๋ฐฐ์ด์ ๊ฐ์ -1๋ก ์ด๊ธฐํํ ํ -1์ผ ๊ฒฝ์ฐ์ ๋ฌด์กฐ๊ฑด ๊ฐ์ ์ ๋ฐ์ดํธํ๋๋ก ๊ตฌํํ์๋ค. ๊ทธ ๊ฒฐ๊ณผ ํ๋ ธ์ต๋๋ค๊ฐ ์ถ๋ ฅ๋์๋ค. ๊ทธ ์ด์ ๋ ์ฐ์ฐํ ๊ฐ์ผ๋ก -1์ด๋ฉฐ ๊ทธ ๊ฐ์ด ์ต์๊ฐ์ธ ๊ฒฝ์ฐ์๋ ๋ฌด์กฐ๊ฑด ๋ค๋ฅธ ๊ฐ์ผ๋ก ์ ๋ฐ์ดํธํด์ ์ ํํ ๊ฐ์ ์ป์ง ๋ชปํด ํ๋ ธ๋ค๊ณ ์๊ฐํ๋ค. ๊ทธ๋์ ๋ฐฉ๋ฌธ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ ๋ฐฐ์ด์ ๋ฐ๋ก ๊ตฌํํ๋ค.
โ ์ฐ์ฐ์์ธ ๊ฒฝ์ฐ์๋ ์ ๋ฐฉ๋ฌธ ์ฌ๋ถ๋ฅผ ํ์ํ์ง ์์๋๊ฐ?
๋ฐฉ๋ฌธ์ฌ๋ถ๋ ํผ์ฐ์ฐ์์ผ ๊ฒฝ์ฐ์๋ง ๋ฐฉ๋ฌธ ์ฌ๋ถ๋ก ํ์ํ๋ค. ์ฐ์ฐ์๋ฅผ ๋ฐฉ๋ฌธ ํ์๋ฅผ ํ ๊ฒฝ์ฐ ํผ์ฐ์ฐ์๊ฐ ๋ฌ๋ผ์ง๋ฉด ์ด๋ฏธ ๋ฐฉ๋ฌธํ ๊ณณ์ผ๋ก
์ฐ์ฐํ์ง ๋ชปํ๋ ๊ฒฝ์ฐ๊ฐ ์๊ธด๋ค๊ณ ์๊ฐํ๊ธฐ ๋๋ฌธ์ด๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main { // ๋์ ์ธ์์๋ ์ํ๊ณผ ํจ๊ป
static char arr[][];
static int dx[] = { 0, 1 };
static int dy[] = { 1, 0 };
static int max_arr[][], min_arr[][], n;
static boolean max_visited[][], min_visited[][];
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
n = Integer.parseInt(bf.readLine());
arr = new char[n][n];
max_arr = new int[n][n];
min_arr = new int[n][n];
max_visited=new boolean[n][n];
min_visited=new boolean[n][n];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(bf.readLine());
for (int j = 0; j < n; j++) {
arr[i][j] = st.nextToken().charAt(0);
}
}
bfs(0, 0, arr[0][0] + "");
System.out.println(max_arr[n - 1][n - 1] + " " + min_arr[n - 1][n - 1]);
}
private static void bfs(int i, int j, String str) {
Queue<String[]> queue = new LinkedList<>();
queue.add(new String[] { Integer.toString(i), Integer.toString(j), str+" " });
max_visited[i][j]=true;
min_visited[i][j]=true;
while (!queue.isEmpty()) {
String temp[] = queue.poll();
for (int k = 0; k < 2; k++) {
int x = Integer.parseInt(temp[0]) + dx[k];
int y = Integer.parseInt(temp[1]) + dy[k];
if (x >= 0 && x < n && y >= 0 && y < n) {
if (!Character.isDigit(arr[x][y])) { // ์ฐ์ฐ์
queue.add(new String[] { Integer.toString(x), Integer.toString(y), temp[2] +arr[x][y] });
} else { // ์ซ์
String[] num_arr=temp[2].split(" ");
int a = Integer.parseInt(num_arr[0]);
char op = num_arr[1].charAt(0);
int b = arr[x][y] - '0';
int result = -1;
if (op == '+') {
result = a + b;
} else if (op == '-') {
result = a - b;
} else if (op == '*') {
result = a * b;
}
if (!max_visited[x][y] || max_arr[x][y] < result) {
max_visited[x][y]=true;
max_arr[x][y] = result;
}
if (!min_visited[x][y] || min_arr[x][y] > result) {
min_visited[x][y]=true;
min_arr[x][y] = result;
}
if(max_arr[x][y]==min_arr[x][y]) {
queue.add(new String[] { Integer.toString(x), Integer.toString(y), max_arr[x][y] + " " });
}else {
queue.add(new String[] { Integer.toString(x), Integer.toString(y), max_arr[x][y] + " " });
queue.add(new String[] { Integer.toString(x), Integer.toString(y), min_arr[x][y] + " " });
}
}
}
}
}
}
}
Main
- ์ง๋์ ํฌ๊ธฐ(n) ์ ๋ ฅ
- arr: ์ง๋ ์ ๋ณด ์ ์ฅ
min_arr, max_arr : ์ฐ์ฐ ๊ฒฐ๊ณผ์ ์ต๋๊ฐ ์ต์๊ฐ ์ ์ฅ
min_visited, max_visited: ์ต๋๊ฐ๊ณผ ์ต์๊ฐ ์ฐ์ฐ์ ๋ฐฉ๋ฌธ ์ฌ๋ถ ์ ์ฅ
- bfs ํจ์ ํธ์ถ ํ ์ฐ์ฐ ๊ฒฐ๊ณผ์ ์ต๋๊ฐ, ์ต์๊ฐ ์ถ๋ ฅ
bfs
- queue : [x์ขํ, y์ขํ, ์ซ์+์ฐ์ฐ์]
- queue๊ฐ ๋น ๋๊น์ง ๋ฐ๋ณต -> ์ค๋ฅธ์ชฝ๊ณผ ์๋์ชฝ์ผ๋ก ์ด๋ -> ๊ทธ ๊ฐ์ด ์ฐ์ฐ์๋ผ๋ฉด queue์ ์ ์ฅ / ์ซ์๋ผ๋ฉด ํผ์ฐ์ฐ์์ ์ฐ์ฐ์ ๊ตฌ๋ถํ์ฌ ์ฐ์ฐํ ๊ฐ ์ ์ฅ ํ ์ต๋๊ฐ๊ณผ ์ต์๊ฐ ์ ๋ฐ์ดํธ ๋ฐ queue์ ์ ์ฅ
์๊ฐ๐ค

'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 9944_NxM ๋ณด๋ ์์ฃผํ๊ธฐ (0) | 2023.06.01 |
---|---|
[Baekjoon] 23747_์๋ (0) | 2023.05.30 |
[Baekjoon] 14719_๋น๋ฌผ (0) | 2023.05.29 |
[Baekjoon] 16509_์ฅ๊ตฐ (1) | 2023.05.28 |
[Baekjoon] 14217_๊ทธ๋ํ ํ์ (0) | 2023.05.26 |