-
Notifications
You must be signed in to change notification settings - Fork 4
[15주차] 백제완 #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[15주차] 백제완 #208
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
24932ae
백제완: [CT] 윷놀이 사기단_241216
jewan100 7ac1bd9
백제완: [BOJ] 1992 쿼드트리_241217
jewan100 a243419
백제완: [BOJ] 1030 프렉탈 평면_241217
jewan100 00de61f
백제완: [BOJ] 1725 히스토그램_241218
jewan100 0cf8da5
백제완: [BOJ] 9372 상근이의 여행_241219
jewan100 1775d6c
백제완: [BOJ] 4803 트리_241219
jewan100 e52ef83
백제완: [PG] 42892 길 찾기 게임_241220
jewan100 eaa828c
백제완: [SQL] Consecutive Numbers_241217
jewan100 20c82c1
백제완: [SQL] Investments in 2016_241219
jewan100 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
BOJ/1000-5000번/JW_1030.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.util.StringTokenizer; | ||
|
||
public class JW_1030 { | ||
|
||
static int s, n, k, r1, r2, c1, c2; | ||
static boolean[][] board; | ||
|
||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
s = Integer.parseInt(st.nextToken()); | ||
n = Integer.parseInt(st.nextToken()); | ||
k = Integer.parseInt(st.nextToken()); | ||
r1 = Integer.parseInt(st.nextToken()); | ||
r2 = Integer.parseInt(st.nextToken()); | ||
c1 = Integer.parseInt(st.nextToken()); | ||
c2 = Integer.parseInt(st.nextToken()); | ||
board = new boolean[r2 - r1 + 1][c2 - c1 + 1]; | ||
makeBoard(s, 0, 0, false); | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < board.length; i++) { | ||
for (int j = 0; j < board[i].length; j++) | ||
if (board[i][j]) | ||
sb.append("1"); | ||
else | ||
sb.append("0"); | ||
sb.append("\n"); | ||
} | ||
System.out.println(sb); | ||
} | ||
|
||
// 분할 정복으로 검은색 타일 채우기 | ||
private static void makeBoard(int depth, int y, int x, boolean tile) { | ||
int len = (int) Math.pow(n, depth); // 그리드의 길이 | ||
// 확인하지 않아도 되는 그리드라면 스킵 | ||
if (isNotOverlap(y, x, len)) { | ||
return; | ||
} | ||
// 재귀 종료 조건 | ||
if (depth == 0) { | ||
board[y - r1][x - c1] = tile; // true = 검은색 타일 | ||
return; | ||
} | ||
int sLen = len / n; // 서브그리드의 길이 | ||
int bsy = y + sLen * (n - k) / 2, bsx = x + sLen * (n - k) / 2; // 검은색 타일이 시작하는 좌표 | ||
int bLen = sLen * k; // 검은색 타일의 길이 | ||
// 서브 그리드별로 확인 | ||
for (int i = y; i < y + len; i += sLen) { | ||
for (int j = x; j < x + len; j += sLen) { | ||
makeBoard(depth - 1, i, j, tile | isBlack(bsy, bsx, bLen, i, j)); // 다음 재귀 진행, 검은색 타일 체크 | ||
} | ||
} | ||
} | ||
|
||
// 유효한 그리드인지 체크 | ||
private static boolean isNotOverlap(int y, int x, int len) { | ||
return y + len <= r1 || y > r2 || x + len <= c1 || x > c2; | ||
} | ||
|
||
// 검은색 타일 체크 | ||
private static boolean isBlack(int bsy, int bsx, int bLen, int y, int x) { | ||
return bsy <= y && y < bsy + bLen && bsx <= x && x < bsx + bLen; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
BOJ/1000-5000번/JW_1725_1.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
|
||
public class JW_1725_1 { | ||
|
||
public static void main(String[] args) throws Exception { | ||
int n = read(); | ||
int[] arr = new int[n]; | ||
for (int i = 0; i < n; i++) | ||
arr[i] = read(); | ||
Deque<Integer> dq = new ArrayDeque<>(); // 가질 수 있는 최대 길이를 저장할 스택(덱) | ||
int[] left = new int[n]; // 각 인덱스 별로 가질 수 있는 왼쪽으로의 최대 길이 | ||
for (int i = 0; i < n; i++) { | ||
left[i] = 1; // 초기값 = 자기 자신의 너비 | ||
// 스택(덱)에 자신보다 큰 높이의 값이 있다면 해당 너비를 포함할 수 있음 | ||
while (!dq.isEmpty() && arr[dq.peekLast()] >= arr[i]) | ||
left[i] += left[dq.pollLast()]; // i 인덱스의 왼쪽 최대 길이에 합 | ||
dq.offerLast(i); // 사용한 인덱스를 삽입 | ||
} | ||
dq.clear(); // 스택(덱) 초기화 | ||
// 왼쪽과 동일하게 진행 | ||
int[] right = new int[n]; | ||
for (int i = n - 1; i >= 0; i--) { | ||
right[i] = 1; | ||
while (!dq.isEmpty() && arr[dq.peekLast()] >= arr[i]) | ||
right[i] += right[dq.pollLast()]; | ||
dq.offerLast(i); | ||
} | ||
int max = 0; | ||
for (int i = 0; i < n; i++) | ||
max = Math.max(max, arr[i] * (left[i] + right[i] - 1)); // 넓이 계산 | ||
System.out.println(max); | ||
} | ||
|
||
private static int read() throws Exception { | ||
int c, n = System.in.read() & 15; | ||
while ((c = System.in.read()) >= 48) | ||
n = (n << 3) + (n << 1) + (c & 15); | ||
if (c == 13) | ||
System.in.read(); | ||
return n; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
BOJ/1000-5000번/JW_1725_2.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
public class JW_1725_2 { | ||
|
||
static int[] arr; | ||
static int maxArea = 0; | ||
|
||
public static void main(String[] args) throws Exception { | ||
int n = read(); | ||
arr = new int[n]; | ||
for (int i = 0; i < n; i++) | ||
arr[i] = read(); | ||
recursive(0, n - 1); | ||
System.out.println(maxArea); | ||
} | ||
|
||
// 분할 정복 | ||
private static void recursive(int l, int r) { | ||
// 종료 조건 | ||
if (l == r) { | ||
maxArea = Math.max(maxArea, arr[l]); | ||
return; | ||
} | ||
int m = (l + r) / 2; | ||
int h = arr[m]; | ||
maxArea = Math.max(maxArea, arr[m]); // 너비가 1인 넓이 계산 | ||
int toL = m, toR = m; | ||
// m을 기준으로 왼쪽, 오른쪽으로 1씩 늘려가면서 최대 넓이 계산 → 항상 최대높이의 왼쪽 or 오른쪽으로 최댓값 | ||
while (l < toL && toR < r) { | ||
// 오른쪽으로 넓히는게 이득이라면 | ||
if (arr[toL - 1] < arr[toR + 1]) { | ||
h = Math.min(h, arr[++toR]); // 최대 높이 갱신 | ||
} else { | ||
h = Math.min(h, arr[--toL]); // 최대 높이 갱신 | ||
} | ||
maxArea = Math.max(maxArea, h * (toR - toL + 1)); | ||
} | ||
// 남은 인덱스까지 계산 | ||
while (toR < r) { | ||
h = Math.min(h, arr[++toR]); | ||
maxArea = Math.max(maxArea, h * (toR - toL + 1)); | ||
} | ||
while (l < toL) { | ||
h = Math.min(h, arr[--toL]); | ||
maxArea = Math.max(maxArea, h * (toR - toL + 1)); | ||
} | ||
// 남은 부분에 대해서 재귀 호출 | ||
recursive(l, m); | ||
recursive(m + 1, r); | ||
} | ||
|
||
private static int read() throws Exception { | ||
int c, n = System.in.read() & 15; | ||
while ((c = System.in.read()) >= 48) | ||
n = (n << 3) + (n << 1) + (c & 15); | ||
if (c == 13) | ||
System.in.read(); | ||
return n; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
BOJ/1000-5000번/JW_1992.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
|
||
public class JW_1992 { | ||
|
||
static char[][] board; | ||
static StringBuilder sb = new StringBuilder(); | ||
|
||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int n = Integer.parseInt(br.readLine()); | ||
board = new char[n][n]; | ||
for (int i = 0; i < n; i++) | ||
board[i] = br.readLine().toCharArray(); | ||
// 압축 | ||
quardTree(n, 0, 0); | ||
System.out.println(sb); | ||
} | ||
|
||
// 분할 정복, 재귀를 통한 압축 | ||
private static void quardTree(int n, int y, int x) { | ||
// 압축이 가능하다면 압축 | ||
if (isPossible(n, y, x)) { | ||
sb.append(board[y][x]); | ||
// 압축이 불가능하다면 분할 정복으로 압축 시도 | ||
} else { | ||
sb.append("("); | ||
quardTree(n / 2, y, x); | ||
quardTree(n / 2, y, x + n / 2); | ||
quardTree(n / 2, y + n / 2, x); | ||
quardTree(n / 2, y + n / 2, x + n / 2); | ||
sb.append(")"); | ||
} | ||
} | ||
|
||
// 모두 같은 값을 가지는지 확인할 함수 | ||
private static boolean isPossible(int n, int y, int x) { | ||
for (int i = 0; i < n; i++) | ||
for (int j = 0; j < n; j++) | ||
if (board[y][x] != board[y + i][x + j]) | ||
return false; | ||
return true; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
BOJ/1000-5000번/JW_4803.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import java.util.HashSet; | ||
|
||
public class JW_4803 { | ||
|
||
static int[] parent; | ||
|
||
public static void main(String[] args) throws Exception { | ||
StringBuilder sb = new StringBuilder(); | ||
int idx = 0; | ||
while (true) { | ||
int n = read(), m = read(); | ||
if (n == 0 && m == 0) | ||
break; | ||
parent = new int[n + 1]; | ||
for (int i = 0; i < n + 1; i++) | ||
parent[i] = i; | ||
boolean[] isCycle = new boolean[n + 1]; // 해당 노드가 사이클이 발생했는지 확인할 배열 | ||
for (int i = 0; i < m; i++) { | ||
int x = read(), y = read(); | ||
int rootX = find(x), rootY = find(y); | ||
// 부모가 같을 경우엔 사이클이 발생 | ||
if (rootX == rootY) | ||
isCycle[rootX] = true; | ||
parent[rootX] = rootY; | ||
isCycle[rootY] |= isCycle[rootX]; // 사이클 전파 | ||
} | ||
HashSet<Integer> roots = new HashSet<>(); | ||
for (int i = 1; i < n + 1; i++) { | ||
int root = find(i); | ||
if (!isCycle[root]) | ||
roots.add(root); | ||
} | ||
sb.append("Case ").append(++idx).append(": "); | ||
// 모두 사이클이 발생했을 경우 | ||
if (roots.size() == 0) | ||
sb.append("No trees.\n"); | ||
// 1개의 루트가 존재 | ||
else if (roots.size() == 1) | ||
sb.append("There is one tree.\n"); | ||
// n개의 루트가 존재 | ||
else | ||
sb.append("A forest of ").append(roots.size()).append(" trees.\n"); | ||
} | ||
System.out.println(sb); | ||
} | ||
|
||
// Find 함수 | ||
private static int find(int x) { | ||
if (x == parent[x]) | ||
return parent[x]; | ||
return parent[x] = find(parent[x]); | ||
} | ||
|
||
private static int read() throws Exception { | ||
int c, n = System.in.read() & 15; | ||
while ((c = System.in.read()) >= 48) | ||
n = (n << 3) + (n << 1) + (c & 15); | ||
if (c == 13) | ||
System.in.read(); | ||
return n; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
BOJ/5001-10000번/JW_9372.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
public class JW_9372 { | ||
|
||
public static void main(String[] args) throws Exception { | ||
int t = read(); | ||
StringBuilder sb = new StringBuilder(); | ||
while (t-- > 0) { | ||
int n = read(), m = read(); | ||
for (int i = 0; i < m; i++) { | ||
read(); | ||
read(); | ||
} | ||
sb.append(n - 1).append("\n"); // n - 1만 출력 | ||
} | ||
System.out.println(sb); | ||
} | ||
|
||
private static int read() throws Exception { | ||
int c, n = System.in.read() & 15; | ||
while ((c = System.in.read()) >= 48) | ||
n = (n << 3) + (n << 1) + (c & 15); | ||
if (c == 13) | ||
System.in.read(); | ||
return n; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.