Skip to content

[3주차] 고다혜 #33

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 10 commits into from
Sep 28, 2024
54 changes: 54 additions & 0 deletions BOJ/10001-15000번/DH_13335.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.*;
import java.io.*;

/*
트럭
*/

public class DH_13335 {
static class Truck {
int w, d;
public Truck(int w, int d) {
this.w = w;
this.d = d;
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

int n = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
int L = Integer.parseInt(st.nextToken());

Deque<Integer> q = new ArrayDeque<>();
LinkedList<Truck> tunnel = new LinkedList<>();

st = new StringTokenizer(br.readLine());
for(int i = 0; i < n; i++) q.add(Integer.parseInt(st.nextToken()));

int tunnelWeight = 0;
int time = 0;

while(true) {
time++;

// 다리 끝에 도달한 트럭이 있는지 확인
if(!tunnel.isEmpty() && tunnel.get(0).d == w) {
tunnelWeight -= tunnel.get(0).w;
tunnel.remove(0);
}

// 다리에 들어올 수 있는 트럭이 있는지 확인
if(!q.isEmpty() && tunnelWeight + q.peek() <= L) {
tunnelWeight += q.peek();
tunnel.add(new Truck(q.poll(), 0));
}

// 다리에 있는 트럭의 위치 한 칸씩 옮겨주기
for(Truck t: tunnel) t.d += 1;
if(tunnel.isEmpty()) break;
}
System.out.println(time);
}
}
88 changes: 88 additions & 0 deletions BOJ/10001-15000번/DH_18404.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import java.io.*;
import java.util.*;

/*
현명한 나이트
*/

public class DH_18404 {
static class Point {
int r, c, d;

public Point(int r, int c, int d) {
this.r = r;
this.c = c;
this.d = d;
}
}
static Deque<Point> q = new ArrayDeque<>();
static int N, M;
static int[][] map; // 방문: -1, 말이 있는 곳: 1 ~ N
static int[] dr = {-1, -2, -2, -1, 1, 2, 2, 1};
static int[] dc = {-2, -1, 1, 2, 2, 1, -1, -2};
static int[] result;

static void solution() {
int cnt = 0; // 말의 개수 세는 변수

L: while(!q.isEmpty()) {
Point current = q.poll();

for(int d = 0; d < dr.length; d++) {
int nr = current.r + dr[d];
int nc = current.c + dc[d];

// 나이트가 갈 수 없는 곳이면서
// 이미 간 곳이라면 continue
if(!check(nr, nc) || map[nr][nc] == -1) continue;
if(map[nr][nc] > 0) { // 말이 있는 곳
cnt++;
result[map[nr][nc] - 1] = current.d + 1; // 몇 번째에 도달했는지 저장
if(cnt == M) break L; // 말이 있는 모든 곳을 방문했다면 while문 빠져나오기
}
q.add(new Point(nr, nc, current.d + 1));
map[nr][nc] = -1; // 방문처리
}
}

StringBuilder sb = new StringBuilder();
for(int i: result) sb.append(i).append(" ");
System.out.println(sb);
}

static boolean check(int r, int c) {
return r >= 0 && r < N && c >= 0 && c < N;
}

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

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

map = new int[N][N];
result = new int[M];

st = new StringTokenizer(br.readLine());

// 나이트의 위치 입력
int r = Integer.parseInt(st.nextToken()) - 1;
int c = Integer.parseInt(st.nextToken()) - 1;
q.add(new Point(r, c, 0));
map[r][c] = -1;

// 말의 위치 입력
// 말의 위치에 몇 번째 말이 저장되어 있는지 저장
int idx = 1;
for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken()) - 1;
c = Integer.parseInt(st.nextToken()) - 1;

map[r][c] = idx++;
}

solution();
}
}
26 changes: 26 additions & 0 deletions BOJ/15001-20000번/DH_15989.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.io.*;

/*
1, 2, 3 더하기 4
*/

public class DH_15989 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int[] dp = new int[10_001];
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

@KodaHye KodaHye Sep 24, 2024

Choose a reason for hiding this comment

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

숫자 사이에 _를 쓸 수 있는데요, 자리수를 쉽게 파악하기 위한 용도로 사용합니다!

예를 들어 1,000,000에서 ,를 _로 나타내준거랑 같아요!


dp[1] = 1; dp[2] = 2; dp[3] = 3;
for(int i = 4; i < dp.length; i++) dp[i] = dp[i - 3] + i / 2 + 1;
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.

오롯이 제 머릿속에서 나온건 아닙니다 ㅎㅎ,,

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

Choose a reason for hiding this comment

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

우와 한줄로 끝날 수 있군요,,??ㅎㅎㅎㅎ 점화식 세우는거 너무 어려워요 ㅜㅜㅜ


int T = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();

for(int i = 0; i < T; i++) {
int num = Integer.parseInt(br.readLine());
sb.append(dp[num]).append("\n");
}

System.out.println(sb);
}
}
69 changes: 69 additions & 0 deletions CodeTree/2017-2018년/DH_디버깅.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import java.io.*;
import java.util.*;

