반응형
문제
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWbrg9uabZsDFAWQ
풀이
- 입력에는 타일을 담을 케이스의 사이즈와 명령어가 입력된다.
-> switch-case 사용. - 방향을 정하면 모든 타일이 그 방향으로 밀리면서 값이 같다면 합쳐진다.
단, 방향에 따라 벽에 닿게 될 타일이 먼저 합쳐진다.
-> 큐를 사용.
-> 방향에 따라 메소드를 분할. - 큐를 사용하기로 생각했기 때문에 큐에 넣을 값을 구상
-> 좌표값과 해당되는 타일의 값을 Point클래스를 만들어 관리
JAVA 코드
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 Solution {
static class Point {
int row, col, ret;
public Point(int row, int col, int ret) {
this.row = row;
this.col = col;
this.ret = ret;
}
}
static int[][] map;
static String command;
static int N;
static void up() {
Queue<Point> q = new LinkedList<Point>(); // 값이 있는 좌표를 담을 큐
for (int i = 0; i < N; i++) { // col 이동
int depth = 0; // 좌표 이동용
for (int j = 0; j < N; j++) {// row 이동
if (map[j][i] != 0) { // 값이 있는 경우
q.offer(new Point(j, i, map[j][i])); // 큐에 담는다.
map[j][i] = 0; // 초기화
}
}
while (!q.isEmpty()) { // 큐가 빌때까지
Point point = new Point(q.peek().row, q.peek().col, q.poll().ret);
if (!q.isEmpty() && point.ret == q.peek().ret) { // 연속된 두수의 값이 같은 경우
map[depth][i] = point.ret + q.poll().ret; // 값을 더한다.
depth++; // 좌표 이동
} else {// 값이 다른 경우
map[depth][i] = point.ret;
depth++;
}
}
}
}
static void down() {
Queue<Point> q = new LinkedList<Point>(); // 값이 있는 좌표를 담을 큐
for (int i = 0; i < N; i++) { // col 이동
int depth = N - 1; // 좌표 이동용
for (int j = N - 1; j >= 0; j--) {// row 이동
if (map[j][i] != 0) { // 값이 있는 경우
q.offer(new Point(j, i, map[j][i])); // 큐에 담는다.
map[j][i] = 0; // 초기화
}
}
while (!q.isEmpty()) { // 큐가 빌때까지
Point point = new Point(q.peek().row, q.peek().col, q.poll().ret);
if (!q.isEmpty() && point.ret == q.peek().ret) { // 연속된 두수의 값이 같은 경우
map[depth][i] = point.ret + q.poll().ret; // 값을 더한다.
depth--; // 좌표 이동
} else {// 값이 다른 경우
map[depth][i] = point.ret;
depth--;
}
}
}
}
static void right() {
Queue<Point> q = new LinkedList<Point>(); // 값이 있는 좌표를 담을 큐
for (int i = 0; i < N; i++) { // row 이동
int depth = N - 1; // 좌표 이동용
for (int j = N - 1; j >= 0; j--) {// col 이동
if (map[i][j] != 0) { // 값이 있는 경우
q.offer(new Point(i, j, map[i][j])); // 큐에 담는다.
map[i][j] = 0; // 초기화
}
}
while (!q.isEmpty()) { // 큐가 빌때까지
Point point = new Point(q.peek().row, q.peek().col, q.poll().ret);
if (!q.isEmpty() && point.ret == q.peek().ret) { // 연속된 두수의 값이 같은 경우
map[i][depth] = point.ret + q.poll().ret; // 값을 더한다.
depth--; // 좌표 이동
} else {// 값이 다른 경우
map[i][depth] = point.ret;
depth--;
}
}
}
}
static void left() {
Queue<Point> q = new LinkedList<Point>(); // 값이 있는 좌표를 담을 큐
for (int i = 0; i < N; i++) { // row이동
int depth = 0; // 좌표 이동용
for (int j = 0; j < N; j++) {// col 이동
if (map[i][j] != 0) { // 값이 있는 경우
q.offer(new Point(i, j, map[i][j])); // 큐에 담는다.
map[i][j] = 0; // 초기화
}
}
while (!q.isEmpty()) { // 큐가 빌때까지
Point point = new Point(q.peek().row, q.peek().col, q.poll().ret);
if (!q.isEmpty() && point.ret == q.peek().ret) { // 연속된 두수의 값이 같은 경우
map[i][depth] = point.ret + q.poll().ret; // 값을 더한다.
depth++; // 좌표 이동
} else {// 값이 다른 경우
map[i][depth] = point.ret;
depth++;
}
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int tc = Integer.parseInt(br.readLine());
for (int test = 1; test <= tc; test++) {
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
command = 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());
}
} // end of input
switch (command) {
case "up":
up();
break;
case "down":
down();
break;
case "left":
left();
break;
case "right":
right();
break;
} // end of switch-case
// output
sb.append("#" + test + "\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sb.append(map[i][j] + " ");
}
sb.append("\n");
}
} // end of testcase
System.out.println(sb);
} // end of main
}
반응형
'Algorithm > 문제 풀이' 카테고리의 다른 글
[SWEA_4366 - JAVA] 정식이의 은행업무 (0) | 2020.05.01 |
---|---|
[SWEA_4050 - JAVA] 재관이의 대량할인 (0) | 2020.04.30 |
[BAEKJOON_16234 - JAVA] 인구 이동 (0) | 2020.04.24 |
[SWEA_7701 - JAVA] 염라대왕의 이름 정렬 (1) | 2020.03.12 |
[SWEA_1907 - JAVA] 모래성 쌓기 (0) | 2020.03.03 |