BOJ_12100_2048(Easy)_java
2023. 2. 22. 11:20ㆍAlgorithm/BOJ
728x90
https://www.acmicpc.net/problem/12100
12100번: 2048 (Easy)
첫째 줄에 보드의 크기 N (1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 게임판의 초기 상태가 주어진다. 0은 빈 칸을 나타내며, 이외의 값은 모두 블록을 나타낸다. 블록에 쓰여 있는 수는 2
www.acmicpc.net
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
백준 12100번 2048(Easy)
골드 2
*/
public class BOJ_12100_2048_Easy {
private static int max = 0;
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());
}
}
System.out.println(solution(N, map));
}
public static int solution(int N, int[][] map){
dfs(0, 0, map);
int ans = max;
return ans;
}
// cnt: 이동수, direction: 이동 방향
public static void dfs(int cnt, int direction, int[][] map){
if (cnt == 6){ // 최대 이동 회수 - cnt가 0일때 이동 안함 -> 6
max = Math.max(calMax(map), max);
return;
}
int[][] newMap = moveController(direction, map);
for (int i = 1; i<5; i++){ // 4가지 이동방향
dfs(cnt+1, i, newMap);
}
}
public static int[][] moveController(int direction, int[][] map){
int[][] newMap;
switch (direction){
case 1:
newMap = moveLeft(map);
break;
case 2:
newMap = moveRight(map);
break;
case 3:
newMap = moveUp(map);
break;
case 4:
newMap = moveDown(map);
break;
default:
newMap = map;
break;
}
return newMap;
}
public static int[][] moveLeft(int[][] map){
int[][] newMap = copyMap(map);
int size = newMap.length;
for (int i = 0; i< size; i++){
int idx = 0; // 블록을 이동해야할 위치
int block = 0; // 이동할 블럭
for (int j = 0; j<size; j++){
if (newMap[i][j] != 0){
if (newMap[i][j] == block){ // 같은 숫자 블록이 있는 경우
newMap[i][idx-1] = block * 2; // 블록 합치기
block = 0;
newMap[i][j] = 0;
}else{
block = newMap[i][j]; // 블록 최신화
newMap[i][j] = 0;
newMap[i][idx] = block; // 현재 블록 저장
idx++; // 이동해야할 위치 변경
}
}
}
}
return newMap;
}
public static int[][] moveRight(int[][] map){
int[][] newMap = copyMap(map);
int size = newMap.length;
for (int i = 0; i< size; i++){
int idx = size -1 ;
int block = 0;
for (int j = size-1; j>=0; j--){
if (newMap[i][j] != 0){
if (newMap[i][j] == block){
newMap[i][idx+1] = block * 2;
block = 0;
newMap[i][j] = 0;
}else{ // 이동 자리가 0 인경우
block = newMap[i][j]; // 이동
newMap[i][j] = 0; // 자기 자리 0 으로
newMap[i][idx] = block;
idx--;
}
}
}
}
return newMap;
}
public static int[][] moveUp(int[][] map){
int[][] newMap = copyMap(map);
int size = newMap.length;
for (int i = 0; i<size; i++){
int idx = 0 ;
int block = 0;
for (int j = 0; j<size; j++){
if (newMap[j][i] != 0){
if (newMap[j][i] == block){
newMap[idx-1][i] = block * 2;
block = 0;
newMap[j][i] = 0;
}else {
block = newMap[j][i];
newMap[j][i] = 0;
newMap[idx][i] = block;
idx++;
}
}
}
}
return newMap;
}
public static int[][] moveDown(int[][] map){
int[][] newMap = copyMap(map);
int size = newMap.length;
for (int i = 0; i<size; i++){
int idx = size-1 ;
int block = 0;
for (int j = size-1; j>=0; j--){
if (newMap[j][i] != 0){
if (newMap[j][i] == block){
newMap[idx+1][i] = block * 2;
block = 0;
newMap[j][i] = 0;
}else {
block = newMap[j][i];
newMap[j][i] = 0;
newMap[idx][i] = block;
idx--;
}
}
}
}
return newMap;
}
//배열 출력
public static void drawMap(int[][] map){
for (int i = 0; i<map.length; i++){
for (int j = 0; j<map[i].length; j++){
System.out.print(map[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
// 배열 복사
public static int[][] copyMap(int[][] map){
int[][] newMap = new int[map.length][map[0].length];
for (int i = 0; i<map.length; i++){
for (int j = 0; j<map[i].length; j++){
newMap[i][j] = map[i][j];
}
}
return newMap;
}
public static int calMax(int[][] map){
int max = 0;
for (int i = 0; i<map.length; i++){
for (int j = 0; j<map[i].length; j++){
max = Math.max(max, map[i][j]);
}
}
return max;
}
}
'Algorithm > BOJ' 카테고리의 다른 글
BOJ_1202_보석 도둑_java (0) | 2023.02.18 |
---|---|
BOJ_1644_소수의 연속합_java (0) | 2023.02.14 |
BOJ_16724_피리 부는 사나이_java (0) | 2023.02.13 |
BOJ_9466_텀 프로젝트_java (0) | 2023.02.13 |
BOJ_2342_Dance Dance Revolution_java (0) | 2023.02.10 |