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

13주차 문제풀이 (고동우) #94

Open
wants to merge 6 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
108 changes: 108 additions & 0 deletions baekjoon/11967_불켜기/src/dongwoo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.StringTokenizer;

public class dongwoo {
static int[] dx = new int[] { 0, 1, 0, -1 };
static int[] dy = new int[] { 1, 0, -1, 0 };

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
Map<Tuple, List<Tuple>> switches = new HashMap<>();
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
boolean[][] map = new boolean[N][N];
boolean[][] visited = new boolean[N][N];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int[] from = new int[] { Integer.parseInt(st.nextToken()) - 1, Integer.parseInt(st.nextToken()) - 1 };
int[] to = new int[] { Integer.parseInt(st.nextToken()) - 1, Integer.parseInt(st.nextToken()) - 1 };
if (!switches.containsKey(new Tuple(from))) {
switches.put(new Tuple(from), new ArrayList<>());
}
switches.get(new Tuple(from)).add(new Tuple(to));
}
Queue<Tuple> q = new ArrayDeque<>();
q.add(new Tuple(new int[] { 0, 0 }));
visited[0][0] = true;
map[0][0] = true;
int cnt = 1;

while (!q.isEmpty()) {
Tuple now = q.poll();

// 현재 위치에서 켤 수 있는 불 다 켜기, 새로 불켠곳이 visited와 인접해있으면 queue에 추가
for (Tuple pos : switches.getOrDefault(now, new ArrayList<>())) {
int x = pos.tuple[0];
int y = pos.tuple[1];
if (!map[x][y]) {
cnt++;
}
map[x][y] = true;
for (int i = 0; i < 4; i++) {
int newX = x + dx[i];
int newY = y + dy[i];
boolean val;
try {
val = map[newX][newY];
} catch (Exception e) {
continue;
}
if (visited[newX][newY] && !visited[x][y]) {
q.add(new Tuple(new int[] { x, y }));
visited[x][y] = true;
break;
}
}
}
// bfs로 주변 탐색하기.
for (int i = 0; i < 4; i++) {
int x = now.tuple[0];
int y = now.tuple[1];
int newX = x + dx[i];
int newY = y + dy[i];
boolean val;
try {
val = map[newX][newY];
} catch (Exception e) {
continue;
}
if (visited[newX][newY] || !val) {
continue;
}
q.add(new Tuple(new int[] { newX, newY }));
visited[newX][newY] = true;

}
}
System.out.println(cnt);
}
}

class Tuple {
int[] tuple;

public Tuple(int[] list) {
tuple = list;
}

@Override
public boolean equals(Object obj) {
return Arrays.equals(tuple, ((Tuple) obj).tuple);
}

@Override
public int hashCode() {
return 100*tuple[0]+tuple[1];
}

}
60 changes: 60 additions & 0 deletions baekjoon/14908_구두수선공/src/dongwoo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class dongwoo {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
Work[] works = new Work[N];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
works[i] = new Work(i + 1, Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
}

Arrays.sort(works);

for (int i = 0; i < N; i++) {
sb.append(works[i].number).append(" ");
}
System.out.println(sb);
}

}

class Work implements Comparable<Work> {
int number;
int time;
int S;

public Work(int number, int time, int S) {
this.number = number;
this.time = time;
this.S = S;
}

@Override
public int compareTo(Work w) {
// double here = Math.round((double) S * 100000 / time);
// double other = Math.round((double) w.S * 100000 / w.time);
double here = (double) S * 100000 / time;
double other = (double) w.S * 100000 / w.time;
// 작업 효율 비교 (보상금 대비 걸리는 시간 )
if (here != other) {
if (here - other > 0) {
return -1;
}
if (here - other < 0) {
return 1;
}
// 효율이 같으면 시간이 적게 걸리는 순서
return this.time - w.time;
}
// 시간도 같다면 오름차순
return this.number - w.number;
}

}
83 changes: 83 additions & 0 deletions baekjoon/14927_전구끄기/src/dongwoo.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.List;
import java.util.StringTokenizer;

