본문 바로가기

알고리즘

벽돌 깨기[swexpert academy]

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRQm6qfL0DFAUo

 

1] 재귀 2] BFS 3] 시뮬레이션이 짬뽕된 재밌는 문제.

 

10/07 문제풀이

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package academymoi;
 
import java.util.Scanner;
 
public class 벽돌깨기3 {
    static boolean flag = false;
    static int ans = Integer.MAX_VALUE;
    static int[] dx = { 010-1 };
    static int[] dy = { 10-10 };
    static int t, n, w, h;
    static int[] dropLocation;
    static int[][] map, backup;
    static Scanner scan = new Scanner(System.in);
 
    public static void main(String[] args) {
        t = scan.nextInt();
        for (int i = 1; i <= t; i++) {
            input();
            dfs(0);
            System.out.println("#" + i + " " + ans);
            ans = Integer.MAX_VALUE;
            flag = true;
        }
 
    }
 
    private static int count() {
        int cnt = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (map[i][j] != 0)
                    cnt += 1;
            }
        }
 
        return cnt;
    }
 
    private static void calculate() {
        for (int i = 0; i < dropLocation.length; i++) {
            bfs(dropLocation[i]);
 
        }
        int x = count();
        if (x < ans) {
            ans = x;
        }
        if (x == 0) {
            flag = false;
        }
        recover();
    }
 
    private static void bfs(int idx) {
        Queue<Pt> q = new LinkedList<Pt>();
        int temp = -1;
        for (int i = 0; i < h; i++) {
            if (map[i][idx] != 0) {
                temp = i;
                break;
            }
        }
        if (temp == -1)
            return;
 
        q.add(new Pt(temp, idx));
 
        while (!q.isEmpty()) {
            Pt pt = q.peek();
            int range = map[pt.x][pt.y];
            map[pt.x][pt.y] = 0;
            q.remove();
 
            for (int j = 0; j < 4; j++) {
                int nx = pt.x;
                int ny = pt.y;
                for (int k = 0; k < range - 1; k++) {
                    nx = nx + dx[j];
                    ny = ny + dy[j];
//                    if(dropLocation[0]==2 && dropLocation[1]==2 && dropLocation[2]==6) 
//                    {
//                        System.out.println("nx:"+nx+" ny:"+ny);
//                    }
                    if (nx >= 0 && ny >= 0 && nx < h && ny < w) {
 
                        if (map[nx][ny] != 0) {
 
                            q.add(new Pt(nx, ny));
                        }
                    }
                }
            }
        }
 
        for (int i = 0; i < w; i++) {
            ArrayList<Integer> arr = new ArrayList<Integer>();
 
            for (int j = h - 1; j >= 0; j--) {
                if (map[j][i] != 0) {
                    arr.add(map[j][i]);
                }
            }
 
            for (int j = h - 1; j >= 0; j--) {
                map[j][i] = 0;
            }
            int t = h - 1;
            for (int j = 0; j < arr.size(); j++) {
                map[t--][i] = arr.get(j);
            }
        }
 
    }
 
    private static void print() {
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
    }
 
    private static void dfs(int depth) {
 
        if (depth == n) {
            calculate();
 
            return;
        }
        for (int i = 0; i < w; i++) {
            dropLocation[depth] = i;
            dfs(depth + 1);
        }
    }
 
    private static void recover() {
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                map[i][j] = backup[i][j];
            }
        }
    }
 
    private static void input() {
 
        n = scan.nextInt();
        w = scan.nextInt();
        h = scan.nextInt();
        map = new int[h][w];
        backup = new int[h][w];
        dropLocation = new int[n];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                map[i][j] = scan.nextInt();
                backup[i][j] = map[i][j];
            }
        }
//        print();
    }
 
}
 
class Pt {
    int x;
    int y;
 
    public Pt(int x, int y) {
        this.x = x;
        this.y = y;
    }
 
    @Override
    public String toString() {
        return "Pt [x=" + x + ", y=" + y + "]";
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

 

 

 

 

 

 

10/02 풀이 =============================================================

9:25 - 10:49

반성

1] 조합 -> 중복으로 가야함.(0,0,0) (1,1,1)로 갈 수 있음.

2] 구조체에 x,y 뿐만 아니라 map[x][y] 의 값도 저장해서 터트리는게 맞음(안정적)

