Skip to content

Commit e4985b0

Browse files
authored
Merge pull request #192 from GreatAlgorithm-Study/dahye
[14주차] 고다혜
2 parents cd474fc + 6819f9c commit e4985b0

File tree

8 files changed

+622
-0
lines changed

8 files changed

+622
-0
lines changed

BOJ/20001-25000번/DH_20181.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.io.*;
2+
import java.util.Arrays;
3+
import java.util.StringTokenizer;
4+
5+
public class DH_20181 {
6+
public static void main(String[] args) throws Exception {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
10+
int N = Integer.parseInt(st.nextToken());
11+
int K = Integer.parseInt(st.nextToken());
12+
13+
int[] arr = new int[N + 1];
14+
long[] dp = new long[N + 1];
15+
16+
st = new StringTokenizer(br.readLine());
17+
18+
for(int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken());
19+
20+
long sum = 0, tmp = 0, result = 0;
21+
int s = 0, e = 0;
22+
23+
// dp[i]: i 지점까지 최선을 다했을 때, 얻을 수 있는 점수
24+
while(e < N + 1) {
25+
if(sum >= K) {
26+
tmp = s == 0 ? 0: Math.max(tmp, dp[s - 1]);
27+
// tmp + sum - K
28+
// : (s전까지 최선을 다했을 때, 얻을 수 있는 점수) + (s ~ e까지 합계) - K
29+
// else 부분에서 e++가 되었으므로 dp[e - 1]을 해줘서 sum을 한 범위까지 확인할 수 있도록 함
30+
dp[e - 1] = Math.max(dp[e - 1], tmp + sum - K);
31+
result = Math.max(result, dp[e - 1]);
32+
sum -= arr[s++];
33+
34+
} else {
35+
sum += arr[e++];
36+
}
37+
}
38+
System.out.println(result);
39+
}
40+
}

BOJ/20001-25000번/DH_20291.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 파일 정리
6+
*/
7+
8+
public class DH_20291 {
9+
public static void main(String[] args) throws Exception {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
12+
int N = Integer.parseInt(br.readLine());
13+
14+
TreeMap<String, Integer> treeMap = new TreeMap<>();
15+
16+
for(int i = 0; i < N; i++) {
17+
String s = br.readLine().split("\\.")[1];
18+
if(treeMap.containsKey(s)) treeMap.put(s, treeMap.get(s) + 1);
19+
else treeMap.put(s, 1);
20+
}
21+
22+
StringBuilder sb = new StringBuilder();
23+
for(String key: treeMap.keySet()) {
24+
sb.append(key).append(" ").append(treeMap.get(key)).append("\n");
25+
}
26+
27+
System.out.println(sb);
28+
}
29+
}

