Skip to content

[유병규-16주차 알고리즘 스터디] #75

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

Open
wants to merge 4 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
65 changes: 65 additions & 0 deletions 유병규_16주차/[BOJ-13905] 세부.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import java.io.*;
import java.util.*;

public class Main {

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

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

int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());

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

int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());

List<Edge>[] graph = new ArrayList[n+1];
for(int i=1; i<=n; i++) {
graph[i] = new ArrayList<>();
}

for(int i=0; i<m; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
graph[a].add(new Edge(b, c));
graph[b].add(new Edge(a, c));
}

boolean[] visited = new boolean[n+1];
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> -Integer.compare(o1[1], o2[1]));
pq.offer(new int[] {s, Integer.MAX_VALUE});

while(!pq.isEmpty()) {
int[] info = pq.poll();
int current = info[0];

if(current == e) {
System.out.println(info[1]);
return;
}

if(visited[current]) continue;
visited[current] = true;
for(Edge edge : graph[current]) {
if(visited[edge.to]) continue;
pq.offer(new int[] {edge.to, Math.min(info[1], edge.weight)});
}
}
System.out.println(0);
}

private static class Edge{
int to;
int weight;

public Edge(int to, int weight) {
this.to = to;
this.weight = weight;
}
}
}
137 changes: 137 additions & 0 deletions 유병규_16주차/[BOJ-15653] 구슬 탈출 4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import java.io.*;
import java.util.*;

public class Main {
private static int n,m;
private static int[] red,blue;
private static char[][] map;
private static int[][] d = {{-1,0},{1,0},{0,-1},{0,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());

map = new char[n][m];
for(int i=0; i<n; i++) {
String line = br.readLine();
for(int j=0; j<m; j++) {
map[i][j] = line.charAt(j);
if(map[i][j] == 'R') {
red = new int[] {i,j};
map[i][j] = '.';
}
if(map[i][j] == 'B') {
blue = new int[] {i,j};
map[i][j] = '.';
}
}
}

int count = bfs();
System.out.println(count);
}

private static int bfs() {
Queue<int[]> q = new LinkedList<>();
boolean[][][][] visited = new boolean[n][m][n][m];

q.offer(new int[] {red[0], red[1], blue[0], blue[1], 0, 0, 4});
visited[red[0]][red[1]][blue[0]][blue[1]] = true;

while(!q.isEmpty()) {
int[] info = q.poll();
int rx = info[0];
int ry = info[1];
int bx = info[2];
int by = info[3];
int count = info[4];
int dStart = info[5];
int dCount = info[6];

int dx = (bx-rx == 0) ? 0 : (bx-rx)/Math.abs(bx-rx);
int dy = (by-ry == 0) ? 0 : (by-ry)/Math.abs(by-ry);
To: for(int i=0; i<dCount; i++) {
int idx = dStart+i;

int nbx = bx + d[idx][0];
int nby = by + d[idx][1];
int nrx = rx + d[idx][0];
int nry = ry + d[idx][1];

if(OOB(nbx, nby) || OOB(nrx, nry)) continue;

//블루 먼저
if(dx == d[idx][0] && dy == d[idx][1]) {
while(true) {
if(map[nbx][nby] == 'O') continue To;
if(map[nbx][nby] == '#') break;
nbx += d[idx][0];
nby += d[idx][1];
}

nbx -= d[idx][0];
nby -= d[idx][1];

while(true) {
if(map[nrx][nry] == 'O') break;
if(map[nrx][nry] == '#' || (nbx == nrx && nby == nry)) break;
nrx += d[idx][0];
nry += d[idx][1];
}

if(map[nrx][nry] == 'O') return count+1;

nrx -= d[idx][0];
nry -= d[idx][1];

if(visited[nrx][nry][nbx][nby]) continue To;
int ndStart = idx < 2 ? 2 : 0;
q.offer(new int[] {nrx, nry, nbx, nby, count+1, ndStart, 2});
visited[nrx][nry][nbx][nby] = true;
}
//레드 먼저
else {
while(true) {
if(map[nrx][nry] == 'O') break;
if(map[nrx][nry] == '#') break;
nrx += d[idx][0];
nry += d[idx][1];
}

if(map[nrx][nry] != 'O') {
nrx -= d[idx][0];
nry -= d[idx][1];
}


while(true) {
if(map[nbx][nby] == 'O') continue To;
if(map[nbx][nby] == '#' || (nbx == nrx && nby == nry)) break;
nbx += d[idx][0];
nby += d[idx][1];
}

nbx -= d[idx][0];
nby -= d[idx][1];

if(map[nrx][nry] == 'O') return count+1;

if(visited[nrx][nry][nbx][nby]) continue To;
int ndStart = idx < 2 ? 2 : 0;
q.offer(new int[] {nrx, nry, nbx, nby, count+1, ndStart, 2});
visited[nrx][nry][nbx][nby] = true;
}
}
}

return -1;
}

private static boolean OOB(int x, int y) {
if(x<0 || x>=n || y<0 || y>=m) return true;
return false;
}
}
71 changes: 71 additions & 0 deletions 유병규_16주차/[BOJ-1727] 커플 만들기.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.io.*;
import java.util.*;

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

int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());

int[] men = new int[n + 1];
int[] women = new int[m + 1];

st = new StringTokenizer(br.readLine());
for (int i=1; i<=n; i++) {
men[i] = Integer.parseInt(st.nextToken());
}

st = new StringTokenizer(br.readLine());
for (int i=1; i<=m; i++) {
women[i] = Integer.parseInt(st.nextToken());
}

// 정렬
Arrays.sort(men);
Arrays.sort(women);

// 작은 그룹과 큰 그룹 결정
int[] small, large;
int smallSize, largeSize;

if (n <= m) {
small = men;
large = women;
smallSize = n;
largeSize = m;
} else {
small = women;
large = men;
smallSize = m;
largeSize = n;
}

// DP 배열 초기화
long[][] dp = new long[smallSize+1][largeSize+1];

// 초기값 설정
for (int i=0; i<=smallSize; i++) {
for (int j=0; j<i; j++) {
dp[i][j] = Long.MAX_VALUE;
}
}

// DP 계산
for (int i=1; i<=smallSize; i++) {
for (int j=i; j<=largeSize; j++) {
if (j == i) {
dp[i][j] = dp[i-1][j-1] + Math.abs(small[i] - large[j]);
} else {
dp[i][j] = Math.min(
dp[i][j-1], // j번째 사람을 선택하지 않는 경우
dp[i-1][j-1] + Math.abs(small[i] - large[j]) // j번째 사람을 선택하는 경우
);
}
}
}
Comment on lines +55 to +67
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 j번째 사람을 선택하는 경우와 선택하지 않는 경우를 따로 처리하려고 dp 배열 두 개를 관리했는데 그럴 필요가 없었군요... 좋은 코드네요 👍


System.out.println(dp[smallSize][largeSize]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.io.*;
import java.util.*;

public class Main {
private static int n, k;
private static int[] num;

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

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

if(k == 1) {
System.out.println(sum);
return;
}

int left = 0;
int right = sum;
int mid = 0;
while(left <= right) {
mid = left + (right-left)/2;

int count = counting(mid);

if(count < k) {
right = mid-1;
continue;
}

left = mid+1;
}

System.out.println(right);
}

private static int counting(int min) {
int count = 0;

int sum = 0;
for(int i=0; i<n; i++) {
sum += num[i];
if(sum < min) continue;
count++;
sum = 0;
}

return count;
}
}