Skip to content
Merged
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
81 changes: 81 additions & 0 deletions BOJ/1000-5000번/DH_1967.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import java.io.*;
import java.util.*;

/*
* 트리의 지름
*/

public class BOJ1967 {
static class Node {
int e, w;
public Node(int e, int w) {
this.e = e;
this.w = w;
}
}
static ArrayList<Node> adj[]; // 노드 정보를 인접 리스트로 저장

public static void main(String[] args) throws Exception {
initInput();
solution();
}

static void solution() {
// maxInfo: 노드의 id와 출발점 기준으로 얼마나 떨어져있는지 저장
int[] maxInfo = getMaxDisInfo(1); // 루트 노듣를 기준으로 maxInfo 구하기
System.out.println(getMaxDisInfo(maxInfo[0])[1]); // maxInfo기준으로 maxInfo 구하기
}

static int[] getMaxDisInfo(int start) {
// maxInfo[0]: 가장 멀리 떨어져 있는 Node의 idx 저장
// maxinfo[1]: 가장 멀리 떨어져 있는 Node가 start 지점으로부터 얼마나 떨어져있는지 저장
int[] maxInfo = new int[2];
maxInfo[1] = Integer.MIN_VALUE;

Deque<Node> q = new ArrayDeque<Node>();
boolean[] v = new boolean[adj.length];

q.add(new Node(start, 0));
v[start] = true;

while(!q.isEmpty()) {
Node current = q.poll();

if(maxInfo[1] < current.w) {
maxInfo[1] = current.w;
maxInfo[0] = current.e;
}

for(Node next: adj[current.e]) {
if(v[next.e]) continue;
q.add(new Node(next.e, current.w + next.w));
v[next.e] = true;
}
}


return maxInfo;
}


static void initInput() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

int N = Integer.parseInt(br.readLine());

adj = new ArrayList[N + 1];

for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Node>();
for(int i = 0; i < N - 1; i++) {
st = new StringTokenizer(br.readLine());

int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());

adj[a].add(new Node(b, w));
adj[b].add(new Node(a, w));
}
}
}
57 changes: 57 additions & 0 deletions BOJ/1000-5000번/DH_4781.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.io.*;
import java.util.*;

/*
사탕 가게
*/

public class BOJ4781 {
static class Candy {
int c, p;
public Candy(int c, int p) {
this.c = c;
this.p = p;
}
}

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();

while(true) {

st = new StringTokenizer(br.readLine());

int n = Integer.parseInt(st.nextToken()); // 사탕 종류의 수
int m = (int) (Math.round(Double.parseDouble(st.nextToken()) * 100)); // 돈의 양
if(n == 0 && m == 0) break;

Candy[] candys = new Candy[n + 1];

for(int i = 1; i < n + 1; i++) {
st = new StringTokenizer(br.readLine());
int c = Integer.parseInt(st.nextToken());
int p = (int) (Math.round(Double.parseDouble(st.nextToken()) * 100));

candys[i] = new Candy(c, p);
}

int[][] dp = new int[candys.length][m + 1];

// dp[i][j]: j원을 쓰면서 i번 사탕까지 살 때, 살 수 있는 최대 칼로리
for(int i = 1; i < dp.length; i++) {
for(int j = 0; j < dp[0].length; j++) {
if(j >= candys[i].p) {
dp[i][j] = Math.max(candys[i].c + dp[i][j - candys[i].p], dp[i - 1][j]);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
sb.append(dp[n][m] + "\n");
}

System.out.print(sb);
}
}
110 changes: 110 additions & 0 deletions BOJ/5001-10000번/DH_9205.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import java.io.*;
import java.util.*;

/*
* 맥주 마시면서 걸어가기
*/

public class DH_9205 {
static class Point {
int r, c;
public Point(int r, int c) {
this.r = r;
this.c = c;
}
}
static class Node implements Comparable<Node> {
int e, w;
public Node(int e, int w) {
this.e = e;
this.w = w;
}

@Override
public int compareTo(Node o) {
return Integer.compare(this.w, o.w);
}
}
static StringBuilder sb = new StringBuilder();
static ArrayList<Node> adj[];
static Point[] p;

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

int tc = Integer.parseInt(br.readLine());

for(int t = 0; t < tc; t++) {
int n = Integer.parseInt(br.readLine()); // 맥주를 파는 편의점의 개수

// 시작할 떄 맥주 한 박스 들고 시작 - 200 * 50 미터 갈 수 있음
p = new Point[n + 2];
adj = new ArrayList[n + 2];

// 위치들 입력 받기
// 0: 집이 있는 곳, n + 1: 락 페스티벌 좌표
for(int i = 0; i < p.length; i++) {
st = new StringTokenizer(br.readLine());
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());

p[i] = new Point(r, c);
}

// 양방향 인접리스트로 표현하기
for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Node>();
for(int i = 0; i < p.length - 1; i++) {
for(int j = i + 1; j < p.length; j++) {
int dis = getDis(i, j);

// 해당 지점을 가기 전 50미터에서 맥주 한 병을 다 마셔야 됨
// 두 지점간 거리가 1000을 넘으면 못감
if(dis > 1000) continue;
adj[i].add(new Node(j, dis));
adj[j].add(new Node(i, dis));
}
}

// 다익스트라를 통해 '집의 시작점: 0' 에서부터 페스티벌 지점까지 갈 수 있는지 확인
sb.append(dijkstra(0)? "happy": "sad").append("\n");
}

System.out.println(sb);
}

// 우선순위 큐를 사용한 다익스트라
static boolean dijkstra(int s) {
PriorityQueue<Node> q = new PriorityQueue<Node>();

q.add(new Node(s, 0));
boolean[] v = new boolean[adj.length];
int[] dis = new int[adj.length];

Arrays.fill(dis, Integer.MAX_VALUE);
v[0] = true;
dis[0] = 0;

while(!q.isEmpty()) {
Node current = q.poll();
v[current.e] = true;

if(current.e == adj.length - 1) return true;
for(Node next: adj[current.e]) {
if(v[next.e]) continue;

if(dis[next.e] > dis[current.e] + next.w) {
dis[next.e] = dis[current.e] + next.w;

q.add(new Node(next.e, dis[next.e]));
}
}
}

return false;
}

static int getDis(int i, int j) {
return Math.abs(p[i].r - p[j].r) + Math.abs(p[i].c - p[j].c);
}
}
127 changes: 127 additions & 0 deletions CodeTree/2021-2022년/DH_놀이기구_탑승.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import java.io.*;
import java.util.*;

