Skip to content

[3주차] 배수빈 #31

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 8 commits into from
Sep 28, 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
55 changes: 55 additions & 0 deletions BOJ/10001-15000번/SB_13335.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;

public class SB_13335 {
static int N, W, L;
static int[] trucks;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
W = Integer.parseInt(st.nextToken()); // 다리 길이
L = Integer.parseInt(st.nextToken()); // 다리 최대하중

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

Queue<Truck> que = new ArrayDeque<>();
que.offer(new Truck(trucks[0], 1));

int weight = trucks[0];
int time = 1;
int idx = 1;
while (!que.isEmpty()) {
time++;

if (time - que.peek().inTime == W)
Copy link
Contributor

Choose a reason for hiding this comment

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

아하!,, 저는 트럭을 ArrayList에 담아서 시간이 갈 수록 각 트럭의 길이을 +1로 해준다음 트럭이 간 길이 == 다리 길이가 될 때 poll 하는 걸로 했는데,

time - 트럭이 터널에 들어간 시간 == W 을 조건으로 해주면 되는군요 ?!?! 😲
제 시간이 다른 분들 시간이 좀 더 걸린 이유가 있었네요,,,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

흑 저도 처음엔 트럭이 간길이==다리길이로 풀었는데 잘 안풀리더라구요...ㅠ 이방법 코드도 다시 한번 확인해봐야겠어요!!

weight -= que.poll().w;

if (idx < N && weight + trucks[idx] <= L && que.size() < W) { // 다리에 트럭추가 가능 시
que.offer(new Truck(trucks[idx], time));
weight += trucks[idx];
idx++;
}
}
System.out.println(time);

}
static class Truck{
int w;
int inTime;

Truck(int w, int inTime) {
this.w = w;
this.inTime = inTime;
}
}
}
43 changes: 43 additions & 0 deletions BOJ/15001-20000번/SB_15989.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

// 1, 2, 3 더하기 4

public class SB_15989 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());

int[] nums = new int[T];
int mx = -1;
for (int i = 0; i < T; i++) {
nums[i] = Integer.parseInt(br.readLine());
if (nums[i] > mx) mx = nums[i];
}

int[][] dp = new int[4][mx + 1];

Arrays.fill(dp[1], 1);
dp[1][0] = 0;

for (int i = 2; i <= 3; i++) {
for (int j = i; j <= mx; j++) {
if (i==j) {
dp[i][j] = 1;
continue;
}
for (int k = i; k > 0; k--) {
dp[i][j] += dp[k][j-i];
}
}
}

StringBuilder sb = new StringBuilder();
for (int n : nums) {
sb.append(dp[1][n] + dp[2][n] + dp[3][n]).append('\n');
}
System.out.println(sb);
}
}
81 changes: 81 additions & 0 deletions BOJ/15001-20000번/SB_18404.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
import java.util.StringTokenizer;

public class SB_18404 {
static int N, M;
static int[][] board;
static int[] caught;
static int[] dx = {-2, -2, -1, -1, 1, 1, 2, 2, 2};
static int[] dy = {-1, 1, -2, 2, -2, 2, -1, 1};

private static void bfs(int x, int y) {
Queue<Node> que = new ArrayDeque<>();
int[][] visited = new int[N+1][N+1];
for (int[] row : visited){
Arrays.fill(row, -1);
}
visited[x][y] = 0;
que.offer(new Node(x, y));

int cnt = 0;
while (!que.isEmpty()){
Node knit = que.poll();
if (board[knit.x][knit.y] > 0) {
caught[board[knit.x][knit.y]] = visited[knit.x][knit.y];
cnt++;
}
if (cnt==M) break;

for (int i = 0; i < 8; i++) {
int nx = knit.x + dx[i];
int ny = knit.y + dy[i];
if (!(0<nx && nx<=N && 0<ny && ny<=N)) continue;
if (visited[nx][ny]==-1){
que.offer(new Node(nx, ny));
visited[nx][ny] = visited[knit.x][knit.y] + 1;
}
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

board = new int[N+1][N+1];
caught = new int[M + 1];
st = new StringTokenizer(br.readLine());
int sx = Integer.parseInt(st.nextToken());
int sy = Integer.parseInt(st.nextToken());

int idx = 1;
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
board[x][y] = idx++;
}

bfs(sx, sy);
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= M; i++) {
sb.append(caught[i]).append(' ');
}
System.out.println(sb);
}

static class Node{
int x, y;

Node(int x, int y) {
this.x = x;
this.y = y;
}
}
}
113 changes: 113 additions & 0 deletions CodeTree/2017-2018년/SB_디버깅.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/*
* i번째에 i가 가도록. dfs로 완전탐색
* * 가지치기
* - 사다리 옆에 있으면 못놓기(양옆 체크)
* - 초기 올바르게 가는 행에는 놓지 말기
* * 구현
* - 사다리타기 함수
* - 사다리 놓기 함수
* */
public class SB_디버깅 {
static int N, M, H; // 열(사람), 선 갯수, 행(취약점, 가로)
static Node[][] board;
static int ans = -1;

private static boolean play() {
for (int c = 0; c < N; c++) { // 사람 한명씩 사다리 탐 (열)
int idx = c;
int r = 0; // 행
while (r<H){
if (board[r][c].isRight) c++;
else if (board[r][c].isLeft) c--;
r++;
}
if (idx!=c) return false;
}
return true;
}

private static void addLine(int depth) {
if (depth>3 || ans!=-1) return;

for (int i = 0; i < H; i++) {
for (int j = 0; j < N-1; j++) {
// 사다릴 못놓으면 패쓰
if (board[i][j].isLeft || board[i][j].isRight) continue; // 사다리 존재하면 놓을 수 없음
if (j>0 && board[i][j-1].isLeft) continue; // 이웃 사다리 여부 확인(사다리 연속X)

// 사다리 놓기
board[i][j].onRight();
board[i][j+1].onLeft();

// System.out.println("depth:"+depth +" i:"+ i +" j: "+j);
if (play()) {
ans = depth;
board[i][j].offRight();
board[i][j+1].offLeft();
return;
}
addLine(depth + 1); // 성공 못했으면 현재 상태에서 사다리 하나 더 추가해보기
board[i][j].offRight(); // 사다리 되돌리기
board[i][j+1].offLeft();
}
}
}
public static void main(String[] args) throws IOException {
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());

board = new Node[H][N];
for (int i = 0; i < H; i++) {
for (int j = 0; j < N; j++) {
board[i][j] = new Node(i, j);
}
}

for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
board[a-1][b-1].onRight();
board[a-1][b].onLeft();
}

if (M==0 || play()) ans = 0;
else addLine(1);

System.out.println(ans);
}

static class Node{
Copy link
Contributor

Choose a reason for hiding this comment

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

사다리 상태를 객체의 메소드로 관리하니 훨씬 깔끔하고 직관적으로 확인할 수 있는 것 같아요! 👍 다음에 참고해보고 싶어요~!

int i;
int j;
boolean isLeft = false;
boolean isRight = false;

Node(int i, int j) {
this.i = i;
this.j = j;
}

public void onLeft(){
this.isLeft = true;
}
public void onRight(){
this.isRight = true;
}
public void offLeft(){
this.isLeft = false;
}
public void offRight(){
this.isRight = false;
}
}
}
Loading