From f973302b983dcc33555e5ee3ca5e6c29564c8a97 Mon Sep 17 00:00:00 2001 From: oncsr Date: Mon, 7 Jul 2025 13:29:27 +0900 Subject: [PATCH] =?UTF-8?q?[20250707]=20BOJ=20/=20G1=20/=202048=20(Easy)?= =?UTF-8?q?=20/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- khj20006/202507/07 BOJ G1 2048 (Easy).md | 180 +++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 khj20006/202507/07 BOJ G1 2048 (Easy).md diff --git a/khj20006/202507/07 BOJ G1 2048 (Easy).md b/khj20006/202507/07 BOJ G1 2048 (Easy).md new file mode 100644 index 00000000..da5a8028 --- /dev/null +++ b/khj20006/202507/07 BOJ G1 2048 (Easy).md @@ -0,0 +1,180 @@ +```java +import java.util.*; +import java.io.*; + +class IOController { + BufferedReader br; + BufferedWriter bw; + StringTokenizer st; + + public IOController() { + br = new BufferedReader(new InputStreamReader(System.in)); + bw = new BufferedWriter(new OutputStreamWriter(System.out)); + st = new StringTokenizer(""); + } + + String nextLine() throws Exception { + String line = br.readLine(); + st = new StringTokenizer(line); + return line; + } + + String nextToken() throws Exception { + while (!st.hasMoreTokens()) nextLine(); + return st.nextToken(); + } + + int nextInt() throws Exception { + return Integer.parseInt(nextToken()); + } + + long nextLong() throws Exception { + return Long.parseLong(nextToken()); + } + + double nextDouble() throws Exception { + return Double.parseDouble(nextToken()); + } + + void close() throws Exception { + bw.flush(); + bw.close(); + } + + void write(String content) throws Exception { + bw.write(content); + } + +} + +public class Main { + + static IOController io; + + // + + static int N; + static int[][] board; + static int ans = 0; + static String[] dirs = {"left", "right", "up", "down"}; + + public static void main(String[] args) throws Exception { + + io = new IOController(); + + init(); + solve(); + + io.close(); + + } + + public static void init() throws Exception { + + N = io.nextInt(); + board = new int[N][N]; + for(int i=0;i= 5) return; + + for(String dir : dirs) { + + int[][] copy = new int[N][N]; + for(int i=0;i list = new ArrayList<>(); + for(int j=0;j newList = compress(list); + for(int j=0;j= newList.size() ? 0 : newList.get(j); + } + } + } + else if(direction.equals("right")) { + for(int i=0;i list = new ArrayList<>(); + for(int j=N-1;j>=0;j--) if(board[i][j] != 0) { + list.add(board[i][j]); + } + List newList = compress(list); + for(int j=N-1;j>=0;j--) { + board[i][j] = N-j > newList.size() ? 0 : newList.get(N-j-1); + } + } + } + else if(direction.equals("up")) { + for(int j=0;j list = new ArrayList<>(); + for(int i=0;i newList = compress(list); + for(int i=0;i= newList.size() ? 0 : newList.get(i); + } + } + } + else { + for(int j=0;j list = new ArrayList<>(); + for(int i=N-1;i>=0;i--) if(board[i][j] != 0) { + list.add(board[i][j]); + } + List newList = compress(list); + for(int i=N-1;i>=0;i--) { + board[i][j] = N-i > newList.size() ? 0 : newList.get(N-i-1); + } + } + } + + } + + static List compress(List list) { + + List newList = new ArrayList<>(); + for(int j=0;j