3] 지도 고치는거 좀 더 고려하고 했어야 <- 여기서 시간 많이 소모

 

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class Solution {
    public static boolean flag = true;
    public static int ans = Integer.MAX_VALUE;
    public static int[] dx = { 010-1 };
    public static int[] dy = { 10-10 };
    public static int t, n, w, h;
    public static int[][] map, backup;
    public static ArrayList<Integer> dropLocation = new ArrayList<Integer>();
    public static int[] dropLoc;
 
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        t = scan.nextInt();
 
        for (int i = 1; i <= t; i++) {
            input(scan);
            f(0);
            System.out.println("#" + i + " " + ans);
            ans = Integer.MAX_VALUE;
            flag = true;
        }
 
        scan.close();
    }
 
    private static void f(int depth) {
        if (!flag)
            return;
        if (depth == n) {
            breakTheWall();
            return;
        }
 
        for (int i = 0; i < w; i++) {
            dropLoc[depth] = i;
            f(depth + 1);
        }
    }
 
    private static void breakTheWall() {
 
        if (!flag)
            return;
 
        for (int i = 0; i < dropLoc.length; i++) {
            int targetY = dropLoc[i];
            int targetVal = 0;
            int targetX = 0;
            for (int j = 0; j < h; j++) {
                if (map[j][targetY] != 0) {
                    targetVal = map[j][targetY];
                    targetX = j;
                    break;
                }
            }
            bfs(targetVal, targetX, targetY);
            fixMap();
            count();
 
        }
 
        recover();
 
    }
 
    private static void count() {
        int cnt = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (map[i][j] != 0)
                    cnt += 1;
            }
        }
        if (cnt == 0) {
            ans = cnt;
            flag = false;
        }
 
        if (ans > cnt)
            ans = cnt;
    }
 
    private static void fixMap() {
        for (int i = 0; i < w; i++) {
            ArrayList<Integer> arr = new ArrayList<Integer>();
            for (int j = h - 1; j >= 0; j--) {
                if (map[j][i] != 0) {
                    arr.add(map[j][i]);
                }
            }
 
            for (int j = 0; j < h; j++)
                map[j][i] = 0;
            int idx = h - 1;
            for (int j = 0; j < arr.size(); j++) {
                map[idx--][i] = arr.get(j);
            }
        }
 
    }
 
    private static void print() {
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
    }
 
    private static void recover() {
 
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                map[i][j] = backup[i][j];
            }
        }
    }
 
    private static void bfs(int targetVal, int targetX, int targetY) {
        Queue<Node> q = new LinkedList<Node>();
        q.add(new Node(targetX, targetY, targetVal));
 
        while (!q.isEmpty()) {
            Node t = q.peek();
            q.remove();
            int cx = t.x;
            int cy = t.y;
            int curVal = t.val;
            map[cx][cy] = 0;
 
            for (int i = 0; i < 4; i++) {
                int tx = cx;
                int ty = cy;
                for (int j = 0; j < curVal - 1; j++) {
                    tx = tx + dx[i];
                    ty = ty + dy[i];
 
                    if (tx >= 0 && ty >= 0 && tx < h && ty < w) {
                        q.add(new Node(tx, ty, map[tx][ty]));
                    }
                }
            }
        }
    }
 
    private static void input(Scanner scan) {
        n = scan.nextInt();
        w = scan.nextInt();
        h = scan.nextInt();
        map = new int[h][w];
        backup = new int[h][w];
        dropLoc = new int[n];
        for (int j = 0; j < h; j++) {
            for (int k = 0; k < w; k++) {
                map[j][k] = scan.nextInt();
                backup[j][k] = map[j][k];
            }
        }
    }
}
 
class Node {
    int x;
    int y;
    int val;
 
    public Node(int x, int y, int val) {
        this.x = x;
        this.y = y;
        this.val = val;
    }
 
    @Override
    public String toString() {
        return "Node [x=" + x + ", y=" + y + "]";
    }
 
}