๐ ๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/16637
๐ฎ ๋ฌธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ
๋ฌธ์ ์์ ์ฃผ์๊น๊ฒ ๋ด์ผํ๋ ์กฐ๊ฑด์ด ๋ช๊ฐ์ง ์๋ค.
- ๋ชจ๋ ์ฐ์ฐ์์ ์ฐ์ ์์๋ ๋์ผํ๊ธฐ ๋๋ฌธ์ ์์ ๊ณ์ฐ์ ์ผ์ชฝ ๋ถํฐ ์ฐจ๋ก๋ก ๊ณ์ฐํ๋ค.
- ๊ดํธ ์์๋ ์ฐ์ฐ์๊ฐ ํ ๊ฐ๋ง ๋ค์ด๊ฐ๋ค.
1๋ฒ ์กฐ๊ฑด์ ์ถฉ์กฑํ๊ธฐ ์ํด์ ์ฌ๊ทํจ์ ํธ์ถ ์ ๋ง๋ค ํ์ฌ๊น์ง์ ๊ฒฐ๊ณผ ๊ฐ(ouput ํ๋ผ๋ฏธํฐ)๊ณผ ๊ดํธ ์ถ๊ฐํ ๊ฒฝ์ฐ ๊ฒฐ๊ณผ๊ฐ ํน์ ๋ค์ ์ฐจ๋ก์ ํผ์ฐ์ฐ์์ ๊ณ์ฐ์ ํ ๊ฒฐ๊ณผ๊ฐ์ ๋ค์ ์ฌ๊ท ํธ์ถ์ ๋ฃ์ด์ฃผ์ด์ผ ํ๋ค.
2๋ฒ ์กฐ๊ฑด์ ์ถฉ์กฑํ๊ธฐ ์ํด์ **ํ์ฌ ์ฐจ๋ก๋ก ๋ถํฐ ๋ค์ ์ฐจ๋ก์ ๋ค๋ค์ ์ฐจ๋ก (index + 1, index + 2)**์ ํผ์ฐ์ฐ์๋ฅผ ๋จผ์ ๊ณ์ฐํด์ค๋ค. ๊ทธ ํ ํ์ฌ๊น์ง์ ๊ฒฐ๊ณผ ๊ฐ๊ณผ ๋ค์ ํ๋ฒ ๊ณ์ฐ ํ๋ฉด ๊ดํธ๋ฅผ ๋ฐ์ํ ๊ฒฐ๊ณผ๊ฐ์ ๊ตฌํ ์ ์๋ค.
๊ดํธ๋ฅผ ์ฌ์ฉํ ๊ฒฐ๊ณผ๊ฐ์ ๊ณ์ฐํ ๊ฒฝ์ฐ index+2 ๊น์ง์ ํผ์ฐ์ฐ์๊น์ง ๊ณ์ฐ ํด์ฃผ์์ผ๋ฏ๋ก, ์ฌ๊ท ํธ์ถ ์ index + 2๊ฐ์ ๋ฃ์ด์ค๋ค.
๊ดํธ๋ฅผ ๊ณ์ฐํ์ง ์๊ณ ์ผ๋ฐ ๊ณ์ฐ ๋จ๊ณ๋ง ์งํํ๋ ๊ฒฝ์ฐ ํ์ฌ ์ฐจ๋ก์ ๋ค์ ์ฐจ๋ก ํผ์ฐ์ฐ์ ๊ฐ์ ๊ณ์ฐํด ์ฌ๊ท ํธ์ถ๋ก ๋ฃ์ด์ค๋ค.
์ ๋ฆฌํ์๋ฉด,
- ๊ดํธ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ num[index + 1], op[index+1], num[index + 2]๊ฐ์ ๊ณ์ฐ ํ ํด๋น ๊ฐ์ output, op[index]๋ก ๊ณ์ฐํ๋ค.
- ์ด ๋, index + 2 ๊น์ง ๊ณ์ฐ ํ์ผ๋ฏ๋ก index + 2๋ฅผ ๋ฃ์ด ์ฌ๊ท๋ฅผ ํธ์ถํ๋ค.
- ๊ดํธ๋ฅผ ์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ output, op[index], num[index+1]๊ฐ์ ๊ณ์ฐ ํ index + 1๋ฅผ ๋ฃ์ด ์ฌ๊ท๋ฅผ ํธ์ถ ํ๋ค.
- ์ฐ์ฐ์๋ฅผ ๋ชจ๋ ์ฌ์ฉํ ๊ฒฝ์ฐ ์ต๋๊ฐ์ ๋น๊ตํด ๊ฐฑ์ ํ๋ค.
- ๊ดํธ ์ฌ์ฉ ๋ฐฐ์ด ์ธ๋ฑ์ค๋ฅผ ์ ์กฐ์ ํด์ฃผ๋ ๊ฒ์ด ์ค์ํ๋ค.
๐ฉ Java ์ฝ๋
package com.algorithm.Baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main_16637_๊ดํธ_์ถ๊ฐํ๊ธฐ {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] num = new int[N / 2 + 1];
char[] op = new char[N / 2];
char[] exp = br.readLine().toCharArray();
for (int index = 0; index < N; index++) {
if (index % 2 == 0) num[index / 2] = Integer.parseInt(String.valueOf(exp[index]));
else op[index / 2] = exp[index];
}
int rst = getMaxResult(num, op);
System.out.println(rst);
br.close();
}
private static int getMaxResult(int[] num, char[] op) {
result = Integer.MIN_VALUE;
makeExp(num[0], 0, num, op);
return result;
}
private static int result;
private static void makeExp(int output, int index, int[] num, char[] op) {
// ์ฐ์ฐ์ ๋ชจ๋ ์ฌ์ฉํ ๊ฒฝ์ฐ
if (index >= op.length) {
// ์ต๋๊ฐ ๊ฐฑ์
result = Math.max(result, output);
return;
}
// ๊ดํธ ๊ณ์ฐ ํ๋ ๊ฒฝ์ฐ
// ์ซ์ ๋ฐฐ์ด์ด ๋์ด๊ฐ์ง ์๋๋ก
if (index + 2 < num.length) {
// ํ์ฌ ์ซ์ ๋ฐฐ์ด ์ธ๋ฑ์ค๋ณด๋ค ๋ค์ ๋๊ฐ์ ์ซ์์ ๊ดํธ ๊ณ์ฐ
int rst = calc(num[index + 1], op[index + 1], num[index + 2]);
// ํ์ฌ๊น์ง์ ouput ๊ณผ ๋ค์ ๋๊ฐ ๊ณ์ฐ ๊ฐ ๊ณ์ฐ
makeExp(calc(output, op[index], rst), index + 2, num, op);
}
// ๊ดํธ ๊ณ์ฐ ํ์ง ์๋ ๊ฒฝ์ฐ
// ํ์ฌ๊น์ง์ output๊ณผ ๋ค์ ์ซ์ ๊ณ์ฐ
int rst = calc(output, op[index], num[index+1]);
makeExp(rst, index + 1, num ,op);
}
private static int calc(int prev, char operation, int post) {
if (operation == '+') return prev + post;
if (operation == '-') return prev - post;
return prev * post;
}
}
'Algorithm > ์์ ํ์(Brute-Force)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค, Java] 9663๋ฒ : N-Queen (0) | 2022.05.16 |
---|---|
[๋ฐฑ์ค, Java] 1062๋ฒ : ๊ฐ๋ฅด์นจ (0) | 2022.05.10 |