public class DH_놀이기구_탑승 {
static LinkedHashMap<Integer, HashSet<Integer>> hashMap;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저 LinkedHashMap 처음봐요...!ㅋㅎㅋㅎ 그냥 해시맵이 아닌 LinkedHashMap를 사용한 이유가 궁금해요!😎

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 이슈를 보니까 입력순서대로 앉힐수 있군요!?!?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞습니다!! 그냥 HashMap을 쓰면 key의 순서가 뒤죽박죽 되더라구요!!!
입력하면서 학생들을 앉혀도 되는데!! 저는 그 생각을 못해서 입력 다 받고, 학생들을 앉히기 위해 LinkedHashMap을 썼습니다!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와우 저도 처음봐요!! 순서를 보장해야 할때 써봐야겠어요!! 👍

static int[][] map;
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};

static void solution() {
// 학생들 map에 위치시키기
for(int key: hashMap.keySet()) {
placeKey(key);
}

// 점수 계산
System.out.println(getScore());
}

// 점수계산
static int getScore() {
int result = 0;
for(int r = 0; r < map.length; r++) {
for(int c = 0; c < map[0].length; c++) {

int current = map[r][c];
int likeCnt = 0;

for(int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];

if(!check(nr, nc)) continue;
// 주변에 친한 친구가 있으면 likeCnt 증가
if(hashMap.get(current).contains(map[nr][nc])) likeCnt++;
}

// 점수 배분: 10^(좋아하는 친구의 수 - 1)
result += Math.pow(10, likeCnt - 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

점수 10의 지수승으로 계산하신거 멋지네요👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주변에 좋아하는 친구 없는 친구 불쌍해요..😹

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10^0 슬프네요,,,,

}
}

return result;
}
static void placeKey(int key) {
// setR, setC: key를 위치할 r, c 정보
// 초기 빈 자리 수는 -1 로 해서 key의 위치가 잘못 정해지는 것 방지
int setR = 0, setC = 0, likeCnt = 0, emptyCnt = -1;

// map 순차 탐색하면서 학생이 앉을 수 있는 곳 찾기
for(int r = 0; r < map.length; r++) {
for(int c = 0; c < map[0].length; c++) {
if(map[r][c] != 0) continue;

int emptyTmpCnt = 0;
int likeTmpCnt = 0;

for(int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];

if(!check(nr, nc)) continue;
int next = map[nr][nc];
if(next == 0) emptyTmpCnt++; // map이 비어있다면 emptyTmpCnt++
if(hashMap.get(key).contains(next)) { // 친한 친구에 속한다면 likeTmpCnt++;
likeTmpCnt++;
}
}

// r, c위치에서 좋아하는 친구 수가 이제까지 좋아하는 친구 수 보다 많으면
// setR, setC 위치 update
// likeCnt, emptyCnt 값도 update
if(likeCnt < likeTmpCnt) {
setR = r;
setC = c;
likeCnt = likeTmpCnt;
emptyCnt = emptyTmpCnt;
} else if(likeCnt == likeTmpCnt) {
// 좋아하는 친구 수가 이제까지 좋아하는 친구 수와 같다면
// 비어있는자리 확인하기
// 현재 위치에서 주변 비어있는 자리 개수가, 직전 자리에서 비어있는 개수보다 많다면
// setR, setC, emptyCnt 값 update
if(emptyCnt < emptyTmpCnt) {
setR = r;
setC = c;
likeCnt = likeTmpCnt;
emptyCnt = emptyTmpCnt;
}
}
}
}

map[setR][setC] = key;
}

static boolean check(int r, int c) {
return r >= 0 && r < map.length && c >= 0 && c < map[0].length;
}
public static void main(String[] args) throws Exception {
initInput();
solution();
}

static void initInput() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

int n = Integer.parseInt(br.readLine());
map = new int[n][n];

hashMap = new LinkedHashMap<>();

for(int i = 0; i < n * n; i++) {
st = new StringTokenizer(br.readLine());
int n0 = Integer.parseInt(st.nextToken());
int n1 = Integer.parseInt(st.nextToken());
int n2 = Integer.parseInt(st.nextToken());
int n3 = Integer.parseInt(st.nextToken());
int n4 = Integer.parseInt(st.nextToken());

hashMap.put(n0, new HashSet<>());
hashMap.get(n0).add(n1);
hashMap.get(n0).add(n2);
hashMap.get(n0).add(n3);
hashMap.get(n0).add(n4);
}
}
}
Loading