Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit edf565c

Browse files
Merge pull request #108 from KodaHye/main
[8주차] 고다혜
2 parents e9b0e74 + 8efca3e commit edf565c

File tree

9 files changed

+457
-0
lines changed

9 files changed

+457
-0
lines changed

‎BOJ/1000-5000번/DH_1911.java‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 훍길 보수하기
6+
*/
7+
8+
public class DH_1911 {
9+
static class Node {
10+
int s, e;
11+
public Node(int s, int e) {
12+
this.s = s;
13+
this.e = e;
14+
}
15+
}
16+
public static void main(String[] args) throws Exception {
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
20+
int N = Integer.parseInt(st.nextToken()); // 폭우
21+
int L = Integer.parseInt(st.nextToken()); // 널빤지
22+
23+
Node[] nodes = new Node[N];
24+
for(int i = 0; i < nodes.length; i++) {
25+
st = new StringTokenizer(br.readLine());
26+
int s = Integer.parseInt(st.nextToken());
27+
int e = Integer.parseInt(st.nextToken());
28+
29+
nodes[i] = new Node(s, e);
30+
}
31+
32+
// 시작점 기준 정렬
33+
Arrays.sort(nodes, (o1, o2) -> Integer.compare(o1.s, o2.s));
34+
35+
int current = 0, cnt = 0;
36+
for(Node n: nodes) {
37+
if(current < n.s) {
38+
current = n.s;
39+
}
40+
41+
while(current < n.e) {
42+
current += L;
43+
cnt++;
44+
}
45+
}
46+
47+
System.out.println(cnt);
48+
}
49+
}

‎BOJ/1000-5000번/DH_2461.java‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 대표 선수
6+
*/
7+
8+
public class DH_2461 {
9+
static class Node {
10+
int value, i, j;
11+
public Node(int value, int i, int j) {
12+
this.value = value;
13+
this.i = i;
14+
this.j = j;
15+
}
16+
}
17+
static int[][] arr;
18+
19+
public static void main(String[] args) throws Exception {
20+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
21+
StringTokenizer st = new StringTokenizer(br.readLine());
22+
23+
int N = Integer.parseInt(st.nextToken()); // 학급의 수
24+
int M = Integer.parseInt(st.nextToken()); // 학생의 수
25+
26+
arr = new int[N][M];
27+
for(int i = 0; i < N; i++) {
28+
st = new StringTokenizer(br.readLine());
29+
for(int j = 0; j < M; j++) arr[i][j] = Integer.parseInt(st.nextToken());
30+
// 한 줄이 다 입력될 때 마다 정렬
31+
Arrays.sort(arr[i]);
32+
}
33+
34+
int result = Integer.MAX_VALUE;
35+
PriorityQueue<Node> pq = new PriorityQueue<>(Comparator.comparingInt(o -> o.value));
36+
37+
int max = 0;
38+
for(int i = 0 ; i < N; i++){
39+
pq.add(new Node(arr[i][0], i, 0));
40+
max = Math.max(arr[i][0],max);
41+
}
42+
43+
while(true){
44+
Node current = pq.poll();
45+
result = Math.min(max - current.value, result);
46+
47+
// 제일 작은 값이 있는 원소의 idx가 열의 마지막 idx라면 반복문 나오기
48+
if(current.j == M - 1) break;
49+
50+
// 최솟값이 있는 행의 열 idx를 + 1 한 뒤, pq에 넣어주고,
51+
// max 값 업데이트 해주기
52+
int value = arr[current.i][current.j + 1];
53+
pq.add(new Node(value, current.i, current.j + 1));
54+
max = Math.max(max,value);
55+
}
56+
System.out.println(result);
57+
}
58+
}

