프로그래밍 공방

[프로그래머스] 블록 이동하기 본문

개발/문제해결

[프로그래머스] 블록 이동하기

hyosupsong 2021. 1. 14. 21:16

문제

programmers.co.kr/learn/courses/30/lessons/60063

 

코딩테스트 연습 - 블록 이동하기

[[0, 0, 0, 1, 1],[0, 0, 0, 1, 0],[0, 1, 0, 1, 1],[1, 1, 0, 0, 1],[0, 0, 0, 0, 0]] 7

programmers.co.kr

문제해결방법

로봇이 가로로 있는 경우와 세로로 있는 경우를 다른 배열로 관리해서 문제를 풀었다.

1. 기존 방향을 유지하면서 이동하면 같은 방향의 배열에 기록해주고, 회전을 하면 다른 방향의 배열에 기록해준다.

2. 로봇을 이동할 때, 두 칸 중 하나라도 0이면 이동한다.

3. 위 두 가지 규칙을 지키면서 BFS를 돌며 N, N 칸에 도착하면 그 값을 출력해줬다.

코드

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package Programmers;
 
import java.util.ArrayDeque;
import java.util.Queue;
 
public class Solution_블록이동하기 {
 
    public static int n;
    public static int[][] dir = {{10}, {-10}, {01}, {0-1}};
    public static int[][] map;
    public static int[][][] count;
    
    public static boolean isMove(int x, int y) {
        if(x>=0 && x<&& y>=0 && y<&& map[x][y]==0return true;
        return false;
    }
    
    public static int bfs(int i1, int j1, int i2, int j2, int k) {
        int second = 1;
        count[k][i1][j1] = second;
        count[k][i2][j2] = second;
        second++;
        Queue<int[]> q = new ArrayDeque<int[]>();
        q.add(new int[] {i1, j1, i2, j2, k});
        while(!q.isEmpty()) {
            int size = q.size();
            for(int s = 0; s<size; s++) {
                int[] cur = q.poll();
                int x1 = cur[0];
                int y1 = cur[1];
                int x2 = cur[2];
                int y2 = cur[3];
                for(int d=0; d<dir.length; d++) {
                    int xx1 = x1+dir[d][0];
                    int yy1 = y1+dir[d][1];
                    int xx2 = x2+dir[d][0];
                    int yy2 = y2+dir[d][1];
                    if(isMove(xx1, yy1) && isMove(xx2, yy2)) {
                        if(count[cur[4]][xx1][yy1]==0 || count[cur[4]][xx2][yy2]==0) {
                            count[cur[4]][xx1][yy1] = second;
                            count[cur[4]][xx2][yy2] = second;
                            q.add(new int[] {xx1, yy1, xx2, yy2, cur[4]});
                        }
                    }
                }
                if(cur[4]==0) {
                    if(isMove(x1-1, y1) && isMove(x1-1, y2)) {
                        if(count[1][x1-1][y1]==0) {
                            count[1][x1][y1]=second;
                            count[1][x1-1][y1]=second;
                            q.add(new int[] {x1, y1, x1-1, y1, 1});
                        }
                        if(count[1][x1-1][y2]==0) {
                            count[1][x1][y2]=second;
                            count[1][x1-1][y2]=second;
                            q.add(new int[] {x1, y2, x1-1, y2, 1});
                        }
                    }
                    if(isMove(x1+1, y1) && isMove(x1+1, y2)) {
                        if(count[1][x1+1][y1]==0) {
                            count[1][x1][y1]=second;
                            count[1][x1+1][y1]=second;
                            q.add(new int[] {x1, y1, x1+1, y1, 1});
                        }
                        if(count[1][x1+1][y2]==0) {
                            count[1][x1][y2]=second;
                            count[1][x1+1][y2]=second;
                            q.add(new int[] {x1, y2, x1+1, y2, 1});
                        }
                    }
                } else {    // 세로
                    if(isMove(x1, y1-1&& isMove(x2, y1-1)) {
                        if(count[0][x1][y1-1]==0) {
                            count[0][x1][y1]=second;
                            count[0][x1][y1-1]=second;
                            q.add(new int[] {x1, y1, x1, y1-10});
                        }
                        if(count[0][x2][y1-1]==0) {
                            count[0][x2][y1]=second;
                            count[0][x2][y1-1]=second;
                            q.add(new int[] {x2, y1, x2, y1-10});
                        }
                    }
                    if(isMove(x1, y1+1&& isMove(x2, y1+1)) {
                        if(count[0][x1][y1+1]==0) {
                            count[0][x1][y1]=second;
                            count[0][x1][y1+1]=second;
                            q.add(new int[] {x1, y1, x1, y1+10});
                        }
                        if(count[0][x2][y1+1]==0) {
                            count[0][x2][y1]=second;
                            count[0][x2][y1+1]=second;
                            q.add(new int[] {x2, y1, x2, y1+10});
                        }
                    }
                }
                if(count[0][n-1][n-1]!=0 || count[1][n-1][n-1]!=0return second-1
            }
            second++;
        }
        return 0;
    }
    
    public static int solution(int[][] board) {
        int answer = 0;
        map = board;
        n = board.length;
        count = new int[2][n][n];
        answer = bfs(00010);
        return answer;
    }
    
    public static void main(String[] args) {
        int[][] board = {{00011},{00010},{01011},{11001},{00000}};
        System.out.println(solution(board));
    }
}
cs


코드에 대한 피드백이나 더 좋은 아이디어는 언제나 환영입니다.