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

[15주차] 배수빈 #207

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
jewan100 merged 9 commits into main from xubin
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions BOJ/1000-5000번/SB_1030.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class SB_1030 {
static int s, N, K, r1, r2, c1, c2;
static char[][] ans;

private static void recursive(int x, int y, int t, int color) { // 현재 재귀에서 행, 열, 시간, 색
int p = (int) Math.pow(N, s - t); // 현재 시간에서 평면 크기 (현재 단계에서 한 격자가 차지하는 실제 크기)
if (!isValid(x*p, x*p+p-1, y*p, y*p+p-1)) return;

if (t == s) { // 종료조건에서 색 기록
ans[x - r1][y - c1] = (char) (color + '0'); // x,y좌표를 출력범위에 맞게 조정 후 값 넣기
return;
}

for (int i = 0; i < N; i++) { // 현재 격자 N*N으로 분열하기 (i,j는 나뉜 하위 격자내 상대좌표)
for (int j = 0; j < N; j++) {
int nColor;
if (color==1) nColor = 1;
else nColor = (isBlackArea(i) && isBlackArea(j)) ? 1 : 0;
recursive(x * N + i, y * N + j, t + 1, nColor); // 하위 좌표값을 전체 큰 사각형에서 본 절대좌표 값으로 변환해 보냄
}
}
}

private static boolean isBlackArea(int i) { // 현재 격자가 검정인지 판단 (i: 현재 행의 인덱스, ((N - K) / 2): 중앙의 검정 구역 시작점)
return !((i < (N - K) / 2) || (((N - 1 - i) < (N - K) / 2))); // 행(i)을 기준으로 해당 행i가 검정구역 위, 아래 존재하는지 판단
}

private static boolean isValid(int tr1, int tr2, int tc1, int tc2) { // 탐색범위가 출력범위에 포함되는지 확인
if (tr1 > r2 || tr2 < r1) return false;
if (tc1 > c2 || tc2 < c1) return false;
return true;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();

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());

int row = r2 - r1 + 1;
int col = c2 - c1 + 1;
ans = new char[row][col];

// 가장 큰 사각형에서 재귀로 분할해 작은 사각형으로 처리하기
recursive(0, 0, 0, 0);

for (char[] c : ans) {
sb.append(c).append('\n');
}
System.out.println(sb);
}
}
34 changes: 34 additions & 0 deletions BOJ/1000-5000번/SB_1725.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;

// pop한 값의 높이를 최소로하는 사각형 만들기
public class SB_1725 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int N = Integer.parseInt(br.readLine());
int[] arr = new int[N + 2]; // 인덱스 1부터 시작, 마지막 막대 0추가

for (int i = 1; i <= N; i++) {
arr[i] = Integer.parseInt(br.readLine());
}

Deque<Integer> stack = new ArrayDeque<>();
stack.push(0); // 0값 먼저 넣어주기

long mx = 0;
for (int i = 1; i <= N + 1; i++) {
while (!stack.isEmpty() && arr[stack.peek()] > arr[i]) { // push할 막대가 스택의 값보다 작다면, pop하면서 사각형 넓이 계산 (스택은 항상 오름차순으로 유지)
int idx = stack.pop();
int h = arr[idx]; // 높이
int w = stack.isEmpty() ? i - 1 : i - stack.peek() - 1; // 넓이
mx = Math.max(mx, (long) h *w);
}
stack.push(i);
}
System.out.println(mx);
}
}
47 changes: 47 additions & 0 deletions BOJ/1000-5000번/SB_1992.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SB_1992 {
static int N;
static int[][] board;

private static boolean check(int n, int sx, int sy) {
int num = board[sx][sy];
for (int i = sx; i < sx+n; i++) {
for (int j = sy; j < sy+n; j++) {
if ((num!=board[i][j])) return false;
}
}
return true;
}

private static StringBuilder dfs(int n, int sx, int sy, StringBuilder sb) {
// 해당 영역이 모두 같은 숫자면 바로 리턴
if (check(n, sx, sy)) return sb.append(board[sx][sy]);

// 아니면 분할
sb.append('(');
int half = n >> 1;
dfs(half, sx, sy, sb);
dfs(half, sx, sy + half, sb);
dfs(half, sx + half, sy, sb);
dfs(half, sx + half, sy + half, sb);

return sb.append(')');
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());

board = new int[N][N];
for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < N; j++) {
board[i][j] = line.charAt(j) - '0';
}
}

