Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bfcc717

Browse files
Merge pull request #316 from GreatAlgorithm-Study/xubin
[23주차] 배수빈
2 parents 6fae217 + 0f68063 commit bfcc717

File tree

6 files changed

+615
-0
lines changed

6 files changed

+615
-0
lines changed

‎BOJ/1000-5000번/SB_1135.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class SB_1135 {
7+
static int N;
8+
static List<List<Integer>> underling = new ArrayList<>();
9+
10+
private static int call(int node) {
11+
ArrayList<Integer> lst = new ArrayList<>();
12+
13+
if (underling.get(node).isEmpty()) { // 리프노드면 0반환
14+
return 0;
15+
}
16+
17+
for (int ud : underling.get(node)) {
18+
lst.add(call(ud)); // 각 부하들의 전화단계 구하기
19+
}
20+
21+
lst.sort(Collections.reverseOrder());
22+
for (int i = 0; i < lst.size(); i++) {
23+
lst.set(i, lst.get(i) + i + 1);
24+
}
25+
26+
return Collections.max(lst);
27+
}
28+
public static void main(String[] args) throws IOException {
29+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
30+
N = Integer.parseInt(br.readLine());
31+
for (int i = 0; i < N; i++) {
32+
underling.add(new ArrayList<>());
33+
}
34+
35+
StringTokenizer st = new StringTokenizer(br.readLine());
36+
for (int i = 0; i < N; i++) {
37+
int boss = Integer.parseInt(st.nextToken());
38+
if (boss==-1) continue;
39+
underling.get(boss).add(i);
40+
}
41+
42+
System.out.println(call(0));
43+
}
44+
}

‎BOJ/1000-5000번/SB_1450.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.StringTokenizer;
8+
9+
public class SB_1450 {
10+
static int N, C;
11+
static int[] weigth;
12+
static List<Long> sbLst = new ArrayList<>();
13+
static int ans = 0;
14+
15+
private static int find(long x) { // upper-bound
16+
int left = 0;
17+
int right = sbLst.size();
18+
19+
while (left < right) {
20+
int mid = (left + right) / 2;
21+
if (sbLst.get(mid) <= x) left = mid + 1;
22+
else right = mid;
23+
}
24+
return right;
25+
}
26+
27+
28+
private static void findSubSum(int depth, long sm) {
29+
if (depth > N-1) {
30+
ans += find(C-sm); // C-현재값 보다 작거나 같은 수 개수 찾기
31+
return;
32+
}
33+
34+
findSubSum(depth + 1, sm);
35+
if (sm + weigth[depth] <= C) {
36+
findSubSum(depth + 1, sm + weigth[depth]);
37+
}
38+
}
39+
40+
private static void subSum(int depth, long sm) {
41+
if (depth >= (N / 2)) { // 첫번째 그룹은 N/2의 값까지 포함되어야 함
42+
sbLst.add(sm);
43+
return;
44+
}
45+
// 현재 원소 포함
46+
subSum(depth + 1, sm);
47+
48+
// 현재 원소 미포함
49+
if (sm + weigth[depth] <= C) {
50+
subSum(depth + 1, sm + weigth[depth]);
51+
}
52+
}
53+
public static void main(String[] args) throws IOException {
54+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
55+
StringTokenizer st = new StringTokenizer(br.readLine());
56+
57+
N = Integer.parseInt(st.nextToken());
58+
C = Integer.parseInt(st.nextToken());
59+
60+
weigth = new int[N];
61+
st = new StringTokenizer(br.readLine());
62+
for (int i = 0; i < N; i++) {
63+
weigth[i] = Integer.parseInt(st.nextToken());
64+
}
65+
66+
// 첫번째 그룹에서 만들 수 있는 모든 부분집합 합 만들기
67+
subSum(0, 0);
68+
Collections.sort(sbLst);
69+
70+
// 두번째 그룹에서도 만들 수 있는 모든 부분집합 합을 만들고, 위에서 만든 리스트에 C-x했을때 작거나 같은 값 수 찾기(이분탐색)
71+
findSubSum(N / 2, 0);
72+
73+
System.out.println(ans);
74+
}
75+
76+
}

