Skip to content
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

[02주차 박민수] 숨바꼭질 3 #12

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
67 changes: 67 additions & 0 deletions 01주차/박민수/1931.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.io.*;
import java.util.*;

/***
* 백준 1931번
* 회의실 배정
* 2023-10-09
*/

public class Main {

static class Meeting implements Comparable<Meeting> {
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<Meeting> 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);
}
}
75 changes: 75 additions & 0 deletions 02주차/박민수/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package BFS.BOJ13549;

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;

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 DISTANCE_MAX = 100000;
static int[] counter = new int[100001]; // INDEX : 현재위치, VALUE : 최소이동횟수

static Queue<Integer> 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());
Arrays.fill(counter, Integer.MAX_VALUE);

// 처음 위치 큐에 삽입
q.add(N);
counter[N] = 0;
while(!q.isEmpty()) {
int now = q.poll();

if (now == K) {
System.out.println(counter[K]);
break;
}

// 걷는 경우, 순간이동 하는 경우 계산
for (int i = 0; i < 3; i++) {

switch (i) {
case 0:
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 + 1 <= DISTANCE_MAX && counter[now + 1] > counter[now] + 1) {
q.add(now + 1);
counter[now + 1] = counter[now] + 1;
}
break;
case 2:
if (now * 2 <= DISTANCE_MAX && counter[now * 2] > counter[now]) {
q.add(now * 2);
counter[now * 2] = counter[now];
}
break;
}
}
}
}
}
87 changes: 87 additions & 0 deletions 03주차/박민수/Main.java
Original file line number Diff line number Diff line change
@@ -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++;
}
}
}