System.out.println(dfs(N, 0, 0, new StringBuilder()));
}
}
76 changes: 76 additions & 0 deletions BOJ/1000-5000번/SB_4803.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class SB_4803 {
static int[] parent;
static boolean[] isCycle;
static String cnt0 = "No trees.";
static String cnt1 = "There is one tree.";

private static int find(int x) {
if (parent[x] != x) return find(parent[x]);
return parent[x];
}

private static void union(int u, int v) {
int pu = find(u);
int pv = find(v);

if (pu==pv) { // 부모가 같으면 사이클이 존재
isCycle[pu] = true;
return;
}

parent[pv] = pu; // 부모다르면 합치기
isCycle[pu] = isCycle[pu] || isCycle[pv]; // 사이클 여부도 합치기
}

private static int cntTree(int N) {
int cnt = 0;
for (int i = 1; i < N + 1; i++) {
if (parent[i]==i && !isCycle[i]) cnt++; // 해당 그룹이 사이클 가지지 않으면 트리(루트 노드 기준)
}
return cnt;
}
private static void init(int N) {
parent = new int[N + 1];
isCycle = new boolean[N + 1];

for (int i = 1; i < N + 1; i++) {
parent[i] = i;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();

int idx = 1;
while (true) {
st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());

if (N==0 && M==0) break;

init(N);
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
union(u, v);
}
int cnt = cntTree(N);

sb.append("Case ").append(idx).append(": ");
if (cnt==0) sb.append(cnt0);
else if (cnt==1) sb.append(cnt1);
else sb.append("A forest of ").append(cnt).append(" trees.");
sb.append('\n');
idx++;
}
System.out.println(sb);
}
}
2 changes: 0 additions & 2 deletions BOJ/20001-25000번/SB_22944.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package bfs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Expand Down
27 changes: 27 additions & 0 deletions BOJ/5001-10000번/SB_9372.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class SB_9372 {
static int N, M;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();

int T = Integer.parseInt(br.readLine());

while (T-- > 0) {
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
}
sb.append(N - 1).append('\n');
}
System.out.println(sb);
}
}
101 changes: 101 additions & 0 deletions CodeTree/2019-2020년/SB_윷놀이_사기단.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class SB_윷놀이_사기단 {
static int[] arr = new int[10];
static HashMap<Integer, int[]> moveIdx = new HashMap<>();
static HashMap<Integer, Integer> score = new HashMap<>();

private static void init() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

하드코딩이 최고였습니다💪🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ᄏᄐᄏᄏ튜ᅲᅲ하드코딩 왕 오랜만이였어요..😂

// 보드 초기화
moveIdx.put(0, new int[]{1, 2, 3, 4, 5}); // 시작
moveIdx.put(1, new int[]{2, 3, 4, 5, 6});
moveIdx.put(2, new int[]{3, 4, 5, 6, 7});
moveIdx.put(3, new int[]{4, 5, 6, 7, 8});
moveIdx.put(4, new int[]{5, 6, 7, 8, 9});
moveIdx.put(5, new int[]{21, 22, 23, 24, 25}); // 첫번째 파란 지점
moveIdx.put(6, new int[]{7, 8, 9, 10, 11});
moveIdx.put(7, new int[]{8, 9, 10, 11, 12});
moveIdx.put(8, new int[]{9, 10, 11, 12, 13});
moveIdx.put(9, new int[]{10, 11, 12, 13, 14});
moveIdx.put(10, new int[]{27, 28, 24, 25, 26}); // 두번째 파란 지점
moveIdx.put(11, new int[]{12, 13, 14, 15, 16});
moveIdx.put(12, new int[]{13, 14, 15, 16, 17});
moveIdx.put(13, new int[]{14, 15, 16, 17, 18});
moveIdx.put(14, new int[]{15, 16, 17, 18, 19});
moveIdx.put(15, new int[]{29, 30, 31, 24, 25}); // 세번째 파란 지점
moveIdx.put(16, new int[]{17, 18, 19, 20, -1});
moveIdx.put(17, new int[]{18, 19, 20, -1, -1});
moveIdx.put(18, new int[]{19, 20, -1, -1, -1});
moveIdx.put(19, new int[]{20, -1, -1, -1, -1});
moveIdx.put(20, new int[]{-1, -1, -1, -1, -1}); // 점수 40에 해당하는 도착 바로전
moveIdx.put(21, new int[]{22, 23, 24, 25, 26}); // 첫번째 파란 지점에서 빨간 화살표 이동(13)
moveIdx.put(22, new int[]{23, 24, 25, 26, 20});
moveIdx.put(23, new int[]{24, 25, 26, 20, -1});
moveIdx.put(24, new int[]{25, 26, 20, -1, -1});
moveIdx.put(25, new int[]{26, 20, -1, -1, -1});
moveIdx.put(26, new int[]{20, -1, -1, -1, -1});
moveIdx.put(27, new int[]{28, 24, 25, 26, 20}); // 두번째 파란 지점에서 빨간 화살표 이동(22)
moveIdx.put(28, new int[]{24, 25, 26, 20, -1});
moveIdx.put(29, new int[]{30, 31, 24, 25, 26});
moveIdx.put(30, new int[]{31, 24, 25, 26, 20});
moveIdx.put(31, new int[]{24, 25, 26, 20, -1});
Comment on lines +12 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 진짜에요 ? ᅲᅲ

흐엉,,,, 현실 부정하고 있었는데,,, 빡구현 하러 가야겠네요,,,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ᄏᄐᄏᄐᄏᄐ헝헝 전 프렉탈평면 부정하고 있었는데,,, 우리 모두 화이팅,,

jewan100 reacted with thumbs up emoji

// 점수 초기화
score.put(-1, 0);
for (int i = 0; i < 21; i++) {
score.put(i, i * 2);
}
score.put(21, 13);
score.put(22, 16);
score.put(23, 19);
score.put(24, 25);
score.put(25, 30);
score.put(26, 35);
score.put(27, 22);
score.put(28, 24);
score.put(29, 28);
score.put(30, 27);
score.put(31, 26);
}

