2024-TEAM-05 / algorithm-for-kakao

์นด์นด์˜ค ๊ธฐ์ถœ ๋ฌธ์ œ ๊ฐ€์ฆˆ์•„๐Ÿฃ
0 stars 0 forks source link

[๋ฐฑ์ค€] 2048 (Easy) #36

Open hye-on opened 1 month ago

hye-on commented 1 month ago

๐Ÿ”— 2048 (Easy)

hye-on commented 1 month ago

๐Ÿ“‘ ๋Œ“๊ธ€ ํ…œํ”Œ๋ฆฟ

์ฝ”๋“œ ํ’€์ด

```cpp #include #include #include using namespace std; //1:17~3:21 ,4:43 int n; long long move(vector>& map, int dir) { //ํ•ฉ์ณ์กŒ๋Š”์ง€ ์—ฌ๋ถ€ vector> chk(n, vector(n, false)); if (dir == 0) { //์•„๋ž˜ for (int j = 0; j < n; j++) { //x์ถ• for (int i = n - 1; i >= 0; i--) { //์›€์ง์ผ ํ•„์š” ์—†๊ฑฐ๋‚˜ ๋ฒฝ์ชฝ์ด๋ฉด ์Šคํ‚ต if (map[i][j] == 0 || (i + 1) >= n) continue; int toY = i + 1; while (toY < n) { if (map[toY][j] == 0) { //๋นˆ์นธ์ด๋ฉด map[toY][j] = map[toY - 1][j]; map[toY - 1][j] = 0; } else { if (!chk[toY][j] && !chk[toY - 1][j] && map[toY][j] == map[toY - 1][j]) { //๊ฐ™์€ ์ˆซ์ž์ด๋ฉด ํ•ฉ์น˜๊ธฐ map[toY][j] *= 2; map[toY - 1][j] = 0; chk[toY][j] = true; } //๋‹ค๋ฅธ ์ˆซ์ž๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆซ์ž์ธ๋ฐ ์ด๋ฏธ ํ•ฉ์นœ ์ ์ด ์žˆ์œผ๋ฉด ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š์Œ } toY++; } } } // cout<<"0 ====================="<= 0; j--) { //์›€์ง์ผ ํ•„์š” ์—†๊ฑฐ๋‚˜ ๋ฒฝ์ชฝ์ด๋ฉด ์Šคํ‚ต if (map[i][j] == 0 || (j + 1) >= n) continue; int toX = j + 1; while (toX < n) { if (map[i][toX] == 0) { //๋นˆ์นธ์ด๋ฉด map[i][toX] = map[i][toX - 1]; map[i][toX - 1] = 0; } else { if (!chk[i][toX] && !chk[i][toX - 1] && map[i][toX] == map[i][toX - 1]) { //๊ฐ™์€ ์ˆซ์ž์ด๋ฉด ํ•ฉ์น˜๊ธฐ map[i][toX] *= 2; map[i][toX - 1] = 0; chk[i][toX] = true; } //๋‹ค๋ฅธ ์ˆซ์ž๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆซ์ž์ธ๋ฐ ์ด๋ฏธ ํ•ฉ์นœ ์ ์ด ์žˆ์œผ๋ฉด ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š์Œ } toX++; } } } /* cout<<"1=============================="<= 0) { if (map[toY][j] == 0) { //๋นˆ์นธ์ด๋ฉด map[toY][j] = map[toY + 1][j]; map[toY + 1][j] = 0; } else { if (!chk[toY][j] && !chk[toY + 1][j] && map[toY][j] == map[toY + 1][j]) { //๊ฐ™์€ ์ˆซ์ž์ด๋ฉด ํ•ฉ์น˜๊ธฐ map[toY][j] *= 2; map[toY + 1][j] = 0; chk[toY][j] = true; } //๋‹ค๋ฅธ ์ˆซ์ž๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆซ์ž์ธ๋ฐ ์ด๋ฏธ ํ•ฉ์นœ ์ ์ด ์žˆ์œผ๋ฉด ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š์Œ } toY--; } } } // cout<<"2======================="<= 0) { if (map[i][toX] == 0) { //๋นˆ์นธ์ด๋ฉด map[i][toX] = map[i][toX + 1]; map[i][toX + 1] = 0; } else { if (!chk[i][toX] && !chk[i][toX + 1] && map[i][toX] == map[i][toX + 1]) { //๊ฐ™์€ ์ˆซ์ž์ด๋ฉด ํ•ฉ์น˜๊ธฐ map[i][toX] *= 2; map[i][toX + 1] = 0; chk[i][toX] = true; } //๋‹ค๋ฅธ ์ˆซ์ž๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆซ์ž์ธ๋ฐ ์ด๋ฏธ ํ•ฉ์นœ ์ ์ด ์žˆ์œผ๋ฉด ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š์Œ } toX--; } } } // cout<<"3=============================="<> map, int depth) { if (depth == 5) return; //map์—์„œ ๋ชจ๋“  ๊ฒƒ์„ dir ๋ฐฉํ–ฅ์œผ๋กœ ์›€์ง์ด๊ธฐ for (int i = 0; i < 4; i++) { vector> mapT = map; // cout<<"depth "<> n; vector>board(n, vector(n, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> board[i][j]; } } BruteForce(board, 0); cout << res << endl; return 0; } ```

์ฝ”๋ฉ˜ํŠธ

