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 8760c68

Browse files
authored
Merge pull request #56 from KodaHye/main
[5μ£Όμ°¨] κ³ λ‹€ν˜œ
2 parents 4a630d4 + 0e3df09 commit 8760c68

File tree

6 files changed

+528
-0
lines changed

6 files changed

+528
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 트리의 지름
6+
*/
7+
8+
public class BOJ1967 {
9+
static class Node {
10+
int e, w;
11+
public Node(int e, int w) {
12+
this.e = e;
13+
this.w = w;
14+
}
15+
}
16+
static ArrayList<Node> adj[]; // λ…Έλ“œ 정보λ₯Ό 인접 리슀트둜 μ €μž₯
17+
18+
public static void main(String[] args) throws Exception {
19+
initInput();
20+
solution();
21+
}
22+
23+
static void solution() {
24+
// maxInfo: λ…Έλ“œμ˜ id와 좜발점 κΈ°μ€€μœΌλ‘œ μ–Όλ§ˆλ‚˜ λ–¨μ–΄μ ΈμžˆλŠ”μ§€ μ €μž₯
25+
int[] maxInfo = getMaxDisInfo(1); // 루트 λ…Έλ“£λ₯Ό κΈ°μ€€μœΌλ‘œ maxInfo κ΅¬ν•˜κΈ°
26+
System.out.println(getMaxDisInfo(maxInfo[0])[1]); // maxInfoκΈ°μ€€μœΌλ‘œ maxInfo κ΅¬ν•˜κΈ°
27+
}
28+
29+
static int[] getMaxDisInfo(int start) {
30+
// maxInfo[0]: κ°€μž₯ 멀리 λ–¨μ–΄μ Έ μžˆλŠ” Node의 idx μ €μž₯
31+
// maxinfo[1]: κ°€μž₯ 멀리 λ–¨μ–΄μ Έ μžˆλŠ” Nodeκ°€ start μ§€μ μœΌλ‘œλΆ€ν„° μ–Όλ§ˆλ‚˜ λ–¨μ–΄μ ΈμžˆλŠ”μ§€ μ €μž₯
32+
int[] maxInfo = new int[2];
33+
maxInfo[1] = Integer.MIN_VALUE;
34+
35+
Deque<Node> q = new ArrayDeque<Node>();
36+
boolean[] v = new boolean[adj.length];
37+
38+
q.add(new Node(start, 0));
39+
v[start] = true;
40+
41+
while(!q.isEmpty()) {
42+
Node current = q.poll();
43+
44+
if(maxInfo[1] < current.w) {
45+
maxInfo[1] = current.w;
46+
maxInfo[0] = current.e;
47+
}
48+
49+
for(Node next: adj[current.e]) {
50+
if(v[next.e]) continue;
51+
q.add(new Node(next.e, current.w + next.w));
52+
v[next.e] = true;
53+
}
54+
}
55+
56+
57+
return maxInfo;
58+
}
59+
60+
61+
static void initInput() throws Exception {
62+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
63+
StringTokenizer st;
64+
65+
int N = Integer.parseInt(br.readLine());
66+
67+
adj = new ArrayList[N + 1];
68+
69+
for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Node>();
70+
for(int i = 0; i < N - 1; i++) {
71+
st = new StringTokenizer(br.readLine());
72+
73+
int a = Integer.parseInt(st.nextToken());
74+
int b = Integer.parseInt(st.nextToken());
75+
int w = Integer.parseInt(st.nextToken());
76+
77+
adj[a].add(new Node(b, w));
78+
adj[b].add(new Node(a, w));
79+
}
80+
}
81+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
사탕 κ°€κ²Œ
6+
*/
7+
8+
public class BOJ4781 {
9+
static class Candy {
10+
int c, p;
11+
public Candy(int c, int p) {
12+
this.c = c;
13+
this.p = p;
14+
}
15+
}
16+
17+
public static void main(String[] args) throws Exception {
18+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
19+
StringTokenizer st;
20+
StringBuilder sb = new StringBuilder();
21+
22+
while(true) {
23+
24+
st = new StringTokenizer(br.readLine());
25+
26+
int n = Integer.parseInt(st.nextToken()); // 사탕 μ’…λ₯˜μ˜ 수
27+
int m = (int) (Math.round(Double.parseDouble(st.nextToken()) * 100)); // 돈의 μ–‘
28+
if(n == 0 && m == 0) break;
29+
30+
Candy[] candys = new Candy[n + 1];
31+
32+
for(int i = 1; i < n + 1; i++) {
33+
st = new StringTokenizer(br.readLine());
34+
int c = Integer.parseInt(st.nextToken());
35+
int p = (int) (Math.round(Double.parseDouble(st.nextToken()) * 100));
36+
37+
candys[i] = new Candy(c, p);
38+
}
39+
40+
int[][] dp = new int[candys.length][m + 1];
41+
42+
// dp[i][j]: j원을 μ“°λ©΄μ„œ i번 μ‚¬νƒ•κΉŒμ§€ μ‚΄ λ•Œ, μ‚΄ 수 μžˆλŠ” μ΅œλŒ€ 칼둜리
43+
for(int i = 1; i < dp.length; i++) {
44+
for(int j = 0; j < dp[0].length; j++) {
45+
if(j >= candys[i].p) {
46+
dp[i][j] = Math.max(candys[i].c + dp[i][j - candys[i].p], dp[i - 1][j]);
47+
} else {
48+
dp[i][j] = dp[i - 1][j];
49+
}
50+
}
51+
}
52+
sb.append(dp[n][m] + "\n");
53+
}
54+
55+
System.out.print(sb);
56+
}
57+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* λ§₯μ£Ό λ§ˆμ‹œλ©΄μ„œ κ±Έμ–΄κ°€κΈ°
6+
*/
7+
8+
public class DH_9205 {
9+
static class Point {
10+
int r, c;
11+
public Point(int r, int c) {
12+
this.r = r;
13+
this.c = c;
14+
}
15+
}
16+
static class Node implements Comparable<Node> {
17+
int e, w;
18+
public Node(int e, int w) {
19+
this.e = e;
20+
this.w = w;
21+
}
22+
23+
@Override
24+
public int compareTo(Node o) {
25+
return Integer.compare(this.w, o.w);
26+
}
27+
}
28+
static StringBuilder sb = new StringBuilder();
29+
static ArrayList<Node> adj[];
30+
static Point[] p;
31+
32+
public static void main(String[] args) throws Exception {
33+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
34+
StringTokenizer st;
35+
36+
int tc = Integer.parseInt(br.readLine());
37+
38+
for(int t = 0; t < tc; t++) {
39+
int n = Integer.parseInt(br.readLine()); // λ§₯μ£Όλ₯Ό νŒŒλŠ” 편의점의 개수
40+
41+
// μ‹œμž‘ν•  λ–„ λ§₯μ£Ό ν•œ λ°•μŠ€ λ“€κ³  μ‹œμž‘ - 200 * 50 λ―Έν„° 갈 수 있음
42+
p = new Point[n + 2];
43+
adj = new ArrayList[n + 2];
44+
45+
// μœ„μΉ˜λ“€ μž…λ ₯ λ°›κΈ°
46+
// 0: 집이 μžˆλŠ” κ³³, n + 1: 락 νŽ˜μŠ€ν‹°λ²Œ μ’Œν‘œ
47+
for(int i = 0; i < p.length; i++) {
48+
st = new StringTokenizer(br.readLine());
49+
int r = Integer.parseInt(st.nextToken());
50+
int c = Integer.parseInt(st.nextToken());
51+
52+
p[i] = new Point(r, c);
53+
}
54+
55+
// μ–‘λ°©ν–₯ μΈμ ‘λ¦¬μŠ€νŠΈλ‘œ ν‘œν˜„ν•˜κΈ°
56+
for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Node>();
57+
for(int i = 0; i < p.length - 1; i++) {
58+
for(int j = i + 1; j < p.length; j++) {
59+
int dis = getDis(i, j);
60+
61+
// ν•΄λ‹Ή 지점을 κ°€κΈ° μ „ 50λ―Έν„°μ—μ„œ λ§₯μ£Ό ν•œ 병을 λ‹€ λ§ˆμ…”μ•Ό 됨
62+
// 두 지점간 거리가 1000을 λ„˜μœΌλ©΄ λͺ»κ°
63+
if(dis > 1000) continue;
64+
adj[i].add(new Node(j, dis));
65+
adj[j].add(new Node(i, dis));
66+
}
67+
}
68+
69+
// λ‹€μ΅μŠ€νŠΈλΌλ₯Ό 톡해 'μ§‘μ˜ μ‹œμž‘μ : 0' μ—μ„œλΆ€ν„° νŽ˜μŠ€ν‹°λ²Œ μ§€μ κΉŒμ§€ 갈 수 μžˆλŠ”μ§€ 확인
70+
sb.append(dijkstra(0)? "happy": "sad").append("\n");
71+
}
72+
73+
System.out.println(sb);
74+
}
75+
76+
// μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©ν•œ λ‹€μ΅μŠ€νŠΈλΌ
77+
static boolean dijkstra(int s) {
78+
PriorityQueue<Node> q = new PriorityQueue<Node>();
79+
80+
q.add(new Node(s, 0));
81+
boolean[] v = new boolean[adj.length];
82+
int[] dis = new int[adj.length];
83+
84+
Arrays.fill(dis, Integer.MAX_VALUE);
85+
v[0] = true;
86+
dis[0] = 0;
87+
88+
while(!q.isEmpty()) {
89+
Node current = q.poll();
90+
v[current.e] = true;
91+
92+
if(current.e == adj.length - 1) return true;
93+
for(Node next: adj[current.e]) {
94+
if(v[next.e]) continue;
95+
96+
if(dis[next.e] > dis[current.e] + next.w) {
97+
dis[next.e] = dis[current.e] + next.w;
98+
99+
q.add(new Node(next.e, dis[next.e]));
100+
}
101+
}
102+
}
103+
104+
return false;
105+
}
106+
107+
static int getDis(int i, int j) {
108+
return Math.abs(p[i].r - p[j].r) + Math.abs(p[i].c - p[j].c);
109+
}
110+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class DH_놀이기ꡬ_νƒ‘μŠΉ {
5+
static LinkedHashMap<Integer, HashSet<Integer>> hashMap;
6+
static int[][] map;
7+
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
8+
9+
static void solution() {
10+
// 학생듀 map에 μœ„μΉ˜μ‹œν‚€κΈ°
11+
for(int key: hashMap.keySet()) {
12+
placeKey(key);
13+
}
14+
15+
// 점수 계산
16+
System.out.println(getScore());
17+
}
18+
19+
// μ μˆ˜κ³„μ‚°
20+
static int getScore() {
21+
int result = 0;
22+
for(int r = 0; r < map.length; r++) {
23+
for(int c = 0; c < map[0].length; c++) {
24+
25+
int current = map[r][c];
26+
int likeCnt = 0;
27+
28+
for(int d = 0; d < 4; d++) {
29+
int nr = r + dr[d];
30+
int nc = c + dc[d];
31+
32+
if(!check(nr, nc)) continue;
33+
// 주변에 μΉœν•œ μΉœκ΅¬κ°€ 있으면 likeCnt 증가
34+
if(hashMap.get(current).contains(map[nr][nc])) likeCnt++;
35+
}
36+
37+
// 점수 λ°°λΆ„: 10^(μ’‹μ•„ν•˜λŠ” 친ꡬ의 수 - 1)
38+
result += Math.pow(10, likeCnt - 1);
39+
}
40+
}
41+
42+
return result;
43+
}
44+
static void placeKey(int key) {
45+
// setR, setC: keyλ₯Ό μœ„μΉ˜ν•  r, c 정보
46+
// 초기 빈 자리 μˆ˜λŠ” -1 둜 ν•΄μ„œ key의 μœ„μΉ˜κ°€ 잘λͺ» μ •ν•΄μ§€λŠ” 것 λ°©μ§€
47+
int setR = 0, setC = 0, likeCnt = 0, emptyCnt = -1;
48+
49+
// map 순차 νƒμƒ‰ν•˜λ©΄μ„œ 학생이 앉을 수 μžˆλŠ” κ³³ μ°ΎκΈ°
50+
for(int r = 0; r < map.length; r++) {
51+
for(int c = 0; c < map[0].length; c++) {
52+
if(map[r][c] != 0) continue;
53+
54+
int emptyTmpCnt = 0;
55+
int likeTmpCnt = 0;
56+
57+
for(int d = 0; d < 4; d++) {
58+
int nr = r + dr[d];
59+
int nc = c + dc[d];
60+
61+
if(!check(nr, nc)) continue;
62+
int next = map[nr][nc];
63+
if(next == 0) emptyTmpCnt++; // map이 λΉ„μ–΄μžˆλ‹€λ©΄ emptyTmpCnt++
64+
if(hashMap.get(key).contains(next)) { // μΉœν•œ μΉœκ΅¬μ— μ†ν•œλ‹€λ©΄ likeTmpCnt++;
65+
likeTmpCnt++;
66+
}
67+
}
68+
69+
// r, cμœ„μΉ˜μ—μ„œ μ’‹μ•„ν•˜λŠ” 친ꡬ μˆ˜κ°€ μ΄μ œκΉŒμ§€ μ’‹μ•„ν•˜λŠ” 친ꡬ 수 보닀 많으면
70+
// setR, setC μœ„μΉ˜ update
71+
// likeCnt, emptyCnt 값도 update
72+
if(likeCnt < likeTmpCnt) {
73+
setR = r;
74+
setC = c;
75+
likeCnt = likeTmpCnt;
76+
emptyCnt = emptyTmpCnt;
77+
} else if(likeCnt == likeTmpCnt) {
78+
// μ’‹μ•„ν•˜λŠ” 친ꡬ μˆ˜κ°€ μ΄μ œκΉŒμ§€ μ’‹μ•„ν•˜λŠ” 친ꡬ μˆ˜μ™€ κ°™λ‹€λ©΄
79+
// λΉ„μ–΄μžˆλŠ”μžλ¦¬ ν™•μΈν•˜κΈ°
80+
// ν˜„μž¬ μœ„μΉ˜μ—μ„œ μ£Όλ³€ λΉ„μ–΄μžˆλŠ” 자리 κ°œμˆ˜κ°€, 직전 μžλ¦¬μ—μ„œ λΉ„μ–΄μžˆλŠ” κ°œμˆ˜λ³΄λ‹€ λ§Žλ‹€λ©΄
81+
// setR, setC, emptyCnt κ°’ update
82+
if(emptyCnt < emptyTmpCnt) {
83+
setR = r;
84+
setC = c;
85+
likeCnt = likeTmpCnt;
86+
emptyCnt = emptyTmpCnt;
87+
}
88+
}
89+
}
90+
}
91+
92+
map[setR][setC] = key;
93+
}
94+
95+
static boolean check(int r, int c) {
96+
return r >= 0 && r < map.length && c >= 0 && c < map[0].length;
97+
}
98+
public static void main(String[] args) throws Exception {
99+
initInput();
100+
solution();
101+
}
102+
103+
static void initInput() throws Exception {
104+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
105+
StringTokenizer st;
106+
107+
int n = Integer.parseInt(br.readLine());
108+
map = new int[n][n];
109+
110+
hashMap = new LinkedHashMap<>();
111+
112+
for(int i = 0; i < n * n; i++) {
113+
st = new StringTokenizer(br.readLine());
114+
int n0 = Integer.parseInt(st.nextToken());
115+
int n1 = Integer.parseInt(st.nextToken());
116+
int n2 = Integer.parseInt(st.nextToken());
117+
int n3 = Integer.parseInt(st.nextToken());
118+
int n4 = Integer.parseInt(st.nextToken());
119+
120+
hashMap.put(n0, new HashSet<>());
121+
hashMap.get(n0).add(n1);
122+
hashMap.get(n0).add(n2);
123+
hashMap.get(n0).add(n3);
124+
hashMap.get(n0).add(n4);
125+
}
126+
}
127+
}

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /