Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- 코딩테스트
- 이분탐색
- binary search
- MySQL
- SQL
- BOJ
- 문자열
- 건강
- db
- DP
- greedy
- 투포인터
- COS PRO
- Java
- dfs
- priorityqueue
- oracle
- math
- 자바
- 문제해결
- 러닝일지
- 알고리즘
- 시뮬레이션
- BFS
- 운동기록
- 백준
- 프로그래머스
- 코테
- SWEA
- 문제풀이
Archives
- Today
- Total
슈콩
[BOJ] 백준 12100 2024 Easy 본문
회전 방식으로 보완 !
[문제]
https://www.acmicpc.net/problem/12100
[소스 코드]
1. 4가지 방향을 기준으로 수행
import java.io.*;
import java.util.*;
public class Main {
static int[] dr = {-1,1,0,0};
static int[] dc = {0,0,-1,1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int 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 t=0;t<total;t++) {
int mode = t;
int[][] copyM = new int[n][n];
for(int i=0;i<n;i++) {
copyM[i] = map[i].clone();
}
for(int m=0;m<5;m++) {
int rot = mode % 4;
mode /= 4;
if(rot==0) {
for(int j=0;j<n;j++) {
int idx = 0;
int[] temp = new int[n];
for(int i=0;i<n;i++) {
if(copyM[i][j]!=0) {
if(temp[idx]==0) temp[idx] = copyM[i][j];
else {
if(temp[idx]==copyM[i][j]) temp[idx++] *= 2;
else temp[++idx] = copyM[i][j];
}
}
}
for(int i=0;i<n;i++) {
copyM[i][j] = temp[i];
}
}
}
else if(rot==1) {
for(int j=0;j<n;j++) {
int idx = n-1;
int[] temp = new int[n];
for(int i=n-1;i>=0;i--) {
if(copyM[i][j]!=0) {
if(temp[idx]==0) temp[idx] = copyM[i][j];
else {
if(temp[idx]==copyM[i][j]) temp[idx--] *= 2;
else temp[--idx] = copyM[i][j];
}
}
}
for(int i=0;i<n;i++) {
copyM[i][j] = temp[i];
}
}
}
else if(rot==2) {
for(int i=0;i<n;i++) {
int idx = 0;
int[] temp = new int[n];
for(int j=0;j<n;j++) {
if(copyM[i][j]!=0) {
if(temp[idx]==0) temp[idx] = copyM[i][j];
else {
if(temp[idx]==copyM[i][j]) temp[idx++] *= 2;
else temp[++idx] = copyM[i][j];
}
}
}
for(int j=0;j<n;j++) {
copyM[i][j] = temp[j];
}
}
}
else {
for(int i=0;i<n;i++) {
int idx = n-1;
int[] temp = new int[n];
for(int j=n-1;j>=0;j--) {
if(copyM[i][j]!=0) {
if(temp[idx]==0) temp[idx] = copyM[i][j];
else {
if(temp[idx]==copyM[i][j]) temp[idx--] *= 2;
else temp[--idx] = copyM[i][j];
}
}
}
for(int j=0;j<n;j++) {
copyM[i][j] = temp[j];
}
}
}
}
int cnt = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
cnt = Math.max(cnt, copyM[i][j]);
}
}
result = Math.max(result, cnt);
}
System.out.println(result);
}
}
2. 1가지 방향을 기준으로 수행 (횟수 만큼 회전) => 보완
import java.io.*;
import java.util.*;
public class Main {
static int n;
static int[][] map, copyM;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
n = Integer.parseInt(br.readLine());
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 total = (int)Math.pow(4, 5);
int result = 0;
for(int t=0;t<total;t++) {
int mode = t;
copyM = new int[n][n];
for(int i=0;i<n;i++) {
copyM[i] = map[i].clone();
}
for(int m=0;m<5;m++) {
int rot = mode % 4;
mode /= 4;
rotate(rot);
move();
}
int cnt = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(copyM[i][j]>cnt) {
cnt = copyM[i][j];
}
}
}
result = Math.max(result, cnt);
}
System.out.println(result);
}
private static void rotate(int rot) {
int[][] temp = new int[n][n];
for(int i=0;i<n;i++) {
temp[i] = copyM[i].clone();
}
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(rot==1) copyM[i][j] = temp[n-1-j][i];
if(rot==2) copyM[i][j] = temp[n-1-i][n-1-j];
if(rot==3) copyM[i][j] = temp[j][n-1-i];
}
}
}
private static void move() {
for(int i=0;i<n;i++) {
int idx = 0;
int[] temp = new int[n];
for(int j=0;j<n;j++) {
if(copyM[i][j]>0) {
if(temp[idx]==0) temp[idx] = copyM[i][j];
else {
if(temp[idx]==copyM[i][j]) {
temp[idx++] *= 2;
}
else if(temp[idx]!=copyM[i][j]) {
temp[++idx] = copyM[i][j];
}
}
}
}
copyM[i] = temp;
}
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 백준 1253 좋다 (0) | 2025.09.17 |
|---|---|
| [BOJ] 백준 13335 트럭 (0) | 2025.09.17 |
| [BOJ] 백준 11559 Puyo Puyo (0) | 2025.09.17 |
| [BOJ] 백준 12015 가장 긴 증가하는 부분 수열 2 (0) | 2025.09.17 |
| [BOJ] 백준 10815 숫자 카드 (0) | 2025.09.16 |