BOJ - [1463 - 1๋ก ๋ง๋ค๊ธฐ] - Silver III๐ฅ
https://www.acmicpc.net/problem/1463 - ๋ฌธ์ ์ค๋ช : n๊ฐ์ด ์ฃผ์ด์ง๋ฉด 3๊ฐ์ ๊ณผ์ ์ค ํ๋๋ฅผ ๊ณ ๋ฅด๋ ๊ณผ์ ์ ๋ฐ๋ณตํ์ฌ 1์ ๋๋ฌํ๊ธฐ๊น์ง ์ต์ ๊ณ์ฐ ํ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ . ์ ํ ๊ฐ๋ฅํ ๊ณผ์ ์ด 2๊ฐ์๋ค๋ฉด ์ด์ง ํธ๋ฆฌ ํ์์ด๋ ๊ณ ๋ฑํ๊ต ํ๋ฅ ๊ณผ ํต๊ณ ๊ณผ๋ชฉ์์ ๋ฐฐ์ฐ๋ ...
1. ๋ฌธ์
https://www.acmicpc.net/problem/1463
- **๋ฌธ์ ์ค๋ช
**:
n๊ฐ์ด ์ฃผ์ด์ง๋ฉด 3๊ฐ์ ๊ณผ์ ์ค ํ๋๋ฅผ ๊ณ ๋ฅด๋ ๊ณผ์ ์ ๋ฐ๋ณตํ์ฌ 1์ ๋๋ฌํ๊ธฐ๊น์ง ์ต์ ๊ณ์ฐ ํ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ .
์ ํ ๊ฐ๋ฅํ ๊ณผ์ ์ด 2๊ฐ์๋ค๋ฉด ์ด์ง ํธ๋ฆฌ ํ์์ด๋ ๊ณ ๋ฑํ๊ต ํ๋ฅ ๊ณผ ํต๊ณ ๊ณผ๋ชฉ์์ ๋ฐฐ์ฐ๋ โ๊ฐ์ ๊ฒ์ด ์๋ ์์ดโ ๊ฐ๋ ์ ํ์ฉํ์ฌ ์ํ์ ์ผ๋ก ํ์ ์๋ค.
๊ทธ๋ฌ๋ ์ด๋ฌธ์ ๋ ๊ณผ์ ์ด 3๊ฐ์ด๋ฏ๋ก ์ด์งํธ๋ฆฌ๋ ํด๋น์ฌํญ์ด ์๊ณ , ์ํ์ ์ผ๋ก ํ๊ธฐ์๋ ์๊ณ ๋ฆฌ์ฆ์ด ๋งค์ฐ ๋ณต์กํด์ง ๊ฐ๋ฅ์ฑ์ด ๋๋ค.
๋ฐ๋ผ์ ๊ธฐ๋ณธ์ ์ผ๋ก ๊น์ด ์ฐ์ ์ผ๋ก ํธ๋ฆฌ๋ฅผ ํ์(DFS)ํ๋๋ก ํ๊ณ ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ(DP)๋ฅผ ํ์ฉํ์ฌ ์ต์ ํ๋ฅผ ์งํํ์๋ค. - ์ฌ์ฉ ์๊ณ ๋ฆฌ์ฆ : DP, DFS
2. ํ์ด
2.1. ์ด๊ธฐ ์ฝ๋
- **์ฝ๋**:
```java
// ์ต์ ํ ๋์ง์์ DFS (์ ์์กฐ์ฌ)
import java.io.*; public class Main
{
static int minDepth = 1000000;
static void search(int n,int depth) {
if (n == 1) {
minDepth = Math.min(depth, minDepth);
return;
}
for(int i=0; i<2; i+=1)
if(n%(3-i) == 0) search(n/(3-i), depth+1);
search(n-1,depth+1);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
search(n,0);
bw.write(Integer.toString(minDepth));
bw.flush();
bw.close();
}
}
``` - **๋ฌธ์ ์ **:
- ์ค์ง DFS ๋ง์ผ๋ก 1์ ๋ง๋ค์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ์ ์ ์กฐ์ฌ ํจ์ผ๋ก์จ n๊ฐ์ด ์ปค์ง์๋ก ์คํ์๊ฐ์ด ๊ธฐํ ๊ธ์์ ์ผ๋ก ์ฆ๊ฐํ๊ฒ ๋๋ค.
- ๊ฒฐ๊ณผ : ์๊ฐ ์ด๊ณผ
2.2. ์ต์ ํ ๊ณผ์
- **์ต์ ํ ํ์์ฑ**: ํธ๋ฆฌ ํ์ ๊ด์ ์ผ๋ก ๋ดค์๋ ๋ชจ๋ ๋ ธ๋๋ฅผ ์ ์์กฐ์ฌํจ์ผ๋ก ๋งค์ฐ ์ค๋์๊ฐ์ด ๊ฑธ๋ฆฌ๊ฒ ๋๊ณ ๋นํจ์จ์ ์ด๋ค.
- **์ต์ ํ ์ ๋ต**: ์กฐ๊ฑด์ ์ถ๊ฐํด ๋ถํ์ํ ๋ ธ๋ ํ์ ๊ณผ์ ์ ๊ฑฐ
- **๋ณ๊ฒฝ**: ๋ ธ๋ ํ์ ์ค ํ์ฌ๊น์ง์ ์ต์ ์์ ํ์๋ณด๋ค ๋ ๊น์ ๋ ธ๋์ ํ์์ ํ์ ์์ ์ ์ฆ์ ์ข ๋ฃ
2.3. ์ต์ ํ๋ ์ฝ๋ (1์ฐจ)
- **์ฝ๋**:
```java
// DFS ์ต์ ํ (1์ฐจ)
import java.io.*; public class Main
{
static int minDepth = 1000000;
static void search(int n,int depth) {
if (n == 1) {
minDepth = Math.min(depth, minDepth);
return;
}
//์ถ๊ฐ๋์ด์ง ์กฐ๊ฑด
if (minDepth == depth) return;
for(int i=0; i<2; i+=1)
if(n%(3-i) == 0) search(n/(3-i), depth+1);
search(n-1,depth+1);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
search(n,0);
bw.write(Integer.toString(minDepth));
bw.flush();
bw.close();
}
}
``` - **์ฝ๋ ์ค๋ช **:
- ๋ ธ๋๋ฅผ ๋ชจ๋ ํ์ํ๋, ํ์ํ๋ ค๋ ๋ ธ๋์ ๊น์ด(Depth)๊ฐ ๋ง์ง๋ง์ผ๋ก ๊ตฌํ 1์ ๋ง๋ค๊ธฐ ์ํ ์ต์ ๊น์ด๋ฅผ ์ด๊ณผํ๋ ๊ฒฝ์ฐ ๊ณผ์ ์ ์ค๋จํ๋๋ก ํ์ฌ ์คํ ์๊ฐ์ ๋จ์ถ ์์ผฐ์.
2.4. ๊ฒฐ๊ณผ
- **๊ฒฐ๊ณผ ์ค๋ช **: ๊ธฐ์กด ์ฝ๋์ ๋นํด ํ์ํ๋ ๋ ธ๋์ ๊ฐฏ์๊ฐ ๋ํญ ๊ฐ์ํ์๋ค.
- **๋น๊ต ๋ถ์ (n=1234)**:
- ํ์ ๋ ธ๋ ๊ฐฏ์
- ์ด๊ธฐ ์ฝ๋ : ๊ฒฐ๊ณผ ๋์ถ ๋ถ๊ฐ (์๊ฐ ์ด๊ณผ)
- ์ต์ ํ(1์ฐจ) : 1970๊ฐ
- ์คํ ์๋(์ ์ถ ๊ฒฐ๊ณผ)
- ์ด๊ธฐ ์ฝ๋ : ๊ฒฐ๊ณผ ๋์ถ ๋ถ๊ฐ (์๊ฐ ์ด๊ณผ)
- ์ต์ ํ(1์ฐจ) : 140 ms
- **๋ฌธ์ ์ **:
- ์ด๊ธฐ ์ฝ๋์ ๋นํด ํ์ ๋ ธ๋ ๊ฐฏ์๋ ์ค์์ผ๋ ์ฌ์ ํ ํ์๊ฐ ์์ง ์๋ค.
- ๊ฒฐ๊ณผ : ์ ๋ต (14340KB, 140ms)
2.5. ์ต์ ํ ๊ณผ์
- **์ต์ ํ ํ์์ฑ**: ์ด๋ฏธ ์ต์ ๊ณ์ฐ ํ์๋ฅผ ๊ณ์ฐํ ์ด๋ ฅ์ด ์๋ ๋ ธ๋์ ๋ํด์๋ ์ค๋ณต์ผ๋ก ๊ณ์ฐ์ ํจ์ผ๋ก์จ ํ์ ๋ ธ๋ ๊ฐฏ์๊ฐ ๋์ด๋จ.
- **์ต์ ํ ์ ๋ต**: ๊ณ์ฐํ๋ ๊ฐ๋ค์ ๋ค์ ํ์ฉํ ์ ์๋๋ก ์๊ณ ๋ฆฌ์ฆ ์์ (DP)
- **๋ณ๊ฒฝ**: ๊ณ์ฐํ ์ต์ ๊ณ์ฐ ํ์๋ฅผ ๋ณ๋๋ก ๊ธฐ๋กํ๊ณ , ๋์ผํ ์๋ฅผ ๊ฐ์ง ๋ ธ๋์ ์ต์ ๊ณ์ฐ ํ์๋ฅผ ๊ตฌํ ๋ ๋ค์ ๊ณ์ฐํ์ง ์๊ณ ์ด๋ฏธ ๊ตฌํ ๊ฐ์ ํ์ฉํ๋๋ก ์์
2.6. ์ต์ ํ๋ ์ฝ๋(2์ฐจ)
- **์ฝ๋**:
```java
// DFS ์ต์ ํ (2์ฐจ)
import java.io.*; public class Main
{
static int minDepth = 1000000;
static int[] depthDict;
static int search(int n,int depth) {
if (n == 1) {
minDepth = Math.min(depth, minDepth);
return 1;
}
if(minDepth == depth) return 0;
if (depthDict[n-1] != 0) {
minDepth = Math.min(depth+depthDict[n-1], minDepth);
return depthDict[n-1]+1;
}
int partialMinDepth = 1000000;
for(int i=0; i<2; i+=1)
if(n%(3-i) == 0) {
int tmp = search(n/(3-i), depth+1);
if(tmp == 0) continue;
partialMinDepth = Math.min(tmp,partialMinDepth);
}
int tmp = search(n-1,depth+1);
if (tmp != 0) partialMinDepth = Math.min(tmp,partialMinDepth);
if (partialMinDepth != 1000000) depthDict[n-1] = partialMinDepth;
return partialMinDepth+1;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
depthDict = new int[n];
search(n,0);
bw.write(Integer.toString(minDepth));
bw.flush();
bw.close();
}
}
``` - **์ฝ๋ ์ค๋ช **:
- 1์ฐจ ์ฝ๋์ ์ถ๊ฐ๋ก DP ๊ฐ๋ ์ ์ ์ฉ. ๊ฐ ๋ ธ๋๋ก ๋ถํฐ 1๊น์ง์ ์ต์ ๊ณผ์ ํ์๋ฅผ ๋ณ๋๋ก ๋ฐฐ์ด์ ์ ์ฅํ๊ณ , ๋์ผํ ์ ๋ ธ๋ ๊ฒ์ฌ์ ์ ์ฅํ ์ต์ ํ์๊ฐ์ ๋ถ๋ฌ์ ์ค๋ณต๊ณ์ฐ์ ๋ฐฉ์งํ์๋ค.
2.7. ๊ฒฐ๊ณผ
- **๊ฒฐ๊ณผ ์ค๋ช **: ์ต์ ํ ์ฝ๋(1์ฐจ)์ ๋นํด ํ์ํ๋ ๋ ธ๋์ ๊ฐฏ์๊ฐ ๋ํญ ๊ฐ์ํ์๋ค.
- **๋น๊ต ๋ถ์ (n=1234)**:
- ํ์ ๋ ธ๋ ๊ฐฏ์
- ์ต์ ํ(1์ฐจ) : 1970๊ฐ
- ์ต์ ํ(2์ฐจ) : 11๊ฐ
- ์คํ ์๋(์ ์ถ ๊ฒฐ๊ณผ)
- ์ต์ ํ(1์ฐจ) : 140 ms
- ์ต์ ํ(2์ฐจ) : 104 ms
- **๊ฒฐ๊ณผ**: ์ ๋ต (18132KB, 104ms)
3. ๊ฒฐ๋ก
- **์์ฝ**: ํธ๋ฆฌ ํ์์ ์ด์ฉํ ๋ ๋ ธ๋์ ๊ฐฏ์๊ฐ ๋ง์๊ฒฝ์ฐ ์คํ ์๊ฐ์ด ๋งค์ฐ ๊ธธ์ด์ง์ ์๋ค. ๋ณ๋์ ์กฐ๊ฑด์ ๋ถ์ฌํ๊ฑฐ๋ DP๊ฐ์ ์ถ๊ฐ์ ์ธ ์๊ณ ๋ฆฌ์ฆ์ ๊ฐ์ด ํ์ฉํ์ฌ ๋ ธ๋ ํ์ํ์๋ฅผ ์ค์ด๋ฉด์ ์คํ ์๊ฐ์ ๋จ์ถ ์ํฌ ์ ์๋ค.
4. ์ฐธ๊ณ ์๋ฃ
- DFS ๊ฐ๋ : \<๊ฒ์๊ธ ์ค๋น ์ค\>
- DP ๊ฐ๋ : \<๊ฒ์๊ธ ์ค๋น ์ค\>