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
118 changes: 118 additions & 0 deletions week5/KBC/boj_1194.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
static class Data {
int y;
int x;
int count;
int keys;
int prevX;
int prevY;

public Data(int y, int x, int count) {
this.y = y;
this.x = x;
this.count = count;
keys = 0b0;
}

public Data(Data data) {
this.y = data.y;
this.x = data.x;
this.count = data.count;
this.keys = data.keys;
this.prevX = data.prevX;
this.prevY = data.prevY;
}

public void pickKey(int key) {
keys = keys | key;
}

public boolean isPickedKey(int key) {
return (keys & key) > 0;
}
}

static boolean[][][] visit;
static final int[] dy = {0, -1, 0, 1};
static final int[] dx = {-1, 0, 1, 0};
static String[][] map;
static int height;
static int width;


public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
height = Integer.parseInt(input[0]);
width = Integer.parseInt(input[1]);
map = new String[height][width];
visit = new boolean[64][height][width];
Data start = null;
for (int y = 0; y < height; y++) {
input = br.readLine().split("");
for (int x = 0; x < width; x++) {
map[y][x] = input[x];
if (map[y][x].equals("0")) {
start = new Data(y, x, 0);
map[y][x] = ".";
}
}
}

if (start != null) {
System.out.println(bfs(start));
return;
}
System.out.println("시작 위치가 없습니다.");
}

public static int bfs(Data start) {
Queue<Data> queue = new LinkedList<>();
queue.add(start);

while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Data data = queue.poll();
for (int k = 0; k < 4; k++) {
Data newData = new Data(data);
newData.y += dy[k];
newData.x += dx[k];
newData.count += 1;

if (outOfRange(newData)) {
continue;
}
String curTile = map[newData.y][newData.x];
if (visit[newData.keys][newData.y][newData.x] || curTile.equals("#")) {
continue;
}
visit[newData.keys][newData.y][newData.x] = true;
if (curTile.equals("1")) {
return newData.count;
}
if (curTile.matches("^[a-f]$")) { //열쇠
newData.pickKey(0b1 << (curTile.charAt(0) - 'a'));
queue.add(newData);
} else if (curTile.matches("^[A-F]$")) { //문
if (newData.isPickedKey(0b1 << (curTile.charAt(0) - 'A'))) {
queue.add(newData);
}
} else if (curTile.equals(".")) {
queue.add(newData);
}
}
}
}
return -1;
}

private static boolean outOfRange(Data newData) {
return newData.y < 0 || newData.y >= map.length || newData.x < 0 || newData.x >= map[0].length;
}
}
23 changes: 23 additions & 0 deletions week5/KBC/boj_13458.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] studentsInClass = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] input = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int b = input[0];
int c = input[1];

long totalSupervisors = 0;
for(int i=0; i<n; i++) {
int remainingStudents = Math.max(studentsInClass[i] - b, 0);
int assistantSupervisorCount = (remainingStudents + c - 1) / c;
totalSupervisors += 1 + assistantSupervisorCount;
}
System.out.println(totalSupervisors);
}
}
109 changes: 109 additions & 0 deletions week5/KBC/boj_16236.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
static class Shark {
int y;
int x;
int size;
int time;
int ate;

public Shark(int y, int x, int size, int time, int ate) {
this.y = y;
this.x = x;
this.time = time;
if (ate >= size) {
ate = 0;
size++;
}
this.size = size;
this.ate = ate;
}
}

static int n;
static int[][] map;

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

Shark babyShark = null;
map = new int[n][n];
for (int i = 0; i < n; i++) {
int[] input = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
for (int k = 0; k < n; k++) {
map[i][k] = input[k];
if (map[i][k] == 9) {
babyShark = new Shark(i, k, 2, 0, 0);
map[i][k] = 0;
}
}
}

if (babyShark == null) {
return;
}

int result = bfs(babyShark);
System.out.println(result);
}

