Tags
- Python
- PGM
- dfs
- ์๋ฎฌ๋ ์ด์
- ๋ฌธ์์ด
- ๋๋น ์ฐ์ ํ์
- ๊ทธ๋ํ ์ด๋ก
- BFS
- Brute Force Algorithm
- ๋ฐฑํธ๋ํน
- ๊ทธ๋ํ ํ์
- queue
- LV2
- BOJ
- ๊น์ด ์ฐ์ ํ์
- ์ํ
- ๊ต์ฌ
- greedy
- ์๋ฃ๊ตฌ์กฐ
- Study
- stack
- ๊ตฌํ
- SpringBoot
- CodingTest
- Dynamic Programming
- ์ ๋ ฌ
- sort
- ์ ์๋ก
- DP
- Java
Archives
๊ธฐ๋ก๋ฐฉ
BOJ_12100 : 2048 (Easy) ๋ณธ๋ฌธ
๐ธ ๋ฌธ์ ๋ถ์ ๐ธ
- N x N ๊ฒ์ ํ์์ 2048์ ์ํํ๋ค.
- 5๋ฒ ์ํํ์ ๋ ์ต๋๊ฐ์ ์ถ๋ ฅํ๋ค.
๐ธ ๋ฌธ์ ํ์ด ๐ธ
- 2048๊ฒ์ ๊ท์น์ ๋ฐ๋ผ ๊ตฌํํ๋ฉด ๋๋ ๋ฌธ์ ์ด๋ค.
- ์ด๋ ๋ฐฉํฅ์ ๋ฐ๋ผ ๋ชจ๋ ์นธ์ ์ซ์๊ฐ ์ด๋ํ๋ค.
- ๊ฐ์ ์ซ์๋ ํฉ์ณ์ง๊ณ , ๊ฐ์ ํ์ฐจ์์ ๋์ด์ ํฉ์ณ์ง์ง ์๋๋ค.
- ๋จผ์ ์์ง์ธ ์ซ์๊ฐ ๋จผ์ ํฉ์ณ์ง๋ค.
- ์ต๋๊ฐ์ ์ฐพ๊ธฐ์ํด 5ํ ๊ฒ์ ํ์์ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ํ์ธํ๋ค.
- ํ์ด๋ DFS๋ก ํ์ํ์๋ค.
๐ธ ์ฝ๋ ๐ธ
import java.io.*;
import java.util.StringTokenizer;
public class Main {
private static final int MAX = 5; // ์ต๋ ์ด๋ ์
private static int N, answer;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
N = Integer.parseInt(br.readLine());
int[][] map = new int[N][N];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
answer = 0;
play_2048(0, map);
bw.write(Integer.toString(answer));
bw.flush();
}
private static void play_2048(int cnt, int[][] map) {
// print(map);
if (cnt == MAX) {
get_maxVal(map);
return;
}
// System.out.println("up : " + (cnt + 1));
play_2048(cnt + 1, move_up(copyMap(map), new boolean[N][N]));
// System.out.println("down : " + (cnt + 1));
play_2048(cnt + 1, move_down(copyMap(map), new boolean[N][N]));
// System.out.println("left : " + (cnt + 1));
play_2048(cnt + 1, move_left(copyMap(map), new boolean[N][N]));
// System.out.println("right : " + (cnt + 1));
play_2048(cnt + 1, move_right(copyMap(map), new boolean[N][N]));
}
private static int[][] move_up(int[][] map, boolean[][] flag) {
for (int col = 0; col < N; col++) {
for (int row = 1; row < N; row++) {
int next = row;
while (next - 1 >= 0 && !flag[next - 1][col]) {
if (map[next - 1][col] == 0)
next--;
else if (map[next - 1][col] == map[row][col]) {
next--;
break;
} else
break;
}
if (next != row && !flag[next][col]) {
if (map[next][col] == 0) { // ๋จ์ ์ด๋
map[next][col] = map[row][col];
map[row][col] = 0;
} else if (map[next][col] == map[row][col]) { // ํฉ์น๊ธฐ
map[next][col] *= 2;
map[row][col] = 0;
flag[next][col] = true;
}
}
}
}
return map;
}
private static int[][] move_down(int[][] map, boolean[][] flag) {
for (int col = 0; col < N; col++) {
for (int row = N - 2; row >= 0; row--) {
int next = row;
while (next + 1 < N && !flag[next + 1][col]) {
if (map[next + 1][col] == 0)
next++;
else if (map[next + 1][col] == map[row][col]) {
next++;
break;
} else
break;
}
if (next != row && !flag[next][col]) {
if (map[next][col] == 0) { // ๋จ์ ์ด๋
map[next][col] = map[row][col];
map[row][col] = 0;
} else if (map[next][col] == map[row][col]) { // ํฉ์น๊ธฐ
map[next][col] *= 2;
map[row][col] = 0;
flag[next][col] = true;
}
}
}
}
return map;
}
private static int[][] move_left(int[][] map, boolean[][] flag) {
for (int row = 0; row < N; row++) {
for (int col = 1; col < N; col++) {
int next = col;
while (next - 1 >= 0 && !flag[row][next - 1]) {
if (map[row][next - 1] == 0)
next--;
else if (map[row][next - 1] == map[row][col]) {
next--;
break;
} else
break;
}
if (next != col && !flag[row][next]) {
if (map[row][next] == 0) { // ๋จ์ ์ด๋
map[row][next] = map[row][col];
map[row][col] = 0;
} else if (map[row][next] == map[row][col]) { // ํฉ์น๊ธฐ
map[row][next] *= 2;
map[row][col] = 0;
flag[row][next] = true;
}
}
}
}
return map;
}
private static int[][] move_right(int[][] map, boolean[][] flag) {
for (int row = 0; row < N; row++) {
for (int col = N - 2; col >= 0; col--) {
int next = col;
while (next + 1 < N && !flag[row][next + 1]) {
if (map[row][next + 1] == 0)
next++;
else if (map[row][next + 1] == map[row][col]) {
next++;
break;
} else
break;
}
if (next != col && !flag[row][next]) {
if (map[row][next] == 0) { // ๋จ์ ์ด๋
map[row][next] = map[row][col];
map[row][col] = 0;
} else if (map[row][next] == map[row][col]) { // ํฉ์น๊ธฐ
map[row][next] *= 2;
map[row][col] = 0;
flag[row][next] = true;
}
}
}
}
return map;
}
private static int[][] copyMap(int[][] map) {
int[][] copy = new int[N][N];
for (int i = 0; i < N; i++) {
System.arraycopy(map[i], 0, copy[i], 0, N);
}
return copy;
}
private static void get_maxVal(int[][] map) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
answer = Math.max(answer, map[i][j]);
}
}
}
private static void print(int[][] map) {
StringBuilder sb = new StringBuilder();
sb.append('\n');
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sb.append(map[i][j]).append('\t');
}
sb.append('\n');
}
sb.append('\n');
System.out.println(sb);
}
}
๐ธ ์ฝ๋ ํด์ ๐ธ
- ๊ฒ์ํ ์ ๋ณด๋ฅผ 2์ฐจ์ intํ ๋ฐฐ์ด map์ ๋ฐ์, dfs๋ฅผ ์ ์ฉํ ์ฌ๊ท ๋ฉ์๋ play_2048์ ๋ฃ๊ณ ์ต๋๊ฐ์ ๊ตฌํ๋ค.
- ๊น์ด ์ ๋ณด๊ฐ 5๊น์ง ์ค๋ฉด ๋ชจ๋ ์ธ๋ฑ์ค๋ฅผ ํ์ธํด ์ต๋๊ฐ์ ๊ฐฑ์ ํ๋ค.
- ์์ง ๊น์ด๊ฐ 5๊น์ง ์ค์ง ์์์ผ๋ฉด, ์ํ์ข์ฐ ์์ผ๋ก ์ด๋ํ๋ค.
- ์ํ์ข์ฐ ์ด๋์ ๊ฐ๋ตํ ๋ค์๊ณผ ๊ฐ์ ์ ์ฐจ๋ก ๊ตฌํํ๋ค.
- ์ด๋ํ๊ณ ์ ํ๋ ๋ฐฉํฅ์์ ๊ฐ๊น์ด ์ธ๋ฑ์ค๋ถํฐ ์ด๋์์ผ ๋ณธ๋ค.
- ๊ฐ ์ ์๋ ์ธ๋ฑ์ค ๋๊น์ง ์ด๋ํ๋ค.
- ์ด๋ X / ๋จ์ ์ด๋ / ํฉ์น๊ธฐ ์ค์์ ๋์ํ๋ค.
๐ธ end ๐ธ
- ์ฒ์์ 14%์์ ํ๋ ธ๋๋ฐ, ์ค๋ฅธ์ชฝ ์ด๋์ ์กฐ๊ฑด์ด ํ๋ ธ๋ค.
- ๋ ๋ฒ์งธ๋ 2%์์ ํ๋ ธ๋๋ฐ ๋ค์๋ณด๋ ์๋ ์ด๋์ ์กฐ๊ฑด๋ ๊ฐ์ ๋ถ๋ถ์์ ํ๋ ธ๋ค.
- ์ํ์ข์ฐ ๋น์ทํ 4๊ฐ์ง ๋ฒ์ ์ ์ฝ๋๋ฅผ ํจ๊ป ๋ณ๊ฒฝํ๋ฉด์ ๋ ์ข ๋ฅ์ ๋ณ๊ฒฝ์ด ๋ฏธํกํ๋ค.
- ํ ๋ฒ ํ๋ ธ์ผ๋ฉด ๋ค๋ฅธ ์ข ๋ฅ๋ ํ์ธํ์ด์ผ ํ๋๋ฐ, ํ์ด ์๊ฐ์ด ๊ธธ์ด์ง๋ค๋ณด๋ ์ง์ค๋ ฅ์ด ํ๋ ค์ง ๊ฒ ๊ฐ๋ค.
728x90
'CodingTest > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ_12015 : ๊ฐ์ฅ ๊ธด ์ฆ๊ฐํ๋ ๋ถ๋ถ ์์ด 2 (0) | 2024.04.09 |
---|---|
BOJ_1005 : ACM Craft (0) | 2024.04.04 |
BOJ_20040 : ์ฌ์ดํด ๊ฒ์ (0) | 2024.03.29 |
BOJ_18352 : ํน์ ๊ฑฐ๋ฆฌ์ ๋์ ์ฐพ๊ธฐ (0) | 2024.03.27 |
BOJ_1647 : ๋์ ๋ถํ ๊ณํ (0) | 2024.03.14 |