์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ์๋ฎฌ๋ ์ด์
- ์๋ฃ๊ตฌ์กฐ
- ์์ด
- BFS
- ๋นํธ๋ง์คํน
- ์ฐ์ ์์ํ
- ํฌํฌ์ธํฐ
- ์ฌ๊ท
- dfs
- ์กฐํฉ
- ์ด์งํธ๋ฆฌ
- LowerBound
- ์นด์นด์ค
- Dijkstra
- ๋ฐฑํธ๋ํน
- ๊ตฌํ
- Union Find
- ์ธ๊ทธ๋จผํธํธ๋ฆฌ
- ํธ๋ผ์ด
- Django
- Floyd
- upperbound
- ํ๋ก์ด๋์์
- PriorityQueue
- ๋ถ๋ถ์งํฉ
- dp
- ๊ทธ๋ฆฌ๋
- ๋ค์ต์คํธ๋ผ
- ํ์๊ฐ์
- ์ด๋ถํ์
- Today
- Total
J
[JAVA] BOJ ๋ฐฑ์ค 17825 ์ฃผ์ฌ์ ์ท๋์ด ๋ณธ๋ฌธ
[JAVA] BOJ ๋ฐฑ์ค 17825 ์ฃผ์ฌ์ ์ท๋์ด
snowball๐ฐ 2023. 3. 6. 17:50https://www.acmicpc.net/problem/17825
17825๋ฒ: ์ฃผ์ฌ์ ์ท๋์ด
์ฒซ์งธ ์ค์ ์ฃผ์ฌ์์์ ๋์ฌ ์ 10๊ฐ๊ฐ ์์๋๋ก ์ฃผ์ด์ง๋ค.
www.acmicpc.net
ํ์ด ๋ฐฉ๋ฒ
- ๋ฏธ๋ฆฌ ์ฃผ์ฌ์๊ฐ ๋์ฐฉํ๋ ์นธ์ ์ ์๋ค์ ์ ์ฅํด์ค๋ค. (์ด๋ ํ๋ ์ด์์ด ๋ฟ์ ์ ์๋ ๊ณณ์ด๋ฉด ๋ฐ๋ก ๋ฐฐ์ด์ ๋ง๋ค์ด์ฃผ๋ ๊ฒ์ด ์ข๋ค.)
- ์ค๋ณต ์์ด์ ์ด์ฉํ์ฌ ์ด๋ํ ๋ง์ ์ ํ๊ณ ์ด๋ ๊ตฌ์ญ์ ๋ง์ด ์๋ ์ง ์กฐ์ฌํ ๋ค ์ด๋ํ๋ค.
์ ์ฒด ์ฝ๋
import java.util.*;
import java.io.*;
public class Main {
static int[] dice;
static int[][] hores;
static int result;
static int[][] map = new int[5][];
static int[] dx = {-1, 0, 0, 1};
static int[] dy = {0, -1, 1, 0};
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
dice = new int[10];
hores = new int[4][2];
map[0] = new int[] {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40};
map[1] = new int[] {0, 13, 16, 19};
map[2] = new int[] {0, 22, 24};
map[3] = new int[] {0, 28, 27, 26};
map[4] = new int[] {0, 25, 30, 35};
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
for(int i=0; i<10; i++)
dice[i] = Integer.parseInt(st.nextToken());
move(0, 0);
System.out.println(result);
}
static void move(int cnt, int sum) {
if(cnt == 10) {
if(result < sum) result = sum;
return;
}
c: for(int i=0; i<4; i++) { // ์์น ์ธ๋ฑ์ค, ๋งต ์ ํ ์ธ๋ฑ์ค
if(hores[i][0] == 'e') continue;
int x = hores[i][0]; int m = hores[i][1];
if(hores[i][1] == 0 && hores[i][0] % 5 == 0 && hores[i][0] < 20) {
hores[i][1] = hores[i][0] / 5; hores[i][0] = 0;
}
hores[i][0] += dice[cnt];
if(hores[i][1] == 0 && hores[i][0] >= map[0].length)
hores[i][0] = 'e';
if(hores[i][1] > 0 && hores[i][1] < 4 && hores[i][0] >= map[hores[i][1]].length) {
hores[i][0] = hores[i][0] - map[hores[i][1]].length + 1;
hores[i][1] = 4;
}
if(hores[i][1] == 4 && hores[i][0] >= map[hores[i][1]].length) {
hores[i][0] = hores[i][0] - map[hores[i][1]].length;
hores[i][1] = 0;
if(hores[i][0] > 0) hores[i][0] = 'e';
else hores[i][0] = 20;
}
if(hores[i][0] != 'e') {
for(int k=0; k<4; k++)
if(k == i) continue;
else if(hores[k][0] == hores[i][0] && hores[k][1] == hores[i][1]) {
hores[i][0] = x; hores[i][1] = m;
continue c;
}
}
if(hores[i][0] == 'e')
move(cnt+1, sum);
else
move(cnt+1, sum+map[hores[i][1]][hores[i][0]]);
hores[i][0] = x; hores[i][1] = m;
}
}
}
ํ์ค์ฉ ์ดํด๋ณด์
static int[] dice;
static int[][] hores;
static int result;
static int[][] map = new int[5][];
dice์๋ ์ฃผ์ฌ์ ์ ๋ณด 10๊ฐ๋ฅผ ๋ด์์ค ๊ฒ์ด๋ค.
hores๋ ๋ง์ ์ ๋ณด์ด๋ค. index 0์๋ ๋ง์ ์์น, index 1์๋ ๋ง์ด ์ด๋ค map์ ์๋์ง๋ฅผ ์ ์ฅํด์ค๋ค.
map์ ๋ฏธ๋ฆฌ ๋ณด๋์ ์ ์๋ฅผ ์ ์ฅํด์ค๋ค.
dice = new int[10];
hores = new int[4][2];
map[0] = new int[] {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40};
map[1] = new int[] {0, 13, 16, 19};
map[2] = new int[] {0, 22, 24};
map[3] = new int[] {0, 28, 27, 26};
map[4] = new int[] {0, 25, 30, 35};
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
for(int i=0; i<10; i++)
dice[i] = Integer.parseInt(st.nextToken());
๋๋ ๋ค์๊ณผ ๊ฐ์ด map์ ์ ๋ณด๋ฅผ ์ ์ฅํ์๋ค.
static void move(int cnt, int sum) {
if(cnt == 10) {
if(result < sum) result = sum;
return;
}
move()๋ฅผ ํตํด ์ค๋ณต ์์ด์ ๊ตฌํํ ๊ฒ์ด๋ค. ๋ฐ๋ผ์ visited๋ ํ์์๋ค. ์ด๋ ํ๋ผ๋ฏธํฐ cnt๋ ๋ช๋ฒ์งธ ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฐ ๊ฒ์ธ์ง, sum์ ์ฌํ๊น์ง ์ ์์ ํฉ์ ์ ์ฅํ๊ณ ์๋ค.
๊ธฐ์ ์กฐ๊ฑด์ ์ํด 10๋ฒ์ ๋ค ๊ตด๋ ธ์ผ๋ฉด result์ sum์ ๊ฐฑ์ ํด์ค๋ค. ๊ทธ๋ฆฌ๊ณ return
c: for(int i=0; i<4; i++) { // ์์น ์ธ๋ฑ์ค, ๋งต ์ ํ ์ธ๋ฑ์ค
if(hores[i][0] == 'e') continue;
int x = hores[i][0]; int m = hores[i][1];
if(hores[i][1] == 0 && hores[i][0] % 5 == 0 && hores[i][0] < 20) {
hores[i][1] = hores[i][0] / 5; hores[i][0] = 0;
}
for๋ฌธ์ผ๋ก ์ด๋ค ๋ง์ ์ด๋ฒ ์ฃผ์ฌ์๋ฅผ ์ ์ฉํ ์ง ๊ณ ๋ฅธ๋ค. ์ด๋ฏธ ๋ง์ด ๋์ ๋ฟ์๋ค๋ฉด ๋ ์ด์ ์ด๋ํ ์ ์์ผ๋ฏ๋ก continue
x์ m์ ํ์ฌ ๋ง์ ์ ๋ณด๋ค์ ์ ์ฅํด๋๋๋ค.
์ฒซ๋ฒ์งธ map์์ 10, 20, 30์ index๋ ๊ฐ๊ฐ 5, 10, 15๋ก 5์ ๋ฐฐ์์ด๋ค. ์ด ์์น์์ ์ถ๋ฐํ๋ ๊ฒฝ์ฐ map์ ์ ๋ณด๊ฐ ๋ฐ๋๊ฒ ๋๋ค. ์ด๋ map์ index๋ฅผ 5๋ก ๋๋ ๋ชซ๊ณผ ๋์ผํ๋ฏ๋ก ๋ค์๊ณผ ๊ฐ์ด ๋ง์ ์ ๋ณด๋ฅผ ๋ฐ๊พผ๋ค.(20์ดํ์ธ ์ด์ ๋ 40์ map์ด ๋ณํ์ง ์๊ธฐ ๋๋ฌธ)
hores[i][0] += dice[cnt];
if(hores[i][1] == 0 && hores[i][0] >= map[0].length)
hores[i][0] = 'e';
if(hores[i][1] > 0 && hores[i][1] < 4 && hores[i][0] >= map[hores[i][1]].length) {
hores[i][0] = hores[i][0] - map[hores[i][1]].length + 1;
hores[i][1] = 4;
}
if(hores[i][1] == 4 && hores[i][0] >= map[hores[i][1]].length) {
hores[i][0] = hores[i][0] - map[hores[i][1]].length;
hores[i][1] = 0;
if(hores[i][0] > 0) hores[i][0] = 'e';
else hores[i][0] = 20;
}
์ด๋ํ map์ ๊ฒฐ์ ํ์ผ๋ฏ๋ก ์ด์ ์ด๋์ ํด์ค ๊ฒ์ด๋ค.
๋ง์ index์ dice๋ฅผ ๋ํด์ ์ด๋ํด์ฃผ๊ณ ์ฒซ๋ฒ์งธ if๋ฌธ์ผ๋ก ๋ง์ ์ข ๋ฃ ์ง์ ์ ๊ฐ๋ ๊ฒฝ์ฐ๋ฅผ ์ฐพ๋๋ค.
๋๋ฒ์งธ if๋ฌธ์์ map 1, 2, 3์ธ ๊ฒฝ์ฐ์ด๊ณ index๊ฐ ์ต๋ ๊ธธ์ด๋ฅผ ๋์ด๊ฐ๋ ๊ฒฝ์ฐ map 4๋ก ์ด๋ํด์ค๋ค. ์ด๋ ์ด๋ํ ๊ธธ์ด๋งํผ ์ค์ฌ์ฃผ๋ ๊ฒ์ ์์ผ๋ฉด ์๋๋ค.
๋ง์ง๋ง if๋ฌธ์์ ๋์ฐฉํ๋ ๊ฒฝ์ฐ๋ฅผ ์ฒ๋ฆฌํด์ค๋ค.
์ด๋ ๋ง์ง๋ง if๋ฌธ์ else if๋ก ์ฒ๋ฆฌํ๋ฉด ์๋๋ค! ๋๋ฒ์งธ์์ ๋ณ๊ฒฝ๋ ๊ฐ์ด ์ธ๋ฒ์งธ if๋ฅผ ๋ง์กฑํ๋ ๊ฒฝ์ฐ๊ฐ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ด๋ค.
if(hores[i][0] != 'e') {
for(int k=0; k<4; k++)
if(k == i) continue;
else if(hores[k][0] == hores[i][0] && hores[k][1] == hores[i][1]) {
hores[i][0] = x; hores[i][1] = m;
continue c;
}
}
๋ง์ง๋ง์ผ๋ก ๋ง์ ์ ๋ณด๋ค์ ํ์ธํ๋ฉฐ ํ์ฌ ์์น์ ๋ค๋ฅธ ๋ง์ด ์กด์ฌํ๋์ง ํ์ธํด์ฃผ๊ณ ์์ผ๋ฉด ์ฒ์์ ์ ์ฅํ ๊ฐ์ผ๋ก ๋ค์ ๋๋๋ ค์ค๋ค continue๋ฅผ ํด์ค๋ค.
if(hores[i][0] == 'e')
move(cnt+1, sum);
else
move(cnt+1, sum+map[hores[i][1]][hores[i][0]]);
hores[i][0] = x; hores[i][1] = m;
}
}
}
๋ง์ด ๋ง์ง๋ง ๋์ฐฉ์ ์ ๋ฟ๋๋ค๋ฉด ์ ์๋ฅผ ์ป์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ sum์ ์๋ฌด๊ฐ๋ ๋ํ์ง ์๋๋ค.
๊ทธ๊ฒ ์๋๋ผ๋ฉด ํ์ฌ ๋จธ๋ฌผ๊ณ ์๋ ์ฃผ์ฌ์ ์ ์๋ฅผ ๋ํ์ฌ ์ฌ๊ท๋ฅผ ํธ์ถํ๋ค.
'๐ Problem Solving > ๐ป Samsung SW Expert' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] BOJ ๋ฐฑ์ค 19236 ์ฒญ์๋ ์์ด (0) | 2023.03.30 |
---|---|
[JAVA] BOJ ๋ฐฑ์ค 17070 ํ์ดํ์ฎ๊ธฐ๊ธฐ1 (0) | 2023.03.29 |
[JAVA] BOJ ๋ฐฑ์ค 19237 ์ด๋ฅธ ์์ด (0) | 2023.03.06 |
[JAVA] BOJ ๋ฐฑ์ค 20061 ๋ชจ๋ ธ๋ฏธ๋ ธ๋๋ฏธ๋ ธ 2 (1) | 2023.03.05 |
[JAVA] BOJ ๋ฐฑ์ค 15685 ๋๋๊ณค ์ปค๋ธ (0) | 2023.02.25 |