https://www.acmicpc.net/problem/12100
12100번: 2048 (Easy)
첫째 줄에 보드의 크기 N (1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 게임판의 초기 상태가 주어진다. 0은 빈 칸을 나타내며, 이외의 값은 모두 블록을 나타낸다. 블록에 쓰여 있는 수는 2
www.acmicpc.net
풀이
문제를 보았을 때 알 수 있는 힌트
2048을 구현하는 문제로 왼쪽, 오른쪽, 아래, 위로 움직이는 함수를 우선 만들었다.
그리고 그래프의 결과가 얕은 복사여서 다른 방향으로 움직일때 문제가 생길 수 있어서 깊은 복사 하는 메서드를 만들었다.
이렇게 구현한 방향을 총 5번 반복할수있는 dfs를 만들면 끝이나는 문제였다.
여기서 방향에 맞춰 움직이는 것이 어려웠지 그부분만 구현하고 나서는 쉽게 풀수 있는 문제였다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
static int n;
static int[][] graph;
static int result;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
graph = new int[n][n];
for (int i = 0; i < n; i++) {
String[] token = br.readLine().split(" ");
for (int j = 0; j < n; j++) {
graph[i][j] = Integer.parseInt(token[j]);
}
}
// 입력 완료
dfs(0,graph);
System.out.println(result);
}
static void dfs(int cnt, int[][] graph) {
int[][] temp = graph;
if(cnt==5) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
result = Math.max(result, temp[i][j]);
}
}
return;
}
dfs(cnt+1,left(temp));
dfs(cnt+1,right(temp));
dfs(cnt+1,top(temp));
dfs(cnt+1,down(temp));
// print(temp);
}
static int[][] left(int[][] graph) {
int[][] temp = new int[n][n];
temp= deepCopy(temp,graph);
int[][] newGraph = new int[n][n];
for (int i = 0; i < n; i++) {
int curPoint=0;
int j=0;
int tempCount = temp[i][j];
while(true) {
j++;
if(j>n-1) break;
if(temp[i][j] ==0) continue;
if(tempCount==0) {
tempCount=temp[i][j];
continue;
}
if(temp[i][j]==tempCount) {
newGraph[i][curPoint++]= tempCount*2;
temp[i][j] = 0;
tempCount = 0;
}
else if(temp[i][j]!=tempCount){
newGraph[i][curPoint++]= tempCount;
tempCount = temp[i][j];
}
}
if(tempCount!=0) {
newGraph[i][curPoint]= tempCount;
}
}
return newGraph;
}
static int[][] right(int[][] graph) {
int[][] temp = new int[n][n];
temp= deepCopy(temp,graph);
int[][] newGraph = new int[n][n];
for (int i = 0; i < n; i++) {
int curPoint=n-1;
int j=n-1;
int tempCount = temp[i][j];
while(true) {
j--;
if(j<0) break;
if(temp[i][j] ==0) continue;
if(tempCount==0) {
tempCount=temp[i][j];
continue;
}
if(temp[i][j]==tempCount) {
newGraph[i][curPoint--]= tempCount*2;
temp[i][j] = 0;
tempCount = 0;
}
else if(temp[i][j]!=tempCount){
newGraph[i][curPoint--]= tempCount;
tempCount = temp[i][j];
}
}
if(tempCount!=0) {
newGraph[i][curPoint]= tempCount;
}
}
return newGraph;
}
static int[][] down(int[][] graph) {
int[][] temp = new int[n][n];
temp= deepCopy(temp,graph);
int[][] newGraph = new int[n][n];
for (int j = 0; j < n; j++) {
int curPoint=n-1;
int i=n-1;
int tempCount = temp[i][j];
while(true) {
i--;
if(i<0) break;
if(temp[i][j] ==0) continue;
if(tempCount==0) {
tempCount=temp[i][j];
continue;
}
if(temp[i][j]==tempCount) {
newGraph[curPoint--][j]= tempCount*2;
temp[i][j] = 0;
tempCount = 0;
}
else if(temp[i][j]!=tempCount){
newGraph[curPoint--][j]= tempCount;
tempCount = temp[i][j];
}
}
if(tempCount!=0) {
newGraph[curPoint][j]= tempCount;
}
}
return newGraph;
}
static int[][] top(int[][] graph) {
int[][] temp = new int[n][n];
temp= deepCopy(temp,graph);
int[][] newGraph = new int[n][n];
for (int j = 0; j < n; j++) {
int curPoint=0;
int i=0;
int tempCount = temp[i][j];
while(true) {
i++;
if(i>n-1) break;
if(temp[i][j] ==0) continue;
if(tempCount==0) {
tempCount=temp[i][j];
continue;
}
if(temp[i][j]==tempCount) {
newGraph[curPoint++][j]= tempCount*2;
temp[i][j] = 0;
tempCount = 0;
}
else if(temp[i][j]!=tempCount){
newGraph[curPoint++][j]= tempCount;
tempCount = temp[i][j];
}
}
if(tempCount!=0) {
newGraph[curPoint][j]= tempCount;
}
}
return newGraph;
}
static int[][] deepCopy(int[][] temp, int[][] graph){
for (int i = 0; i < temp.length; i++) {
for (int j = 0; j < temp[0].length; j++) {
temp[i][j] = graph[i][j];
}
}
return temp;
}
static void print(int[][] graph) {
for (int[] is : graph) {
System.out.println(Arrays.toString(is));
}
System.out.println();
}
}
https://github.com/dydwkd486/coding_test/blob/main/baekjoon/java/baekjoon12100/Main.java
'Coding Test > baekjoon' 카테고리의 다른 글
[백준 17135] 캐슬 디펜스- JAVA (solved.ac - 골드 3) (0) | 2022.08.20 |
---|---|
[백준 12094] 2048 (Hard)- JAVA (solved.ac - 플레 4) (0) | 2022.08.20 |
[백준 3109] 빵집- JAVA (solved.ac - 골드 2) (0) | 2022.08.18 |
[백준 1946] 신입 사원- JAVA (solved.ac - 실버 1) (0) | 2022.08.17 |
[백준 11651] 좌표 정렬하기 2 - JAVA (solved.ac - 실버 5) (0) | 2022.07.25 |