-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgrammers_388353.java
More file actions
95 lines (83 loc) · 2.75 KB
/
Copy pathProgrammers_388353.java
File metadata and controls
95 lines (83 loc) · 2.75 KB
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
package programmers.lv2;
import java.util.LinkedList;
import java.util.Queue;
public class Programmers_388353 {
int N;
int M;
char[][] map;
int[][] pos = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } }; // 상하좌우
public static void main(String[] args) {
Programmers_388353 test = new Programmers_388353();
String[] storage = { "HAH", "HBH", "HHH", "HAH", "HBH" };
String[] requests = { "C", "B", "B", "B", "B", "H" };
int result = test.solution(storage, requests);
System.out.println("result: " + result);
}
public int solution(String[] storage, String[] requests) {
int answer = 0;
N = storage.length;
M = storage[0].length();
map = new char[N][M];
for (int i = 0; i < N; i++) {
map[i] = storage[i].toCharArray();
}
for (int i = 0; i < requests.length; i++) {
String request = requests[i];
if (request.length() > 1) {
// crane
crane(request.charAt(0));
} else {
// lift
lift(request.charAt(0));
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j] >= 'A' && map[i][j] <= 'Z') {
answer++;
}
}
}
return answer;
}
public void crane(char container) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j] == container) {
map[i][j] = '0';
}
}
}
}
public void lift(char container) {
Queue<int[]> queue = new LinkedList<>();
boolean[][] visited = new boolean[N][M];
for (int i = 0; i < N; i++) {
queue.add(new int[] { i, -1 });
queue.add(new int[] { i, M });
}
for (int j = 0; j < M; j++) {
queue.add(new int[] { -1, j });
queue.add(new int[] { N, j });
}
while (!queue.isEmpty()) {
int[] arr = queue.poll();
for (int i = 0; i < pos.length; i++) {
int nr = arr[0] + pos[i][0];
int nc = arr[1] + pos[i][1];
if (nr < 0 || nc < 0 || nr >= N || nc >= M) {
continue;
} else {
if (!visited[nr][nc]) {
visited[nr][nc] = true;
if (map[nr][nc] == container) {
map[nr][nc] = '0';
} else if (map[nr][nc] == '0') {
queue.add(new int[] { nr, nc });
}
}
}
}
}
}
}