BOJ/20001-25000번/DH_22944.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 죽음의 비
6+
*/
7+
8+
public class DH_22944 {
9+
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
10+
static class Point {
11+
int k, r, c, d, p, u;
12+
public Point(int k, int r, int c, int d, int p, int u) {
13+
this.k = k;
14+
this.r = r;
15+
this.c = c;
16+
this.d = d;
17+
this.p = p;
18+
this.u = u;
19+
}
20+
}
21+
22+
static int N, H, D;
23+
static char[][] map;
24+
static boolean[][][] v;
25+
static Deque<Point> q;
26+
27+
public static void main(String[] args) throws Exception {
28+
initInput();
29+
System.out.println(solution());
30+
}
31+
32+
static int solution() {
33+
int result = -1;
34+
35+
while(!q.isEmpty()) {
36+
Point current = q.poll();
37+
38+
if(map[current.r][current.c] == 'E') {
39+
result = current.d;
40+
break;
41+
}
42+
43+
for(int d = 0; d < 4; d++) {
44+
int nr = current.r + dr[d];
45+
int nc = current.c + dc[d];
46+
int nk = current.k;
47+
int np = current.p;
48+
int nu = current.u;
49+
50+
if(!check(nr, nc) || v[current.k][nr][nc]) continue;
51+
52+
if(map[nr][nc] == '.') {
53+
if(nu > 0) nu -= 1;
54+
else np -= 1;
55+
}
56+
57+
// 우산이라면
58+
else if(map[nr][nc] == 'U') {
59+
map[nr][nc] = '.';
60+
nu = D - 1;
61+
nk = current.k + 1;
62+
}
63+
64+
if(np == 0) continue;
65+
66+
v[current.k][nr][nc] = true;
67+
q.add(new Point(nk, nr, nc, current.d + 1, np, nu));
68+
}
69+
}
70+
71+
return result;
72+
}
73+
74+
static boolean check(int r, int c) {
75+
return r >= 0 && r < N && c >= 0 && c < N;
76+
}
77+
78+
static void initInput() throws Exception {
79+
System.setIn(new FileInputStream("./input/BOJ22944.txt"));
80+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
81+
StringTokenizer st = new StringTokenizer(br.readLine());
82+
83+
N = Integer.parseInt(st.nextToken()); // 한 변의 길이
84+
H = Integer.parseInt(st.nextToken()); // 현재 체력
85+
D = Integer.parseInt(st.nextToken()); // 우산의 내구도
86+
87+
q = new ArrayDeque<Point>();
88+
int uCnt = 0;
89+
90+
int sr = 0, sc =0;
91+
92+
map = new char[N][N];
93+
94+
for(int r = 0; r < N; r++) {
95+
String s = br.readLine();
96+
map[r] = s.toCharArray();
97+
98+
for(int c = 0; c < N; c++) {
99+
if(map[r][c] == 'S') {
100+
sr = r; sc = c;
101+
map[r][c] = '.';
102+
}
103+
if(map[r][c] == 'U') uCnt += 1;
104+
}
105+
}
106+
107+
v = new boolean[uCnt + 1][N][N];
108+
q.add(new Point(0, sr, sc, 0, H, 0));
109+
v[0][sr][sc] = true;
110+
}
111+
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class DH_색깔폭탄 {
5+
static class Point {
6+
int r, c;
7+
public Point(int r, int c) {
8+
this.r = r;
9+
this.c = c;
10+
}
11+
}
12+
static int N, M, score;
13+
static int[][] map;
14+
static final int INF = Integer.MAX_VALUE;
15+
static final int RED = -2, BLACK = -1, EMPTY = 0;
16+
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
17+
18+
public static void main(String[] args) throws Exception {
19+
initInput();
20+
solution();
21+
System.out.println(score);
22+
}
23+
24+
static void solution() {
25+
Deque<Point> bomb;
26+
while((bomb = step1()).size() >= 2) { // 폭탄 묶음 찾기
27+
step2(bomb); // 폭탄 터지기
28+
step3(); // 중력 작용하기
29+
step4(); // 반시계 방향으로 90도 회전하기
30+
step3(); // 중력 작용하기
31+
}
32+
}
33+
34+
// 폭탄 묶음 찾기
35+
static Deque<Point> step1() {
36+
Deque<Point> result = new ArrayDeque<Point>(); // 우선순위에 따라 폭탄이 터질 위치를 저장하는 큐
37+
Deque<Point> tmp = new ArrayDeque<Point>(); // bfs탐색했던 위치들을 모두 저장하는 큐
38+
Deque<Point> q = new ArrayDeque<Point>(); // bfs 탐색하는 큐
39+
40+
// 폭탄묶음의 크기, 빨간색 폭탄의 개수,
41+
int groupSize = -INF, redCnt = INF;
42+
43+
// 빨간색 폭탄은 bfs를 할 때마다 또 다시 사용할 수 있으므로
44+
// 따로 빨간색을 제외한 색깔 폭탄을 확인했는지 구분해주기 위해 bfs에서 사용하는 방문 배열 외에 또 다른 2차원 boolean 배열을 만들어줌
45+
boolean[][] checkColor = new boolean[N][N];
46+
47+
// bfs 탐색을 하는데, 우선순위에 따른 폭탄 묶음을 구하기 위해
48+
// r은 N - 1부터 시작하고, c는 0부터 시작함
49+
// 그리고 빨간 색이 아닌 다른 색깔 폭탄일 때 bfs 탐색 시작
50+
for(int r = N - 1; r >= 0; r--) {
51+
for(int c = 0; c < N; c++) {
52+
if(!isColor(map[r][c]) || checkColor[r][c]) continue;
53+
54+
boolean[][] v = new boolean[N][N];
55+
int currentRedCnt = 0; // 현재 위치에서 폭탄 묶음을 구성할 때, 빨간 폭탄의 개수
56+
int currentSize = 1; // 현재 위치에서 폭탄 묶음을 구성할 때, 폭탄 묶음의 크기
57+
58+
tmp.clear();
59+
60+
q.add(new Point(r, c));
61+
tmp.add(new Point(r, c));
62+
63+
v[r][c] = true;
64+
65+
while(!q.isEmpty()) {
66+
Point current = q.poll();
67+
68+
for(int d = 0; d < 4; d++) {
69+
int nr = current.r + dr[d];
70+
int nc = current.c + dc[d];
71+
72+
// 범위에 벗어났거나, 확인을 한 곳이거나, 돌이 있거나, 빈 공간이라면 continue
73+
if(!check(nr, nc) || v[nr][nc] || map[nr][nc] == BLACK || map[nr][nc] == EMPTY) continue;
74+
75+
// 빨간 폭탄도 아니고, 검정폭탄도 아닌데, 현재 자신의 색깔과 다른 경우
76+
if((map[nr][nc] > 0 && map[nr][nc] != map[r][c])) continue;
77+
78+
// 빨간 폭탄일 경우
79+
if(map[nr][nc] == RED) currentRedCnt += 1;
80+
81+
q.add(new Point(nr, nc));
82+
tmp.add(new Point(nr, nc));
83+
84+
v[nr][nc] = true;
85+
currentSize += 1;
86+
}
87+
}
88+
89+
// 폭탄 묶음 크기가 큰 폭탄부터
90+
if(groupSize < currentSize) {
91+
result.clear();
92+
result.addAll(tmp);
93+
94+
groupSize = currentSize;
95+
redCnt = currentRedCnt;
96+
}
97+
// 폭탄 묶음 크기가 같다면, 빨간 폭탄의 개수 확인하기
98+
else if(groupSize == currentSize) {
99+
if(currentRedCnt < redCnt) {
100+
result.clear();
101+
result.addAll(tmp);
102+
103+
groupSize = currentSize;
104+
redCnt = currentRedCnt;
105+
}
106+
}
107+
}
108+
}
109+
110+
return result;
111+
}
112+
113+
// 폭탄 터지기
114+
static void step2(Deque<Point> bomb) {
115+
score += bomb.size() * bomb.size();
116+
117+
while(!bomb.isEmpty()) {
118+
Point current = bomb.poll();
119+
map[current.r][current.c] = EMPTY; // 빈 공간으로 바꿔주기
120+
}
121+
}
122+
123+
// 중력 작용
124+
// 폭탄들이 있으면 큐에 넣어주고, 돌이 나온다면 gravity(기준선)을 기준으로 쌓아주기
125+
static void step3() {
126+
int[][] tmp = new int[N][N];
127+
Deque<Integer> q = new ArrayDeque<Integer>();
128+
129+
for(int c = 0; c < N; c++) {
130+
int gravity = N;
131+
132+
for(int r = N - 1; r >= 0; r--) {
133+
if(isBomb(map[r][c])) q.add(map[r][c]);
134+
if(map[r][c] == BLACK) {
135+
tmp[r][c] = BLACK;
136+
while(!q.isEmpty()) tmp[--gravity][c] = q.poll();
137+
gravity = r;
138+
}
139+
}
140+
141+
while(!q.isEmpty()) tmp[--gravity][c] = q.poll();
142+
}
143+
144+
map = tmp;
145+
}
146+
147+
// 반시계 방향으로 회전
148+
// (r, c) -> (N - 1 - c, r) 로 바뀜
149+
static void step4() {
150+
int[][] tmp = new int[N][N];
151+
for(int r = 0; r < N; r++) {
152+
for(int c = 0; c < N; c++) {
153+
tmp[N - 1 - c][r] = map[r][c];
154+
}
155+
}
156+
157+
map = tmp;
158+
}
159+
160+
static boolean check(int r, int c) {
161+
return r >= 0 && r < N && c >= 0 && c < N;
162+
}
163+
164+
// 빨간색을 제외한 모든 색깔 폭탄
165+
static boolean isColor(int n) {
166+
return n > 0 && n < M + 1;
167+
}
168+
169+
static boolean isBomb(int n) {
170+
return isColor(n) || n == RED;
171+
}
172+
173+
static void printMapInfo() {
174+
System.out.println("printMapInfo ---------------------");
175+
for(int r = 0; r < map.length; r++) {
176+
System.out.println(Arrays.toString(map[r]));
177+
}
178+
}
179+
180+
static void initInput() throws Exception {
181+
System.setIn(new FileInputStream("./input/색깔폭탄.txt"));
182+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
183+
StringTokenizer st = new StringTokenizer(br.readLine());
184+
185+
N = Integer.parseInt(st.nextToken());
186+
M = Integer.parseInt(st.nextToken());
187+
188+
map = new int[N][N];
189+
for(int r = 0; r < N; r++) {
190+
st = new StringTokenizer(br.readLine());
191+
for(int c = 0; c < N; c++) {
192+
map[r][c] = Integer.parseInt(st.nextToken());
193+
if(map[r][c] == 0) map[r][c] = RED;
194+
}
195+
}
196+
}
197+
}

0 commit comments

Comments
 (0)