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

조이썬(이영수) - 3주차 #8

Open
wants to merge 2 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
64 changes: 64 additions & 0 deletions boj/youngsu5582/week2/1461.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.Collections;
import java.util.Optional;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {
private static PriorityQueue<Integer> plus = new PriorityQueue<>(Collections.reverseOrder());
private static PriorityQueue<Integer> minus = new PriorityQueue<>(Collections.reverseOrder());
private static int n;
private static int m;
private static int sum = 0;

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

st = new StringTokenizer(br.readLine(), " ");
while (st.hasMoreTokens()) {
int number = Integer.parseInt(st.nextToken());
if (number > 0) {
plus.add(number);
} else {
minus.add(number * -1);
}
}

int max = findMax();
add(plus);
add(minus);

System.out.println(sum - max);
}

public static int findMax() {
int plus = Optional.ofNullable(Main.plus.peek())
.orElse(-1);
int minus = Optional.ofNullable(Main.minus.peek())
.orElse(-1);
return plus > minus ? plus : minus;
}

public static void add(PriorityQueue<Integer> pq) {
int count = 0;
int temp = -1;
while (!pq.isEmpty()) {
temp = Math.max(pq.poll(), temp);
count++;
if (count == m) {
count = 0;
sum += temp * 2;
temp = -1;
}
}
if (temp == -1) {
return;
}
sum += temp * 2;
}
}
58 changes: 58 additions & 0 deletions boj/youngsu5582/week2/1890.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
private static int[][] list;
private static BigInteger[][] dp;
private static int n;

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

n = Integer.valueOf(reader.readLine());

list = new int[n][n];
dp = new BigInteger[n][n];

for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(reader.readLine(), " ");
int index = 0;
while (st.hasMoreTokens()) {
list[i][index++] = Integer.valueOf(st.nextToken());
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j] = BigInteger.ZERO;
}
}

dp[0][0] = BigInteger.ONE;

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
execute(i, j);
}
}
System.out.println(dp[n - 1][n - 1]);
}

public static void execute(int x, int y) {
int count = list[x][y];
if (list[x][y] == 0) {
return;
}
int nextX = x + count;
int nextY = y + count;
if (nextY < n) {
dp[x][nextY] = dp[x][y].add(dp[x][nextY]);
}
if (nextX < n) {
dp[nextX][y] = dp[x][y].add(dp[nextX][y]);
}
}
}
176 changes: 176 additions & 0 deletions boj/youngsu5582/week2/22944.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Main {
private static Field[][] list;
private static int n;
private static int h;
private static int d;
private static int answer = Integer.MAX_VALUE;
private static int[][] visited;

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

StringTokenizer st = new StringTokenizer(reader.readLine(), " ");

n = Integer.valueOf(st.nextToken());
h = Integer.valueOf(st.nextToken());
d = Integer.valueOf(st.nextToken());

list = new Field[n][n];
visited = new int[n][n];

int startX = 0;
int startY = 0;
for (int i = 0; i < n; i++) {
String line = reader.readLine();
for (int j = 0; j < n; j++) {
list[i][j] = Field.from(line.charAt(j));
if (list[i][j] == Field.START) {
startX = i;
startY = j;
}
}
}

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
visited[i][j] = Integer.MAX_VALUE;
}
}

Player player = Player.from(h);
player.move(startX, startY);
if (answer == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(answer);
}
}

private static class Player {
private int hp;
private int umbrella;
private int count;
private Map<Integer, Boolean> used;

public Player(int hp, int count, int umbrella, final Map<Integer, Boolean> used) {
this.hp = hp;
this.count = count;
this.umbrella = umbrella;
this.used = new HashMap<>(used);
}

public static Player from(final int hp) {
return new Player(hp, 0, 0, new HashMap<>());
}


public void move(int x, int y) {
check(x, y);
if (isEnd()) {
return;
}
this.count += 1;
for (Direction direction : Direction.values()) {
int nextX = x + direction.x;
int nextY = y + direction.y;
if (canMove(nextX, nextY) && visited[nextX][nextY] > count) {
visited[nextX][nextY] = count;
new Player(hp, count, umbrella, used).move(nextX, nextY);
}
}
}

private boolean isEnd() {
if (hp == 0) {
return true;
}
return count >= answer;
}

private void check(int x, int y) {
Field f = list[x][y];
switch (f) {
case TOXIC:
moveToxic();
break;
case END:
moveEnd();
break;
case UMBRELLA:
moveUmbrella(x, y);
moveToxic();
break;
default:
}
}

private void moveUmbrella(int x, int y) {
if (used.containsKey(x * 500 + y)) {
return;
}
used.put(x * 500 + y, true);
umbrella = d;
}

private void moveToxic() {
if (umbrella > 0) {
this.umbrella -= 1;
return;
}
if (hp > 0) {
this.hp -= 1;
}
}

private void moveEnd() {
answer = Math.min(answer, count);
}

private boolean canMove(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < n)
}
}

private enum Direction {
UP(-1, 0),
RIGHT(0, 1),
DOWN(1, 0),
LEFT(0, -1);
private final int x;
private final int y;

Direction(final int x, final int y) {
this.x = x;
this.y = y;
}

}

private enum Field {
START('S'),
TOXIC('.'),
UMBRELLA('U'),
END('E');
private final char value;

Field(final char value) {
this.value = value;
}

public static Field from(final char value) {
for (final Field field : values()) {
if (field.value == value) {
return field;
}
}
return null;
}
}
}
47 changes: 47 additions & 0 deletions boj/youngsu5582/week2/5567.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
private static int[][] list;
private static boolean[] visited;
private static int answer = 0;

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

int n = Integer.parseInt(reader.readLine());
int m = Integer.parseInt(reader.readLine());

list = new int[n + 1][n + 1];
visited = new boolean[n + 1];
for (int i = 0; i < m; i++) {
StringTokenizer st = new StringTokenizer(reader.readLine(), " ");
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
list[x][y] = 1;
list[y][x] = 1;
}
visited[1] = true;

dfs(1, 2);
System.out.println(answer);

}

public static void dfs(int x, int count) {
if (count <= 0) {
return;
}
for (int i = 2; i < list.length; i++) {
if (list[x][i] == 1) {
if (!visited[i]) {
answer++;
visited[i] = true;
}
dfs(i, count - 1);
}
}
}
}
Loading