‎BOJ/20001-25000번/DH_20007.java‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 떡 돌리기
6+
*/
7+
8+
public class DH_20007 {
9+
static final Long INF = Long.MAX_VALUE;
10+
static class Node implements Comparable<Node> {
11+
int e;
12+
long w;
13+
public Node(int e, long w) {
14+
this.e = e;
15+
this.w = w;
16+
}
17+
18+
@Override
19+
public int compareTo(Node o) {
20+
return Long.compare(this.w, o.w);
21+
}
22+
}
23+
static ArrayList<Node> adj[];
24+
static int N, M, X, Y;
25+
static long[] dis;
26+
27+
static void dijkstra() {
28+
dis = new long[N];
29+
Arrays.fill(dis, INF);
30+
31+
32+
PriorityQueue<Node> pq = new PriorityQueue<Node>();
33+
pq.add(new Node(Y, 0));
34+
dis[Y] = 0;
35+
36+
while(!pq.isEmpty()) {
37+
Node current = pq.poll();
38+
39+
for(Node next: adj[current.e]) {
40+
if(dis[next.e] > dis[current.e] + next.w) {
41+
dis[next.e] = dis[current.e] + next.w;
42+
pq.add(new Node(next.e, dis[next.e]));
43+
}
44+
}
45+
}
46+
}
47+
48+
static int getCnt() {
49+
// 가는 길이가 최소길이 순이 될 수 있도록 정렬
50+
Arrays.sort(dis);
51+
52+
// 한 번에 갈 수 있는 경우의 수
53+
int cnt = 1;
54+
55+
long tmpDis = 0; // 거리의 합계
56+
for(int i = 0; i < N; i++) {
57+
long doubleDis = dis[i] * 2; // 왕복 거리
58+
// 도달할 수 없는 지점이 있거나, 왕복 거리가 X보다 멀다면 -1 반환
59+
if(dis[i] == INF || doubleDis > X) return - 1;
60+
61+
// 왕복 거리로 갈 수 있다면 거리의 합계에 더해주기
62+
if(tmpDis + doubleDis <= X) {
63+
tmpDis += doubleDis;
64+
}
65+
// 갈 수 없다면 cnt 늘려주고 거리의 합계를 왕복 거리로 갱신
66+
else {
67+
cnt++;
68+
tmpDis = doubleDis;
69+
}
70+
}
71+
72+
return cnt;
73+
}
74+
public static void main(String[] args) throws Exception {
75+
initInput();
76+
dijkstra();
77+
System.out.println(getCnt());
78+
}
79+
80+
static void initInput() throws Exception {
81+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
82+
StringTokenizer st = new StringTokenizer(br.readLine());
83+
84+
N = Integer.parseInt(st.nextToken()); // 집의 개수
85+
M = Integer.parseInt(st.nextToken()); // 도로의 개수
86+
X = Integer.parseInt(st.nextToken()); // 갈 수 있는 최대 거리
87+
Y = Integer.parseInt(st.nextToken()); // 시작점
88+
89+
adj = new ArrayList[N];
90+
for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Node>();
91+
92+
for(int i = 0; i < M; i++) {
93+
st = new StringTokenizer(br.readLine());
94+
int a = Integer.parseInt(st.nextToken());
95+
int b = Integer.parseInt(st.nextToken());
96+
int c = Integer.parseInt(st.nextToken());
97+
98+
adj[a].add(new Node(b, c));
99+
adj[b].add(new Node(a, c));
100+
}
101+
}
102+
}

