diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/boj/youngsu5582/week1/1260.java b/boj/youngsu5582/week1/1260.java new file mode 100644 index 0000000..600bdaa --- /dev/null +++ b/boj/youngsu5582/week1/1260.java @@ -0,0 +1,67 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.StringTokenizer; + +public class Main { + private static final StringBuilder sb = new StringBuilder(); + + public static void main(final String[] args) throws IOException { + final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(reader.readLine(), " "); + + int n = Integer.parseInt(st.nextToken()); + int m = Integer.parseInt(st.nextToken()); + final int v = Integer.parseInt(st.nextToken()); + + + final int[][] ary = new int[n + 1][n + 1]; + final boolean[] visited = new boolean[n + 1]; + final boolean[] visited2 = visited.clone(); + for (int i = 0; i < m; i++) { + st = new StringTokenizer(reader.readLine(), " "); + final int x = Integer.parseInt(st.nextToken()); + final int y = Integer.parseInt(st.nextToken()); + ary[x][y] = 1; + ary[y][x] = 1; + } + dfs(visited, ary, v); + sb.append("\n"); + bfs(visited2, ary, v); + System.out.println(sb); + + } + + public static void dfs(boolean[] visited, int[][] ary, int pos) { + visited[pos] = true; + sb.append(pos) + .append(" "); + for (int i = 1; i < ary[pos].length; i++) { + if (!visited[i] && ary[pos][i] == 1) { + dfs(visited, ary, i); + } + } + } + + public static void bfs(boolean[] visited, int[][] ary, int pos) { + Deque temp = new ArrayDeque<>(); + temp.add(pos); + while (!temp.isEmpty()) { + pos = temp.pollFirst(); + if (visited[pos]) { + continue; + } + visited[pos] = true; + sb.append(pos) + .append(" "); + for (int i = 1; i < ary[pos].length; i++) { + if (!visited[i] && ary[pos][i] == 1) { + temp.add(i); + } + } + } + } + +} diff --git a/boj/youngsu5582/week1/2606.java b/boj/youngsu5582/week1/2606.java new file mode 100644 index 0000000..917ea48 --- /dev/null +++ b/boj/youngsu5582/week1/2606.java @@ -0,0 +1,43 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Main { + private static boolean[] visited; + private static int[][] ary; + private static int n; + private static int count; + + public static void main(final String[] args) throws IOException { + final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + + n = Integer.valueOf(reader.readLine()); + int m = Integer.parseInt(reader.readLine()); + + ary = new int[n + 1][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()); + ary[x][y] = 1; + ary[y][x] = 1; + } + + visited = new boolean[n + 1]; + dfs(1); + System.out.println(count-1); + } + + public static void dfs(int pos) { + visited[pos] = true; + count++; + for (int i = 1; i <= n; i++) { + if(!visited[i] && ary[pos][i]==1) { + dfs(i); + } + } + } + +} diff --git a/boj/youngsu5582/week1/2839.java b/boj/youngsu5582/week1/2839.java new file mode 100644 index 0000000..ecaae23 --- /dev/null +++ b/boj/youngsu5582/week1/2839.java @@ -0,0 +1,30 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class Main { + private static final StringBuilder sb = new StringBuilder(); + + public static void main(final String[] args) throws IOException { + final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + final int n = Integer.parseInt(reader.readLine()); + + int result = Integer.MAX_VALUE; + + for(int i = 0; i*5 <=n; i++) { + for(int j = 0; i*5 + j*3 <=n; j++) { + if(i*5 + j*3 == n){ + result = Math.min(result,i+j); + } + } + } + + if(result==Integer.MAX_VALUE){ + sb.append(-1); + }else{ + sb.append(result); + } + + System.out.println(sb); + } +} diff --git a/boj/youngsu5582/week2/1461.java b/boj/youngsu5582/week2/1461.java new file mode 100644 index 0000000..93248f6 --- /dev/null +++ b/boj/youngsu5582/week2/1461.java @@ -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 plus = new PriorityQueue<>(Collections.reverseOrder()); + private static PriorityQueue 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 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; + } +} diff --git a/boj/youngsu5582/week2/1890.java b/boj/youngsu5582/week2/1890.java new file mode 100644 index 0000000..24d1346 --- /dev/null +++ b/boj/youngsu5582/week2/1890.java @@ -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]); + } + } +} diff --git a/boj/youngsu5582/week2/22944.java b/boj/youngsu5582/week2/22944.java new file mode 100644 index 0000000..98bb4e4 --- /dev/null +++ b/boj/youngsu5582/week2/22944.java @@ -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 used; + + public Player(int hp, int count, int umbrella, final Map 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; + } + } +} diff --git a/boj/youngsu5582/week2/5567.java b/boj/youngsu5582/week2/5567.java new file mode 100644 index 0000000..517640c --- /dev/null +++ b/boj/youngsu5582/week2/5567.java @@ -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); + } + } + } +} diff --git a/programmers/youngsu5582/178871.java b/programmers/youngsu5582/178871.java new file mode 100644 index 0000000..94ad92c --- /dev/null +++ b/programmers/youngsu5582/178871.java @@ -0,0 +1,37 @@ +import java.io.IOException; +import java.util.*; + +public class Main { + private static Map mp = new HashMap<>(); + + public static void main(final String[] args){ + String[] players = new String[]{"mumu", "soe", "poe", "kai", "mine"}; + String[] answers = new String[]{"kai", "kai", "mine", "mine"}; + Arrays.stream(solution(players,answers)).forEach(System.out::println); + } + + public static String[] solution(String[] players, String[] callings) { + init(players); + for (String calling : callings) { + int index = find(calling); + swap(players,calling,index); + } + return players; + } + private static void init(String[] players){ + for(int i = 0; i