- ์ถœ๋ ฅ๊ฐ’ ํ™•์ธํ•˜๋ ค๊ณ  ๋„ฃ์—ˆ๋˜ ์ฝ”๋“œ ํ•œ์ค„ ์•ˆ์ง€์›Œ์„œ ๊ณ„์† ํ‹€๋ ธ์—ˆ๋„ค์š”... ๊ฒŒ์‹œํŒ ๋ฐ˜๋ก€ ๋‹ค ๋„ฃ์–ด๋ณด๊ณ  ์™œ์•ˆ๋ผ.. ์ด๋Ÿฌ๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ๐Ÿ˜ข
- ์ข€ ๊นŒ๋‹ค๋กœ์šด ๊ตฌํ˜„๋ฌธ์ œ์˜€๋˜๊ฒƒ ๊ฐ™์•„์š”. ํ•œ๋ฐฉํ–ฅ ๋ฏธ๋ฃจ๊ธฐ๋งŒ ๊ตฌํ˜„ํ•˜๊ณ  ํšŒ์ „์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค๋Š”๋ฐ ๊ทธ๋Ÿฌ๋ฉด ์‹ค์ˆ˜๋ฅผ ๋œํ•  ๊ฒƒ ๊ฐ™์•„์„œ ์ข‹์€๊ฒƒ ๊ฐ™์•„์š”
uijin-j commented 1 month ago

๐Ÿ“‘ ๋Œ“๊ธ€ ํ…œํ”Œ๋ฆฟ

์ฝ”๋“œ ํ’€์ด

```java import java.io.*; import java.util.*; /** * 11:57 START! 12:33 STOP! (35๋ถ„) * 16:17 RE! 16:41 END! (25๋ถ„) */ public class Main { /** * ์™„์ „ํƒ์ƒ‰ DFS O(4^5*N^2) ~= O(1024N^2) ~= O(N^2) ~= 400 */ static int N; static int answer = 0; public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); N = Integer.parseInt(bf.readLine()); int[][] board = new int[N][N]; StringTokenizer st; for(int i = 0; i < N; ++i) { st = new StringTokenizer(bf.readLine()); for(int j = 0; j < N; ++j) { board[i][j] = Integer.parseInt(st.nextToken()); } } dfs(0, board); System.out.println(answer); } public static void dfs(int level, int[][] board) { if(level == 5) { int max = 0; for(int i = 0; i < N; ++i) { for(int j = 0; j < N; ++j) { max = Math.max(max, board[i][j]); } } answer = Math.max(answer, max); return; } // ์ƒ dfs(level + 1, up(board)); // ํ•˜ dfs(level + 1, down(board)); // ์ขŒ dfs(level + 1, left(board)); // ์šฐ dfs(level + 1, right(board)); } public static int[][] up(int[][] board) { int[][] result = new int[N][N]; boolean[][] merged = new boolean[N][N]; for(int i = 0; i < N; ++i) { // i๋Š” col int idx = 0; for(int j = 0; j < N; ++j) { // j๋Š” row if(board[j][i] != 0) { if(idx != 0 && !merged[idx - 1][i] && board[j][i] == result[idx - 1][i]) { result[idx - 1][i] += board[j][i]; merged[idx - 1][i] = true; } else { result[idx++][i] = board[j][i]; } } } } return result; } public static int[][] down(int[][] board) { int[][] result = new int[N][N]; boolean[][] merged = new boolean[N][N]; for(int i = 0; i < N; ++i) { // i๋Š” col int idx = N - 1; for(int j = N - 1; j >= 0; --j) { // j๋Š” row if(board[j][i] != 0) { if(idx != N - 1 && !merged[idx + 1][i] && board[j][i] == result[idx + 1][i]) { result[idx + 1][i] += board[j][i]; merged[idx + 1][i] = true; } else { result[idx--][i] = board[j][i]; } } } } return result; } public static int[][] left(int[][] board) { int[][] result = new int[N][N]; boolean[][] merged = new boolean[N][N]; for(int i = 0; i < N; ++i) { // i๋Š” row int idx = 0; for(int j = 0; j < N; ++j) { // j๋Š” col if(board[i][j] != 0) { if(idx != 0 && !merged[i][idx - 1] && board[i][j] == result[i][idx - 1]) { result[i][idx - 1] += board[i][j]; merged[i][idx - 1] = true; } else { result[i][idx++] = board[i][j]; } } } } return result; } public static int[][] right(int[][] board) { int[][] result = new int[N][N]; boolean[][] merged = new boolean[N][N]; for(int i = 0; i < N; ++i) { // i๋Š” row int idx = N - 1; for(int j = N - 1; j >= 0; --j) { // j๋Š” col if(board[i][j] != 0) { if(idx != N - 1 && !merged[i][idx + 1] && board[i][j] == result[i][idx + 1]) { result[i][idx + 1] += board[i][j]; merged[i][idx + 1] = true; } else { result[i][idx--] = board[i][j]; } } } } return result; } } ```

์ฝ”๋ฉ˜ํŠธ

- DFS๋กœ ์™„์ „ํƒ์ƒ‰์„ ํ•ด๋„ ์‹œ๊ฐ„์ œํ•œ์ด ๊ฑธ๋ฆฌ์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™์•„์„œ, ์™„ํƒ์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!
- ์ƒ/ํ•˜/์ขŒ/์šฐ๋กœ ๋ฐ€์–ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ๋”ฐ๋กœ ์žˆ๋Š”๋ฐ, ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”!๐Ÿง