‎BOJ/5001-10000번/DH_9342.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.io.*;
2+
3+
/*
4+
* 염색체
5+
*/
6+
7+
public class DH_9342 {
8+
public static void main(String[] args) throws Exception {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
StringBuilder sb = new StringBuilder();
11+
12+
int t = Integer.parseInt(br.readLine());
13+
for(int i = 0; i < t; i++) {
14+
String s = br.readLine();
15+
if(s.matches("^[ABCEDF]?A+F+C+[ABCDEF]?$")) {
16+
sb.append("Infected!\n");
17+
} else sb.append("Good\n");
18+
}
19+
20+
System.out.println(sb);
21+
}
22+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class DH_방화벽_설치하기 {
5+
static class Point {
6+
int r, c;
7+
public Point(int r, int c) {
8+
this.r = r;
9+
this.c = c;
10+
}
11+
}
12+
static int N, M, emptyArea, result;
13+
static int[][] map;
14+
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
15+
16+
static void solution() {
17+
// 방화벽 3개 추가 설치 (조합)
18+
func(0, 0, new boolean[N][M]);
19+
System.out.println(result);
20+
}
21+
22+
static int bfs(boolean[][] wall) {
23+
Deque<Point> q = new ArrayDeque<Point>();
24+
25+
int cnt = 0; // 불에 타는 영역 수
26+
boolean[][] v = new boolean[N][M];
27+
28+
for(int r = 0; r < N; r++) {
29+
for(int c = 0; c < M; c++) {
30+
// 이미 방문한 곳이거나, 불이 아니거나, 벽일 때
31+
if(v[r][c] || map[r][c] != 2 || wall[r][c]) continue;
32+
q.add(new Point(r, c));
33+
v[r][c] = true;
34+
}
35+
}
36+
37+
while(!q.isEmpty()) {
38+
Point current = q.poll();
39+
40+
for(int d = 0; d < 4; d++) {
41+
int nr = current.r + dr[d];
42+
int nc = current.c + dc[d];
43+
44+
// 범위에 벗어나거나, 이미 간 곳이거나, 벽이면 못감
45+
if(!check(nr, nc) || v[nr][nc] || map[nr][nc] == 1 || wall[nr][nc]) continue;
46+
q.add(new Point(nr, nc));
47+
v[nr][nc]= true;
48+
cnt++;
49+
}
50+
}
51+
52+
return cnt;
53+
}
54+
55+
static boolean check(int r, int c) {
56+
return r >= 0 && r < N && c >= 0 && c < M;
57+
}
58+
static void func(int idx, int depth, boolean[][] wall) {
59+
if(depth == 3) {
60+
61+
int fireArea = bfs(wall);
62+
result = Math.max(result, emptyArea - 3 - fireArea);
63+
64+
return;
65+
}
66+
67+
for(int i = idx; i < N * M; i++) {
68+
int r = i / M;
69+
int c = i % M;
70+
71+
// 이미 방화벽이 있거나, 불이 있는 곳에는 방화벽을 세울 수 없음
72+
if(map[r][c] != 0) continue;
73+
74+
wall[r][c] = true;
75+
func(i + 1, depth + 1, wall);
76+
wall[r][c] = false;
77+
}
78+
}
79+
80+
public static void main(String[] args) throws Exception {
81+
System.setIn(new FileInputStream("./input/방화벽설치하기.txt"));
82+
initInput();
83+
solution();
84+
}
85+
86+
static void initInput() throws Exception {
87+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
88+
StringTokenizer st = new StringTokenizer(br.readLine());
89+
90+
result = Integer.MIN_VALUE;
91+
92+
N = Integer.parseInt(st.nextToken());
93+
M = Integer.parseInt(st.nextToken());
94+
95+
map = new int[N][M];
96+
97+
for(int r = 0; r < N; r++) {
98+
st = new StringTokenizer(br.readLine());
99+
for(int c = 0; c < M; c++) {
100+
map[r][c] = Integer.parseInt(st.nextToken());
101+
if(map[r][c] == 0) emptyArea++;
102+
}
103+
}
104+
}
105+
}

‎Programmers/Level2/DH_150369.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
택배 배달과 수거하기
3+
*/
4+
5+
class DH_150369 {
6+
public long solution(int cap, int n, int[] deliveries, int[] pickups) {
7+
long answer = 0;
8+
9+
int d = 0, p = 0;
10+
11+
for(int i = n - 1; i >= 0; i--) {
12+
d -= deliveries[i];
13+
p -= pickups[i];
14+
15+
while(d < 0 || p < 0) {
16+
d += cap;
17+
p += cap;
18+
19+
// 왕복 왔다갔다 하는 거리
20+
answer += (i + 1) * 2;
21+
}
22+
}
23+
return answer;
24+
}
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /