From bfd305ec7c80cf6cc54b792a9d8fa9ab1b4e3123 Mon Sep 17 00:00:00 2001 From: pmsu2007 Date: Tue, 10 Oct 2023 23:56:33 +0900 Subject: [PATCH 1/5] =?UTF-8?q?1931=20:=20=ED=9A=8C=EC=9D=98=EC=8B=A4=20?= =?UTF-8?q?=EB=B0=B0=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1931.java" | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 "01\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/1931.java" diff --git "a/01\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/1931.java" "b/01\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/1931.java" new file mode 100644 index 0000000..1c39c43 --- /dev/null +++ "b/01\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/1931.java" @@ -0,0 +1,67 @@ +import java.io.*; +import java.util.*; + +/*** + * 백준 1931번 + * 회의실 배정 + * 2023-10-09 + */ + +public class Main { + + static class Meeting implements Comparable { + private int start; + private int end; + + public Meeting(int start, int end) { + this.start = start; + this.end = end; + } + + @Override + public int compareTo(Meeting o) { + // 회의 끝 (오름차순 정렬) + if (this.end > o.end) { + return 1; + } else if(this.end < o.end) { + return - 1; + } else + // 회의 끝 시간 같을 시, 회의 시작 (오름차순 정렬) + return this.start - o.start; + } + } + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + int n = Integer.parseInt(br.readLine()); + + List meetings = new ArrayList<>(); + + StringTokenizer st = null; + for (int i = 0; i < n; i++) { + st = new StringTokenizer(br.readLine()); + int s = Integer.parseInt(st.nextToken()); + int e = Integer.parseInt(st.nextToken()); + meetings.add(new Meeting(s, e)); + } + + // 회의 끝을 오름차순으로 정렬 + Collections.sort(meetings); + + + int end = 0; + int answer = 0; + + // 정렬된 회의에서 가장 빨리 끝나는 회의와 겹치지 않는 것을 카운팅하고, 가장 빨리 끝나는 회의 갱신 + for (Meeting meeting : meetings) { + if (meeting.start >= end) { + end = meeting.end; + answer++; + } + } + + System.out.println(answer); + } +} From c436e9841833dc3df6a81d4e55ad076bea3a0466 Mon Sep 17 00:00:00 2001 From: pmsu2007 Date: Mon, 16 Oct 2023 22:46:34 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=EB=A9=94=EB=AA=A8=EB=A6=AC=20=EC=B4=88?= =?UTF-8?q?=EA=B3=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Main.java" | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 "02\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" diff --git "a/02\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" "b/02\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" new file mode 100644 index 0000000..ed9bcb9 --- /dev/null +++ "b/02\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" @@ -0,0 +1,70 @@ +package BFS.BOJ13549; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.LinkedList; +import java.util.Queue; +import java.util.StringTokenizer; + +public class Main { + + public static class Distance { + int x; + int time; + + public Distance(int x, int time) { + this.x = x; + this.time = time; + } + } + + static int N; + static int K; + static final int MAX = 100000; + static boolean[] visited = new boolean[100001]; + static Queue q = new LinkedList<>(); + + 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()); + K = Integer.parseInt(st.nextToken()); + + // 처음 위치 큐에 삽입 + q.add(new Distance(N, 0)); + visited[N] = true; + while(!q.isEmpty()) { + Distance now = q.poll(); + + if (now.x == K) { + System.out.println(now.time); + break; + } + + // 걷는 경우, 순간이동 하는 경우 계산 + for (int i = 0; i < 3; i++) { + Distance next = null; + + switch (i) { + case 0: + next = new Distance(now.x - 1, now.time + 1); + break; + case 1: + if (now.x + 1 <= MAX) + next = new Distance(now.x + 1, now.time + 1); + break; + case 2: + if (now.x * 2 <= MAX) + next = new Distance(now.x * 2, now.time); + break; + } + + if (next != null && !visited[next.x]) { + q.add(next); + } + } + } + } +} From 6c4ebbe65b5f3349d4408162096e85d903388b96 Mon Sep 17 00:00:00 2001 From: pmsu2007 Date: Mon, 16 Oct 2023 23:07:59 +0900 Subject: [PATCH 3/5] =?UTF-8?q?13549=20:=20=EC=88=A8=EB=B0=94=EA=BC=AD?= =?UTF-8?q?=EC=A7=88=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Main.java" | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git "a/02\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" "b/02\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" index ed9bcb9..130decc 100644 --- "a/02\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" +++ "b/02\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" @@ -3,6 +3,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; @@ -21,9 +22,10 @@ public Distance(int x, int time) { static int N; static int K; - static final int MAX = 100000; - static boolean[] visited = new boolean[100001]; - static Queue q = new LinkedList<>(); + static final int DISTANCE_MAX = 100000; + static int[] counter = new int[100001]; + + static Queue q = new LinkedList<>(); public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); @@ -31,39 +33,42 @@ public static void main(String[] args) throws IOException { N = Integer.parseInt(st.nextToken()); K = Integer.parseInt(st.nextToken()); + Arrays.fill(counter, Integer.MAX_VALUE); // 처음 위치 큐에 삽입 - q.add(new Distance(N, 0)); - visited[N] = true; + q.add(N); + counter[N] = 0; while(!q.isEmpty()) { - Distance now = q.poll(); + int now = q.poll(); - if (now.x == K) { - System.out.println(now.time); + if (now == K) { + System.out.println(counter[K]); break; } // 걷는 경우, 순간이동 하는 경우 계산 for (int i = 0; i < 3; i++) { - Distance next = null; switch (i) { case 0: - next = new Distance(now.x - 1, now.time + 1); + if (now - 1 >= 0 && counter[now - 1] > counter[now] + 1) { + q.add(now - 1); + counter[now - 1] = counter[now] + 1; + } break; case 1: - if (now.x + 1 <= MAX) - next = new Distance(now.x + 1, now.time + 1); + if (now + 1 <= DISTANCE_MAX && counter[now + 1] > counter[now] + 1) { + q.add(now + 1); + counter[now + 1] = counter[now] + 1; + } break; case 2: - if (now.x * 2 <= MAX) - next = new Distance(now.x * 2, now.time); + if (now * 2 <= DISTANCE_MAX && counter[now * 2] > counter[now]) { + q.add(now * 2); + counter[now * 2] = counter[now]; + } break; } - - if (next != null && !visited[next.x]) { - q.add(next); - } } } } From afb74b44cc1c635ab251d3ebf3e15965f95f4d81 Mon Sep 17 00:00:00 2001 From: pmsu2007 Date: Fri, 20 Oct 2023 14:37:39 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=EC=A3=BC=EC=84=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\353\260\225\353\257\274\354\210\230/Main.java" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/02\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" "b/02\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" index 130decc..8ab5ded 100644 --- "a/02\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" +++ "b/02\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" @@ -20,10 +20,10 @@ public Distance(int x, int time) { } } - static int N; - static int K; + static int N; // 수빈이의 위치 + static int K; // 동생의 위치 static final int DISTANCE_MAX = 100000; - static int[] counter = new int[100001]; + static int[] counter = new int[100001]; // INDEX : 현재위치, VALUE : 최소이동횟수 static Queue q = new LinkedList<>(); From 01b1ad3fe84a4d346ad4209631d2e9eca40754b6 Mon Sep 17 00:00:00 2001 From: pmsu2007 Date: Wed, 1 Nov 2023 00:56:17 +0900 Subject: [PATCH 5/5] 9663 : N-Queen --- .../Main.java" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "03\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" diff --git "a/03\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" "b/03\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" new file mode 100644 index 0000000..2a23f47 --- /dev/null +++ "b/03\354\243\274\354\260\250/\353\260\225\353\257\274\354\210\230/Main.java" @@ -0,0 +1,87 @@ +package BackTracking.BOJ9663; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +/*** + * 백준 9663번 + * N-Queen + * 2023-10-27 + * + * Back Tracking + */ + +public class Main { + + static int N; // 체스판 크기, 퀸의 개수, 1 <= N < 15 + static int[][] map; + static int answer = 0; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + map = new int[N + 1][N + 1]; + + for (int i = 1; i <= N; i++) { + placedQueen(1, i); + } + + System.out.println(answer); + } + + public static void placedQueen(int row, int col) { + // 해당 위치에 퀸을 놓았을 때 막히는 부분 체크 + // 새롭게 알게된 사실 : 열의 차와 행의 차가 같을 경우 대각선에 놓여있는 경우임 + mark(row, col); + + // 종료 조건 + if (row == N) { + answer++; + // 백트래킹 + unmark(row, col); + return; + } + + for (int i = 1; i <= N; i++) { + // 다음 단계 조건 + if (map[row + 1][i] == 0) { + placedQueen(row + 1, i); + } + } + + // 백트래킹 + unmark(row, col); + } + + public static void mark(int cx, int cy) { + int times = 1; + map[cx][cy] = cx; + // 남아있는 행 만큼 + for(int nx = cx + 1; nx <= N; nx++) { + for(int dy = -1; dy <= 1; dy++) { + int ny = cy + dy * times; + if (1 <= nx && nx <= N && 1 <= ny && ny <= N && map[nx][ny] == 0) { + map[nx][ny] = cx; + } + } + times++; + } + } + + public static void unmark(int cx, int cy) { + + int times = 1; + map[cx][cy] = 0; + // 남아있는 행 만큼 + for(int nx = cx + 1; nx <= N; nx++) { + for(int dy = -1; dy <= 1; dy++) { + int ny = cy + dy * times; + if (1 <= nx && nx <= N && 1 <= ny && ny <= N && map[nx][ny] == cx) { + map[nx][ny] = 0; + } + } + times++; + } + } +}