public class dongwoo {
static boolean[][] map;
static int[] di = new int[] { 0, 0, 1, 0, -1 };
static int[] dj = new int[] { 0, 1, 0, -1, 0 };
static int min = Integer.MAX_VALUE;
static int N;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
map = new boolean[N+1][N];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
if (Integer.parseInt(st.nextToken()) == 1) {
map[i + 1][j] = true;
}
}
}
for (int i = 0; i < 1<<N; i++) {
for (int j = 0; j < N; j++) {
if ((i & (1 << j)) == (1 << j)) {
map[0][j] = true;
} else {
map[0][j] = false;
}
}
checkLine(0, 0);
}

if (min == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(min);
}
}

private static void checkLine(int i, int clickCnt) {
List<Integer> searchable = new ArrayList<>();
for (int j = 0; j < N; j++) {
if (map[i][j]) {
searchable.add(j);
}
}
if (i == N) {
if (searchable.isEmpty()) {
min = Math.min(min, clickCnt);
}
return;
}
for (int j = 0; j < searchable.size(); j++) {
int pos = searchable.get(j);
clickSwitch(i + 1, pos);
}
checkLine(i + 1, searchable.size() + clickCnt);
for (int j = 0; j < searchable.size(); j++) {
int pos = searchable.get(j);
clickSwitch(i + 1, pos);
}

}

private static void clickSwitch(int i, int j) {
for (int k = 0; k < 5; k++) {
int newI = i + di[k];
int newJ = j + dj[k];
boolean val;
try {
val = map[newI][newJ];
} catch (Exception e) {
continue;
}
map[newI][newJ] = !val;
}
}
}
83 changes: 83 additions & 0 deletions baekjoon/2904_수학은너무쉬워/src/dongwoo.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.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class dongwoo {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
int[] numbers = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
Map<Integer, Integer> soinsubunhae = new HashMap<>(); // 등장하는 모든 숫자를 곱해서 소인수분해 때려놓은거
for (int i = 0; i < N; i++) {
numbers[i] = Integer.parseInt(st.nextToken());
disAssemble(numbers[i], soinsubunhae);
}

int maximumValue = 1;
Map<Integer, Integer> soinsubunhaeNanungeo = new HashMap<>(); // 소인수분해 한 결과를 N으로 나눈거
for (Integer i : soinsubunhae.keySet()) {
int cnt = soinsubunhae.get(i);
if (cnt / N > 0) {
maximumValue *= (int) Math.pow(i, (cnt / N));
soinsubunhaeNanungeo.put(i, soinsubunhaeNanungeo.getOrDefault(i, 0) + cnt / N);
}
}
sb.append(maximumValue).append(" ");

// 각 숫자마다 돌면서 부족한 숫자 채워넣기
int moves = 0;
for (int i = 0; i < N; i++) {
Map<Integer, Integer> res = new HashMap<>();
disAssemble(numbers[i], res);
for (Integer n : soinsubunhaeNanungeo.keySet()) {
if (soinsubunhaeNanungeo.get(n) > res.getOrDefault(n, 0)) {
moves += soinsubunhaeNanungeo.get(n) - res.getOrDefault(n, 0);
}
}
}
sb.append(moves);
System.out.println(sb);

}

// 소인수분해
public static void disAssemble(int N, Map<Integer, Integer> result) {
if (N == 1) {
return;
}
if (isPrime(N)) {
if (result.containsKey(N)) {
result.put(N, result.get(N) + 1);
} else {
result.put(N, 1);
}
return;
}
for (int i = (int) Math.sqrt(N) + 1; i > 1; i--) {
if (N % i == 0) {
disAssemble(N / i, result);
disAssemble(i, result);
break;
}
}

}

private static boolean isPrime(int N) {
if (N < 2) {
return false;
}
for (int i = 2; i <= Math.sqrt(N); i++) {
if (N % i == 0) {
return false;
}
}
return true;
}

}
Loading