Skip to content
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
29 changes: 29 additions & 0 deletions 7주차/KBC/boj_11867.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//BOJ 11867 박스 나누기 게임
public class Main {
static final boolean WIN = true;
static final boolean LOSE = false;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
int m = Integer.parseInt(input[1]);
int max = Math.max(n, m);
boolean[] dp = new boolean[max + 1];
dp[1] = LOSE;
dp[2] = WIN;

for (int i = 3; i <= max; i++) {
for (int k = 1; k <= i / 2; k++) {
dp[i] = !(dp[k] | dp[i - k]);
if(dp[i]) break;
}
}

System.out.println(dp[n] | dp[m] ? "A" : "B");
}
}
91 changes: 91 additions & 0 deletions 7주차/KBC/boj_15686.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

//BOJ 15686 [치킨 배달]
public class Main {
static int N;
static int M;
static List<Pos> houses;
static List<Pos> stores;
static int numberOfStore;
static boolean[] isVisited;
static int[] selectedStore;
static int result;


public static class Pos {
int x;
int y;

public Pos(int x, int y) {
this.x = x;
this.y = y;
}

public int getDistance(Pos p) {
return Math.abs(x - p.x) + Math.abs(y - p.y);
}
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
initialize(br);
dfs(0, 0);
System.out.println(result);
}

public static void initialize(BufferedReader br) throws IOException {
String[] input = br.readLine().split(" ");
N = Integer.parseInt(input[0]);
M = Integer.parseInt(input[1]);
houses = new ArrayList<>();
stores = new ArrayList<>();

for (int y = 1; y <= N; y++) {
String[] line = br.readLine().split(" ");
for (int x = 1; x <= N; x++) {
if (line[x - 1].equals("1")) {
houses.add(new Pos(x, y));
}
if (line[x - 1].equals("2")) {
stores.add(new Pos(x, y));
}
}
}

numberOfStore = stores.size();
isVisited = new boolean[numberOfStore];
selectedStore = new int[M];
result = Integer.MAX_VALUE;
}

public static void dfs(int depth, int init) {
if (depth == M) {
//도시의 치킨 거리 구하기
result = Math.min(result, getTotalDistance());
return;
}
//치킨집 M개 선택
for (int i = init; i < stores.size(); i++) {
selectedStore[depth] = i;
dfs(depth + 1, i + 1);
}
}

public static int getTotalDistance() {
int totalDistance = 0;
for (Pos house : houses) {
int distance = Integer.MAX_VALUE;
for (int depth = 0; depth < M; depth++) {
int index = selectedStore[depth];
Pos store = stores.get(index);
distance = Math.min(distance, house.getDistance(store));
}
totalDistance += distance;
}
return totalDistance;
}
}
64 changes: 64 additions & 0 deletions 7주차/KBC/boj_1717.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;


//BOJ 1717 집합의 표현
public class Main {
static int[] pointer;
static int[] priority;
static final int UNION = 0;
static final int COMPARE = 1;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
int m = Integer.parseInt(input[1]);

pointer = new int[n + 1];
priority = new int[n + 1];
for (int i = 0; i <= n; i++) {
pointer[i] = i;
}

for (int i = 0; i < m; i++) {
int[] temp = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int type = temp[0];
int lSet = find(temp[1]);
int rSet = find(temp[2]);

if (type == UNION) {
if(lSet == rSet) continue;
if (priority[lSet] > priority[rSet]) {
pointer[rSet] = lSet;
} else {
pointer[lSet] = rSet;
if(priority[lSet] == priority[rSet])
priority[rSet]++;
}
}
if (type == COMPARE) {
if (lSet == rSet) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}

public static int find(int x) {
if (pointer[x] == x) {
return x;
} else {
return find(pointer[x]);
}
}
}
//1 2
//0 0 1
//1 0 1
//
//
83 changes: 83 additions & 0 deletions 7주차/KBC/boj_1939.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.lang.Math.max;
import static java.lang.Math.min;

//BOJ 1939 중량제한
public class Main {
static boolean[] isVisit;
static int[] prevWeight;
static List<List<Island>> paths;
static int maxWeight;

public static class Island {

int number;
int weight;

public Island(int target, int weight) {
this.number = target;
this.weight = weight;
}
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//초기 데이터 n, m
int[] set = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
//이전까지 도달했던 최대중량 값
prevWeight = new int[set[0] + 1];
//최대 중량
maxWeight = 0;
isVisit = new boolean[set[0] + 1];

//다음 섬의 이름과 중량에 대한 정보를 담은 Graph클래스
//Graph클래스를 2차원배열로 선언, 각 섬마다 갈 수 있는 다음 섬을 담을 것
paths = new ArrayList<>();
for (int i = 0; i <= set[0]; i++) {
paths.add(new ArrayList<>());
}


for (int i = 0; i < set[1]; i++) {
int[] input = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int source = input[0];
int target = input[1];
int weight = input[2];

//양방향 그래프
paths.get(source).add(new Island(target, weight));
paths.get(target).add(new Island(source, weight));
}

//최대중량을 구하고자 하는 경로
int[] path = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int source = path[0];
int target = path[1];

dfs(source, target, Integer.MAX_VALUE);
System.out.println(maxWeight);
}

//현재 위치, 목표 위치, 지금 까지 거친 경로에서 가능한 최대중량
public static void dfs(int source, int target, int weight) {
if (source == target) {
maxWeight = max(maxWeight, weight);
return;
}

if (!isVisit[source] && weight > prevWeight[source]) {
isVisit[source] = true;
prevWeight[source] = weight;
for (Island island : paths.get(source)) {
dfs(island.number, target, min(weight, island.weight));
}
isVisit[source] = false;
}
}
}
43 changes: 43 additions & 0 deletions 7주차/KBC/boj_9935.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.Stack;

//BOJ 9935 [문자열 폭발]
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();

Stack<Character> stack = new Stack<>();
String sentence = br.readLine();
String bomb = br.readLine();

for (char ch : sentence.toCharArray()) {
stack.add(ch);
if (stack.size() >= bomb.length()) {
boolean isBombed = true;
for (int i = 0; i < bomb.length(); i++) {
if (stack.get((stack.size() - bomb.length()) + i) != bomb.charAt(i)) {
isBombed = false;
break;
}
}
if (isBombed) {
for (int i = 0; i < bomb.length(); i++) {
stack.pop();
}
}
}
}

if (stack.isEmpty()) {
System.out.println("FRULA");
} else {
for(char ch : stack) {
sb.append(ch);
}
System.out.println(sb);
}
}
}