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 3d1662c

Browse files
Merge pull request #233 from GreatAlgorithm-Study/dahye
[17주차] 고다혜
2 parents 4e4082e + 0d045e5 commit 3d1662c

File tree

9 files changed

+607
-26
lines changed

9 files changed

+607
-26
lines changed

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 닭싸움 팀 정하기
6+
*/
7+
8+
public class DH_1765 {
9+
static class Node {
10+
int e;
11+
boolean isFriend;
12+
13+
public Node(int e, boolean isFriend) {
14+
this.e = e;
15+
this.isFriend = isFriend;
16+
}
17+
}
18+
static boolean[] check;
19+
static ArrayList<Node> adj[];
20+
static int groupCnt;
21+
22+
public static void main(String[] args) throws Exception {
23+
initInput();
24+
solution();
25+
}
26+
27+
static void solution() {
28+
for(int i = 1; i < check.length; i++) {
29+
if(check[i]) continue;
30+
groupCnt += 1;
31+
dfs(i);
32+
}
33+
34+
System.out.println(groupCnt);
35+
}
36+
37+
static void dfs(int node) {
38+
check[node] = true;
39+
40+
for(Node next: adj[node]) {
41+
// 현재 사람의 바로 옆 사람이 친구라면 같은 팀을 할 수 있음
42+
if(next.isFriend) {
43+
44+
if(check[next.e]) continue;
45+
dfs(next.e);
46+
47+
}
48+
// 현재 사람의 바로 옆 사람이 원수라면
49+
else {
50+
// 옆사람과 옆옆사람이 원수일 때, 현재 사람과 같은 팀을 할 수 있음
51+
for(Node nnext: adj[next.e]) {
52+
if(check[nnext.e] || nnext.isFriend) continue;
53+
dfs(nnext.e);
54+
}
55+
}
56+
}
57+
}
58+
59+
static void initInput() throws Exception {
60+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
61+
StringTokenizer st;
62+
63+
int n = Integer.parseInt(br.readLine());
64+
check = new boolean[n + 1];
65+
adj = new ArrayList[n + 1];
66+
67+
for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Node>();
68+
69+
int m = Integer.parseInt(br.readLine());
70+
71+
for(int i = 0; i < m; i++) {
72+
st = new StringTokenizer(br.readLine());
73+
74+
boolean isFriend = st.nextToken().charAt(0) == 'F';
75+
76+
int p = Integer.parseInt(st.nextToken());
77+
int q = Integer.parseInt(st.nextToken());
78+
79+
adj[p].add(new Node(q, isFriend));
80+
adj[q].add(new Node(p, isFriend));
81+
}
82+
}
83+
}

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

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,61 @@
66
*/
77

88
public class DH_2258 {
9-
9+
1010
static class Node implements Comparable<Node> {
1111
int w, c;
12-
12+
1313
public Node(int w, int c) {
1414
this.w = w;
1515
this.c = c;
1616
}
1717

18-
public Node(int w, int c, int sum) {
19-
this.w = w;
20-
this.c = c;
21-
}
22-
2318
@Override
2419
public int compareTo(Node o) {
25-
if (this.c != o.c)
26-
return Integer.compare(this.c, o.c); // 가격 오름차순
20+
if(this.c != o.c) return Integer.compare(this.c, o.c); // 가격 오름차순
2721
return Integer.compare(o.w, this.w); // 무게 내림차순
2822
}
2923
}
30-
31-
static final int INF = Integer.MAX_VALUE;
32-
24+
static final Long INF = Long.MAX_VALUE;
25+
3326
public static void main(String[] args) throws Exception {
3427
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
3528
StringTokenizer st = new StringTokenizer(br.readLine());
3629

3730
PriorityQueue<Node> pq = new PriorityQueue<Node>();
38-
31+
3932
int N = Integer.parseInt(st.nextToken()); // 덩어리의 개수
4033
long M = Integer.parseInt(st.nextToken()); // 필요한 고기의 양
41-
42-
for(int r = 0; r < N; r++) {
34+
35+
for(int r = 0; r < N; r++) {
4336
st = new StringTokenizer(br.readLine());
4437
int a = Integer.parseInt(st.nextToken());
4538
int b = Integer.parseInt(st.nextToken());
46-
39+
4740
pq.add(new Node(a, b));
4841
}
49-
50-
int result = INF;
42+
43+
long result = INF;
5144

5245
long totalSum = 0;
53-
int totalCost = 0, same = 0, markCost = -1;
54-
55-
while (!pq.isEmpty()) {
46+
long totalCost = 0;
47+
int same = 0, markCost = -1;
48+
49+
while(!pq.isEmpty()) {
5650
Node current = pq.poll();
57-
58-
if(markCost != current.c) {
51+
52+
if(markCost != current.c) {
5953
markCost = current.c;
6054
same = 0;
6155
} else same += current.c;
6256

6357
totalSum += current.w;
6458
totalCost = current.c;
65-
if(totalSum >= M)
59+
if(totalSum >= M) {
6660
result = Math.min(result, totalCost + same);
61+
}
6762
}
68-
69-
System.out.println(result == INF ? -1: result);
63+
64+
System.out.println(result == INF ? -1: result);
7065
}
7166
}

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 빵집
6+
*/
7+
8+
public class DH_3109 {
9+
static int[] dr = {-1, 0, 1}, dc = {1, 1, 1};
10+
static int R, C, cnt;
11+
static char[][] map;
12+
static boolean flag;
13+
14+
public static void main(String[] args) throws Exception {
15+
initInput();
16+
solution();
17+
18+
System.out.println(cnt);
19+
}
20+
21+
static void solution() {
22+
for(int r = 0; r < R; r++) {
23+
flag = false;
24+
dfs(r, 0); // (r, 0)에서 dfs 시작
25+
}
26+
}
27+
28+
static void dfs(int r, int c) {
29+
if(c == C - 1) {
30+
cnt += 1;
31+
flag = true;
32+
return;
33+
}
34+
35+
for(int d = 0; d < 3; d++) {
36+
int nr = r + dr[d];
37+
int nc = c + dc[d];
38+
39+
if(!check(nr, nc) || map[nr][nc] == 'x' || flag) continue;
40+
map[nr][nc] = 'x';
41+
dfs(nr, nc);
42+
}
43+
}
44+
45+
static boolean check(int r, int c) {
46+
return r >= 0 && r < R && c >= 0 && c < C;
47+
}
48+
49+
static void initInput() throws Exception {
50+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
51+
StringTokenizer st = new StringTokenizer(br.readLine());
52+
53+
R= Integer.parseInt(st.nextToken());
54+
C = Integer.parseInt(st.nextToken());
55+
56+
map = new char[R][C];
57+
58+
for(int r = 0; r < R; r++) {
59+
String str = br.readLine();
60+
map[r] = str.toCharArray();
61+
}
62+
}
63+
64+
static void printMap(char[][] map) {
65+
for(int r = 0; r < map.length; r++) {
66+
System.out.println(Arrays.toString(map[r]));
67+
}
68+
69+
System.out.println();
70+
}
71+
}

0 commit comments

Comments
(0)

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