static int ans = 0;

private static void dfs(int hidx, int cnt, int total, List<Integer> pos) { // 움직일 말 번호, 주사위 던진 횟수, 현재까지 점수, 말들의 위치
List<Integer> tmpPos = new ArrayList<>(pos);
int curDice = arr[cnt] - 1; // 현재 주사위 값
tmpPos.set(hidx, moveIdx.get(tmpPos.get(hidx))[curDice]); // 선택한 말 이동 (현재 말의 위치에서 주사위 값만큼 이동)
int curScore = score.get(tmpPos.get(hidx)); // 말 이동 후 점수

if (cnt == 9) { // 주사위 10번 모두 던지면 최종 점수 계산 후 리턴
ans = Math.max(ans, total + curScore);
return;
}

int nxtDice = arr[cnt + 1] - 1; // 다음 주사위 값
for (int i = 0; i < 4; i++) { // 4개말 중 선택
if (tmpPos.get(i)==-1) continue; // 이미 도착한 말은 패쓰
int nxtPos = moveIdx.get(tmpPos.get(i))[nxtDice]; // 선택한 말의 다음 위치
if (nxtPos !=-1 && tmpPos.contains(nxtPos)) continue; // 다음 위치에 다른 말이 이미 있으면 패쓰
dfs(i, cnt + 1, total + curScore, tmpPos); // 선택한 말로 dfs 수행
}

}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

for (int i = 0; i < 10; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}

init();

List<Integer> pos = new ArrayList<>(Arrays.asList(0, 0, 0, 0));
dfs(0, 0, 0, pos);

System.out.println(ans);
}
}
Loading

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