/*
디버깅
(1) powerset을 이용해서 사다리를 놓을 수 있는 곳, 놓을 수 없는 곳 모두 고려
- 주의 사항: 처음 사다리가 놓여져 있는 곳은 사다리를 없앨 수 없음
- 사다리의 개수가 3개 이상이라면 return;
(2) 조합을 이용해서 0, 1, 2, 3개 고를 수 있는 경우 구하고 가능한지 확인
*/

public class DH_디버깅 {
static boolean[][] line;
static int N, M, H;

static void solution() {
for(int i = 0; i < 4; i++) func(0, 0, i, line);
System.out.println(-1);
}

static void func(int depth, int idx, int cnt, boolean[][] check) {
Copy link
Contributor

Choose a reason for hiding this comment

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

재귀함수 반환 타입을 boolean으로 해서 값을 찾으면 true를 전달해서 재귀를 종료시키는 방법도 좋을 것 같아요!

if(depth == cnt) {
for(int c = 0; c < N; c++) {
int currentC = c;
for(boolean[] booleans : line) {
if (currentC < N - 1 && booleans[currentC]) currentC++;
else if (currentC > 0 && booleans[currentC - 1]) currentC--;
}
if(c != currentC) return;
}

System.out.println(cnt);
System.exit(0);
return;
}

for(int i = idx; i < check.length * check[0].length; i++) {
int r = i / line[0].length, c = i % line[0].length;
if(check(i) || (c > 0 && check[r][c - 1])) continue;
check[r][c] = true;
func(depth + 1, i + 1, cnt, check);
check[r][c] = false;
}
}

static boolean check(int idx) {
int len = line[0].length;
int r = idx / len, c = idx % len;
return line[r][c];
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken()); // 고객의 수
M = Integer.parseInt(st.nextToken()); // 메모리 유실 선의 개수
H = Integer.parseInt(st.nextToken()); // 취약 점의 개수

line = new boolean[H][N - 1];
for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken()) - 1;
int b = Integer.parseInt(st.nextToken()) - 1;

line[a][b] = true;
}
solution();
}
}
119 changes: 119 additions & 0 deletions CodeTree/2021-2022년/DH_나무_타이쿤.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import java.io.*;
import java.util.*;

public class DH_나무_타이쿤 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int N, M;
static int[][] map; // 나무의 높이 저장
static boolean[][] check; // 특수 영양제가 있는 곳을 저장하는 변수
static int[] dr = {0, 0, -1, -1, -1, 0, 1, 1, 1};
static int[] dc = {0, 1, 1, 0, -1, -1, -1, 0, 1};

static void solution() throws Exception {

for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());

int d = Integer.parseInt(st.nextToken());
int p = Integer.parseInt(st.nextToken());

move(d, p); // 특수 영양제 이동
grow(); // 대각선 확인 후 영양제 투입
cutAndPut(); // 높이가 2 이상인 리브로수 베고, 특수영양제 올리기
}

System.out.println(getTotalHeight());
}

static int getTotalHeight() {
int result = 0;
for(int r = 0; r < N; r++) {
for(int c = 0; c < N; c++) {
result += map[r][c];
}
}
return result;
}
static void cutAndPut() {
// 새롭게 특수 영양제를 놓을 곳을 저장하는 변수
boolean[][] tmp = new boolean[N][N];

for(int r = 0; r < N; r++) {
for(int c = 0; c < N; c++) {
// 이미 영양제가 있었던 곳이거나, 높이가 2미만이면 continue;
if(check[r][c] || map[r][c] < 2) continue;
map[r][c] -= 2;
tmp[r][c] = true;
}
}

// check 배열 update
check = tmp;
}

static void grow() {
for(int r = 0; r < N; r++) {
for(int c = 0; c < N; c++) {
if(!check[r][c]) continue;
int cnt = 0;
for(int d = 2; d < dr.length; d += 2) {

int nr = r + dr[d];
int nc = c + dc[d];

if(!check(nr, nc) || map[nr][nc] == 0) continue;
cnt++;
}

map[r][c] += cnt;
}
}
}

static boolean check(int r, int c) {
return r >= 0 && r < N && c >= 0 && c < N;
}
static void move(int d, int p) {
boolean[][] tmp = new boolean[N][N];
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.

ㅋㅋㅋ ㅋㅋㅋㅋ 좋습니다!!! 저도 큐를 이용해서 푸는 방식 구경했습니다 !!!!! ㅎㅎ


for(int r = 0; r < N; r++) {
for(int c = 0; c < N; c++) {
if(!check[r][c]) continue;
check[r][c] = false;

int nr = (r + dr[d] * p) % N;
int nc = (c + dc[d] * p) % N;

nr = nr < 0 ? nr + N: nr;
nc = nc < 0 ? nc + N :nc;

map[nr][nc] += 1; // 이동 후 땅에 영양제 투입
tmp[nr][nc] = true;
}
}

check = tmp;
}
public static void main(String[] args) throws Exception {
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

map = new int[N][N];
check = new boolean[N][N];

for(int r = N - 2; r < N; r++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

오 초기 영양제 투입도 for문으로 깔끔하게 구현했군요!

for(int c = 0; c < 2; c++) check[r][c] = true;
}

for(int r = 0; r < N; r++) {
st = new StringTokenizer(br.readLine());
for(int c = 0; c < N; c++) {
map[r][c] = Integer.parseInt(st.nextToken());
}
}

solution();
}
}
Loading