public static int bfs(Shark babyShark) {
int[] dx = {0, -1, 1, 0};
int[] dy = {-1, 0, 0, 1};
Queue<Shark> queue = new LinkedList<>();
Queue<Shark> canEat = new LinkedList<>();
queue.add(babyShark);
boolean[][] isVisit = new boolean[n][n];
int aliveTime = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Shark current = queue.poll();
isVisit[current.y][current.x] = true;
for (int k = 0; k < 4; k++) {
int newY = current.y + dy[k];
int newX = current.x + dx[k];
if (isOutOfRange(newY, newX) || isVisit[newY][newX] || current.size < map[newY][newX]) { //지나갈수 없을 때
continue;
}
if (map[newY][newX] == 0 || map[newY][newX] == current.size) { //지나갈 수 있을 때
isVisit[newY][newX] = true;
queue.add(new Shark(newY, newX, current.size, current.time + 1, current.ate));
} else if (current.size > map[newY][newX]) { // 먹을 수 있을 때
isVisit[newY][newX] = true;
canEat.add(new Shark(newY, newX, current.size, current.time + 1, current.ate + 1));
}

}
}
if (!canEat.isEmpty()) {
Shark target = canEat.poll();
while (!canEat.isEmpty()) {
Shark temp = canEat.poll();
if (temp.y < target.y) {
target = temp;
} else if (temp.y == target.y && temp.x < target.x) {
target = temp;
}
}
isVisit = new boolean[n][n];
map[target.y][target.x] = 0;
aliveTime = target.time;
queue.clear();
queue.add(target);
}
}
return aliveTime;
}

private static boolean isOutOfRange(int newY, int newX) {
return newY < 0 || newY >= n || newX < 0 || newX >= n;
}
}
103 changes: 103 additions & 0 deletions week5/KBC/boj_2206.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
static final int MAX = 1000000;
static int[][] map;
static int height;
static int width;

static int[] dx = {1, 0, -1, 0};
static int[] dy = {0, 1, 0, -1};

static class Pos {
int x;
int y;

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

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
height = Integer.parseInt(input[0]);
width = Integer.parseInt(input[1]);
map = new int[height][width];
for (int h = 0; h < height; h++) {
int[] numbers = Arrays.stream(br.readLine().split("")).mapToInt(Integer::parseInt).toArray();
for (int w = 0; w < width; w++) {
map[h][w] = numbers[w];
}
}

int[][] distanceMapToStart = bfs(0, 0);
int[][] distanceMapToEnd = bfs(width - 1, height - 1);
int minDistance = MAX;
if(distanceMapToStart[height-1][width-1] != 0) {
minDistance = distanceMapToStart[height-1][width-1];
}
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++) {
if (map[h][w] == 1) {
int min_start = MAX;
int min_end = MAX;
for (int i = 0; i < 4; i++) {
int newX = w + dx[i];
int newY = h + dy[i];
if (newX < 0 || newX >= width || newY < 0 || newY >= height || map[newY][newX] == 1) {
continue;
}
int distanceStart = distanceMapToStart[h + dy[i]][w + dx[i]];
int distanceEnd = distanceMapToEnd[h + dy[i]][w + dx[i]];
if (distanceStart != 0) {
min_start = Math.min(distanceStart, min_start);
}
if (distanceEnd != 0) {
min_end = Math.min(distanceEnd, min_end);
}
}
minDistance = Math.min(minDistance, min_start + min_end + 1);
}
}
}
if (minDistance == MAX) {
System.out.println(-1);
return;
}
System.out.println(minDistance);
}

public static int[][] bfs(int startX, int startY) {
int[][] distanceMap = new int[height][width];
int distance = 1;
Queue<Pos> queue = new LinkedList<>();
if (map[startY][startX] != 1) {
queue.add(new Pos(startX, startY));
distanceMap[startY][startX] = distance;
distance++;
}

while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Pos curPosition = queue.poll();
for (int direct = 0; direct < 4; direct++) {
int newX = curPosition.x + dx[direct];
int newY = curPosition.y + dy[direct];
if (newX < 0 || newX >= width || newY < 0 || newY >= height || distanceMap[newY][newX] != 0 || map[newY][newX] == 1) {
continue;
}
distanceMap[newY][newX] = distance;
queue.add(new Pos(newX, newY));
}
}
distance++;
}
return distanceMap;
}
}