코드와이
[SW Expert Academy] 6109. 추억의 2048게임 본문
문제링크
swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWbrg9uabZsDFAWQ
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
package D4;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class 추억의_2048_게임 {
	static int n, map[][];
	static String dir;
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		StringBuilder sb = new StringBuilder();
		
		int T = Integer.parseInt(br.readLine());
		
		for(int tc = 1 ; tc <= T ; tc++) {
			sb.append("#").append(tc).append("\n");
			st = new StringTokenizer(br.readLine());
			
			n = Integer.parseInt(st.nextToken());
			dir = st.nextToken();
			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());
				}
			}
			go();
			for(int[] arr : map) {
				for(int a : arr) sb.append(a).append(" ");
				sb.append("\n");
			}
		}
		sb.setLength(sb.length() - 1);
		System.out.println(sb);
	}
	
	public static void go() {
		
		Queue<Integer> queue = new LinkedList<Integer>();
		int tmp;
		switch (dir) {
		case "up":
			for(int j = 0 ; j < n ; j++) {
				for(int i = 0 ; i < n ; i++) {
					tmp = -1;
					if(map[i][j] != 0) {
						tmp = map[i][j];
					}
					for(int k = i + 1; k < n ; k++) {
						if(map[k][j] != 0) {
							if(tmp == map[k][j]) {
								map[i][j] = tmp*2;
								map[k][j] = 0;
								i = k;
							}
							break;
						}
					}
				}
				for(int i = 0 ; i < n ; i++) {
					if(map[i][j] != 0) {
						queue.offer(map[i][j]);
						map[i][j] = 0;
					}
				}
				int size = queue.size();
				for(int i = 0 ; i < size ; i++) {
					map[i][j] = queue.poll();
				}
				
			}
			break;
		case "down":
			for(int j = 0 ; j < n ; j++) {
				for(int i = n-1 ; i >= 0 ; i--) {
					tmp = -1;
					if(map[i][j] != 0) {
						tmp = map[i][j];
					}
					for(int k = i - 1; k >= 0 ; k--) {
						if(map[k][j] != 0) {
							if(tmp == map[k][j]) {
								map[i][j] = tmp*2;
								map[k][j] = 0;
								i = k;
							}
							break;
						}
					}
				}
				for(int i = n-1 ; i >= 0 ; i--) {
					if(map[i][j] != 0) {
						queue.offer(map[i][j]);
						map[i][j] = 0;
					}
				}
				int size = queue.size();
				for(int i = 0 ; i < size ; i++) {
					map[n - 1 - i][j] = queue.poll();
				}
				
			}
			break;
		case "left":
			for(int i = 0 ; i < n ; i++) {
				for(int j = 0 ; j < n ; j++) {
					tmp = -1;
					if(map[i][j] != 0) {
						tmp = map[i][j];
					}
					for(int k = j + 1; k < n ; k++) {
						if(map[i][k] != 0) {
							if(tmp == map[i][k]) {
								map[i][j] = tmp*2;
								map[i][k] = 0;
								j = k;
							}
							break;
						}
					}
				}
				for(int j = 0 ; j < n ; j++) {
					if(map[i][j] != 0) {
						queue.offer(map[i][j]);
						map[i][j] = 0;
					}
				}
				int size = queue.size();
				for(int j = 0 ; j < size ; j++) {
					map[i][j] = queue.poll();
				}
				
			}
			break;
		case "right":
			for(int i = 0 ; i < n ; i++) {
				for(int j = n-1 ; j >= 0 ; j--) {
					tmp = -1;
					if(map[i][j] != 0) {
						tmp = map[i][j];
					}
					for(int k = j - 1; k >= 0 ; k--) {
						if(map[i][k] != 0) {
							if(tmp == map[i][k]) {
								map[i][j] = tmp*2;
								map[i][k] = 0;
								j = k;
							}
							break;
						}
					}
				}
				for(int j = n-1 ; j >= 0 ; j--) {
					if(map[i][j] != 0) {
						queue.offer(map[i][j]);
						map[i][j] = 0;
					}
				}
				int size = queue.size();
				for(int j = 0 ; j < size ; j++) {
					map[i][n - 1 - j] = queue.poll();
				}
				
			}
			break;
		}
	}
}'SW_Expert' 카테고리의 다른 글
| [SW Expert Academy] 1808. 지희의 고장난 계산기 (0) | 2021.05.04 | 
|---|---|
| [SW Expert Academy] 7465. 창용 마을 무리의 개수 (0) | 2021.04.27 | 
| [SW Expert Academy] 2383. [모의 SW 역량테스트] 점심 식사시간 (0) | 2021.04.23 | 
| [SW Expert Academy] 2112. [모의 SW 역량테스트] 보호 필름 (0) | 2021.04.22 | 
| [SW Expert Academy] 4301. 콩 많이 심기 (0) | 2021.04.22 | 
