슈콩

[BOJ] 12100 2024(Easy) 본문

Algorithms/Baekjoon

[BOJ] 12100 2024(Easy)

shukong 2025. 11. 30. 19:36

 

 

 

[문제]

https://www.acmicpc.net/problem/12100

 

 

[소스 코드]

import java.io.*;
import java.util.*;

public class Main {
	static int[] dr = {-1,1,0,0};
	static int[] dc = {0,0,-1,1};
	static int n;
	static int[][] copy;
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		n = Integer.parseInt(br.readLine());
		int[][] map = new int[n][n];
		for(int i=0;i<n;i++) {
			st = new StringTokenizer(br.readLine());
			for(int j=0;j<n;j++) {
				map[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		int result = 0;
		int total = (int)Math.pow(4, 5);
		for(int i=0;i<total;i++) {
			copy = new int[n][n];
			for(int j=0;j<n;j++) copy[j] = map[j].clone();
			int mode = i;
			for(int k=0;k<5;k++) {
				int dir = mode % 4;
				mode /= 4;
				rotate(dir);
				merge();
			}
			for(int r=0;r<n;r++) {
				for(int c=0;c<n;c++) {
					result = Math.max(result, copy[r][c]);
				}
			}
		}
		System.out.println(result);
	}
	private static void rotate(int dir) {
		int[][] temp = new int[n][n];
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				if(dir==0) temp[i][j] = copy[i][j];
				if(dir==1) temp[i][j] = copy[n-1-j][i];
				if(dir==2) temp[i][j] = copy[n-1-i][n-1-j];
				if(dir==3) temp[i][j] = copy[j][n-1-i];
			}
		}
		copy = temp;
	}
	private static void merge() {
		int[][] temp = new int[n][n];
		for(int i=0;i<n;i++) {
			int pos = 0;
			for(int j=0;j<n;j++) {
				if(copy[i][j]!=0) {
					if(temp[i][pos]==0) temp[i][pos] = copy[i][j];
					else {
						if(temp[i][pos]==copy[i][j]) temp[i][pos++] *= 2;
						else temp[i][++pos] = copy[i][j];
					}
				}
			}
		}
		copy = temp;
	}
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 13458 시험 감독  (0) 2025.11.30
[BOJ] 3190 뱀  (0) 2025.11.30
[BOJ] 13460 구슬 탈출 2  (0) 2025.11.30
[BOJ] 14503 로봇 청소기  (0) 2025.11.29
[BOJ] 9205 맥주 마시면서 걸어가기  (0) 2025.11.29