‎BOJ/15001-20000번/SB_19237.java

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class SB_19237 {
7+
static int N, M, K;
8+
static int[] dx = {-1, 1, 0, 0}; // 상하좌우
9+
static int[] dy = {0, 0, -1, 1};
10+
static int[][] board;
11+
static Smell[][] sBoard;
12+
static int[][][] dir;
13+
static Shark[] sharks;
14+
15+
private static boolean checkShark() {
16+
int cnt = 0;
17+
for (Shark s : sharks) {
18+
if (!s.isDead) cnt++;
19+
}
20+
return cnt == 1;
21+
}
22+
23+
private static void spreadSmell() {
24+
for (int i = 0; i < N; i++) {
25+
for (int j = 0; j < N; j++) {
26+
if (sBoard[i][j].idx==0) continue;
27+
sBoard[i][j].t--;
28+
if (sBoard[i][j].t==0) sBoard[i][j].idx = 0;
29+
}
30+
}
31+
}
32+
33+
private static void moveShark() {
34+
int[][] tmpBoard = new int[N][N];
35+
36+
for (Shark s : sharks) {
37+
if (s.idx==0 || s.isDead) continue;
38+
39+
// 아무 냄새 없는 칸 있는지 체크
40+
int mvDir = -1;
41+
for (int d : dir[s.idx][s.d]) { // 현재 상어가 보고있는 방향부터
42+
int nx = s.x + dx[d];
43+
int ny = s.y + dy[d];
44+
if (!isValid(nx, ny) || sBoard[nx][ny].idx!=0) continue;
45+
mvDir = d;
46+
break;
47+
}
48+
49+
if (mvDir==-1) { // 냄새 빈칸이 없을 경우
50+
for (int d : dir[s.idx][s.d]) { // 현재 상어가 바라보는 위치에서 이동 우선순위
51+
int nx = s.x + dx[d];
52+
int ny = s.y + dy[d];
53+
54+
if (!isValid(nx, ny) || sBoard[nx][ny].idx!= s.idx) continue;
55+
mvDir = d;
56+
break;
57+
}
58+
}
59+
60+
// 위치 최종 설정
61+
s.x+= dx[mvDir];
62+
s.y += dy[mvDir];
63+
s.d = mvDir;
64+
65+
if (tmpBoard[s.x][s.y] != 0) {
66+
s.isDead = true;
67+
}else{
68+
board[s.x][s.y] = s.idx;
69+
tmpBoard[s.x][s.y] = K;
70+
}
71+
}
72+
73+
74+
// 모든 상어가 이동이 끝나면 냄새 뿌리기
75+
for (Shark s : sharks) {
76+
if (s.idx==0 || s.isDead) continue;
77+
sBoard[s.x][s.y] = new Smell(s.idx, K);
78+
}
79+
}
80+
81+
private static boolean isValid(int x, int y) {
82+
return 0 <= x && x < N && 0 <= y && y < N;
83+
}
84+
85+
public static void main(String[] args) throws IOException {
86+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
87+
StringTokenizer st = new StringTokenizer(br.readLine());
88+
89+
N = Integer.parseInt(st.nextToken());
90+
M = Integer.parseInt(st.nextToken());
91+
K = Integer.parseInt(st.nextToken());
92+
93+
board = new int[N][N];
94+
sBoard = new Smell[N][N];
95+
dir = new int[M+1][4][4];
96+
97+
// 냄새보드 초기화
98+
for (int i = 0; i < N; i++) {
99+
for (int j = 0; j < N; j++) {
100+
sBoard[i][j] = new Smell(0, 0);
101+
}
102+
}
103+
104+
sharks = new Shark[M + 1];
105+
for (int i = 0; i < N; i++) { // 보드 입력
106+
st = new StringTokenizer(br.readLine());
107+
for (int j = 0; j < N; j++) {
108+
int num = Integer.parseInt(st.nextToken());
109+
board[i][j] = num;
110+
if (num != 0) {
111+
sharks[num] = new Shark(num, i, j);
112+
sBoard[i][j] = new Smell(num, K); // 처음 냄새 남기기
113+
}
114+
}
115+
}
116+
117+
118+
st = new StringTokenizer(br.readLine()); // 상어 방향 입력
119+
for (int i = 1; i <= M; i++) {
120+
int d = Integer.parseInt(st.nextToken())-1;
121+
sharks[i].setDir(d);
122+
}
123+
sharks[0] = new Shark(0, 0, 0);
124+
sharks[0].isDead = true;
125+
126+
for (int k = 1; k <= M; k++) { // 상어별 우선순위 방향 입력
127+
for (int i=0; i<4; i++) {
128+
st = new StringTokenizer(br.readLine());
129+
for (int j = 0; j < 4; j++) {
130+
dir[k][i][j] = Integer.parseInt(st.nextToken())-1;
131+
}
132+
}
133+
}
134+
135+
int time = 0;
136+
while (time++ <= 1000) {
137+
// 상어 이동
138+
moveShark();
139+
140+
// 냄새 시간 --
141+
spreadSmell();
142+
143+
// 상어 체크
144+
if (checkShark()) {
145+
System.out.println(time);
146+
return;
147+
}
148+
}
149+
System.out.println(-1);
150+
}
151+
152+
static class Shark implements Comparable<Shark>{
153+
int x, y, idx, d;
154+
boolean isDead = false;
155+
156+
public Shark(int idx, int x, int y) {
157+
this.idx = idx;
158+
this.x = x;
159+
this.y = y;
160+
}
161+
162+
public void setDir(int d) {
163+
this.d = d;
164+
}
165+
166+
private void move(int x, int y, int d) {
167+
this.x = x;
168+
this.y = y;
169+
this.d = d;
170+
}
171+
172+
@Override
173+
public int compareTo(Shark o) {
174+
return this.idx - o.idx;
175+
}
176+
177+
@Override
178+
public String toString() {
179+
return "Shark{" +
180+
"x=" + x +
181+
", y=" + y +
182+
", idx=" + idx +
183+
", d=" + d +
184+
", isDead=" + isDead +
185+
'}';
186+
}
187+
}
188+
189+
static class Smell{
190+
int idx, t;
191+
192+
public Smell(int idx, int t) {
193+
this.idx = idx;
194+
this.t = t;
195+
}
196+
197+
@Override
198+
public String toString() {
199+
return "Smell{" +
200+
"idx=" + idx +
201+
", t=" + t +
202+
'}';
203+
}
204+
}
205+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /