issues
search
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
๐ ๋๊ธ ํ ํ๋ฆฟ
Language : C++
์ฑ๋ฅ
์ฝ๋ ํ์ด
```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
๐ ๋๊ธ ํ ํ๋ฆฟ
Language : Java
์ฑ๋ฅ
์ฝ๋ ํ์ด
```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๋ก ์์ ํ์์ ํด๋ ์๊ฐ์ ํ์ด ๊ฑธ๋ฆฌ์ง ์์ ๊ฒ ๊ฐ์์, ์ํ์ผ๋ก ํ์์ต๋๋ค! - ์/ํ/์ข/์ฐ๋ก ๋ฐ์ด์ฃผ๋ ๋ฉ์๋๊ฐ ๋ฐ๋ก ์๋๋ฐ, ํ๋๋ก ํฉ์น๋ ๋ฐฉ๋ฒ๋ ์์ ๊ฒ ๊ฐ๋ค์!๐ง
๐ 2048 (Easy)