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
86 changes: 86 additions & 0 deletions BOJ/1000-5000번/YJ_1967.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
* 알고리즘: DFS
* 시간복잡도: 1 ≤ n ≤ 10,000
* 아이디어:
* 트리에 존재하는 모든 경로들 중에서 가장 긴 것의 길이 구하기
* DFS 1번: 루트에서 가장 멀리 있는 노드 탐색 (루트는 항상 존재하기 때문에 루트에서 시작)
* DFS 2번: 해당 노드에서 가장 먼 노드 탐색 > 가장 긴 길이 계산
*/

class Node {
int node;
int weight;

public Node(int node, int weight) {
this.node = node;
this.weight = weight;
}
}

public class YJ_1967 {
static List<Node>[] TREE = null;
static boolean[] VISITED = null;
static int FAR_NODE = 0;
static int longLength = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
VISITED = new boolean[N+1];

//List index 가 하나의 노드 단위
TREE = new ArrayList[N+1];
for(int i=1; i<N+1; i++){
TREE[i] = new ArrayList<>();
}

//트리 생성
for(int i=1; i<N; i++){
String[] line = br.readLine().split("\\s");
int parent = Integer.parseInt(line[0]);
int child = Integer.parseInt(line[1]);
int weight = Integer.parseInt(line[2]);

TREE[parent].add(new Node(child,weight)); //★무방향 (자식 노드로 계속 깊게 탐색)
TREE[child].add(new Node(parent,weight)); //★양방향 (자식 노드에서 부모노드를 타고 다시 위로 올라오면 탐색)
}
//루트부터 시작하므로 방문처리
int root = 1;
VISITED[root] = true;

//1.루트에서 가장 멀리 있는 노드 탐색
dfs(root,0);

//2.해당 노드에서 가장 먼 노드 탐색
VISITED = new boolean[N+1];
VISITED[FAR_NODE] = true;
dfs(FAR_NODE,0);

System.out.println(longLength);
br.close();
}

private static void dfs(int start, int sum){
longLength = Math.max(sum,longLength);
if(sum == longLength){
FAR_NODE = start;
}

for(Node data : TREE[start]){
int node = data.node;
int weight = data.weight;

if(!VISITED[node]){
VISITED[node] = true;
dfs(node,sum+weight);
VISITED[node] = false;
}
}
}

}
81 changes: 81 additions & 0 deletions BOJ/5001-10000번/YJ_9205.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package BOJ;

import java.io.*;
import java.util.ArrayDeque;
import java.util.Deque;

/**
* 알고리즘: BFS
* 시간복잡도:
* 테스트 케이스 t ≤ 50 , 편의점 갯수 0 ≤ n ≤ 100
* 모든 지점 간의 거리 계산 시 N * (N-1) = O(N^2) > 50 * (100^2) = 50 * 10000 = 500,000 > 10^5 으로 BFS 풀이가능
* */
public class YJ_9205 {
static class Pos {
int x;
int y;
Pos(int x, int y) {
this.x = x;
this.y = y;
}
}

static Deque<Pos> DEQUE;
static int[][] CONVINI;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());

for(int i=0; i<t; i++){
int conviniCount = Integer.parseInt(br.readLine());

//집
String[] home = br.readLine().split("\\s");
DEQUE = new ArrayDeque<>();
DEQUE.offer(new Pos(Integer.parseInt(home[0]), Integer.parseInt(home[1])));
//편의점
CONVINI = new int[conviniCount][2];
for(int j=0; j<conviniCount; j++){
String[] c = br.readLine().split("\\s");
CONVINI[j][0] = Integer.parseInt(c[0]);
CONVINI[j][1] = Integer.parseInt(c[1]);
}
//페스티벌
String[] f = br.readLine().split("\\s");
Pos festival = new Pos(Integer.parseInt(f[0]), Integer.parseInt(f[1]));

bfs(conviniCount, festival);
}
br.close();
}

static void bfs(int conviniCount, Pos festival){
final int STAMINA = 1000;
boolean[] visited = new boolean[conviniCount];

while(!DEQUE.isEmpty()){
Pos current = DEQUE.poll();
if(calculateDistance(current,festival) <= STAMINA){
System.out.println("happy");
return;
}

for(int i=0; i<conviniCount; i++){
if(visited[i]){
continue;
}
Pos nextConvini = new Pos(CONVINI[i][0],CONVINI[i][1]);
if(calculateDistance(current,nextConvini) <= STAMINA){
DEQUE.offer(nextConvini);
visited[i] = true;
}
}
}
System.out.println("sad");
}

private static int calculateDistance(Pos current, Pos next){
return Math.abs(current.x-next.x) + Math.abs(current.y-next.y);
}

}
138 changes: 138 additions & 0 deletions CodeTree/2021-2022년/YJ_놀이기구_탑승.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import java.util.*;
import java.io.*;

class Student {
int x;
int y;
int likeCount;
int blanckCount;

public Student(int x, int y, int likeCount, int blanckCount){
this.x=x;
this.y=y;
this.likeCount=likeCount;
this.blanckCount=blanckCount;
}

public boolean isBestPosition(Student best){
//1. 4방향 탐색 "좋아하는 친구"의 수가 가장 많은 위치로 이동
if(this.likeCount != best.likeCount){
return this.likeCount > best.likeCount;
}
//2. 4방향을 탐색해서 비어있는 위치 카운팅
else if(this.blanckCount != best.blanckCount){
return this.blanckCount > best.blanckCount;
}
//3. 그 중 행 번호가 가장 작은 위치로 이동
else if(this.x != best.x){
return this.x < best.x;
}
//4. 그 중 열 번호가 가장 작은 위치로 이동
return this.y < best.y;
}

}

public class YJ_놀이기구_탑승 {
static int[] numbers; //학생들 번호
static Map<Integer,List<Integer>> likeStudents = new HashMap<>(); //학생별 좋아하는 번호
static int[][] ride; //놀이기구 탑승
static int n=0;
static int TOTAL=0;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
ride = new int[n][n];
TOTAL = n*n;
numbers = new int[TOTAL];

//입력값 초기화
for(int i=0; i<TOTAL; i++){
String[] line = br.readLine().split("\\s");
numbers[i] = Integer.parseInt(line[0]);

//좋아하는 학생 4명
List<Integer> likes = new ArrayList<>();
for(int j=0; j<4; j++){
likes.add(Integer.parseInt(line[j+1]));
}
likeStudents.put(numbers[i],likes);
}

for(int i=0; i<TOTAL; i++){
//학생 1명을 모든 구간을 탐색하며 최우선 자리에 앉히기
search(numbers[i], likeStudents.get(numbers[i]));
}

//점수판
int score = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
score += calculateScore(i,j);
}
}
System.out.println(score);
}

static void search(int number, List<Integer> likes){
Student best = new Student(n,n,-1,-1);
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(ride[i][j] != 0){
continue;
}
Student student = getCountByCondition(likes,i,j);
if(student.isBestPosition(best)){
best = student;
}
}
}
ride[best.x][best.y] = number;
}

static int[] nx = {0,1,0,-1};
static int[] ny = {1,0,-1,0};
private static Student getCountByCondition(List<Integer> likes, int currentX, int currentY){
int blanckCount=0;
int likeCount=0;

for(int i=0; i<4; i++){
int x = currentX + nx[i];
int y = currentY + ny[i];

if(stop(x,y)){
continue;
}
//2.비어있는 위치 카운팅
if(ride[x][y] == 0){
blanckCount++;
}
//1.좋아하는 친구 수 카운팅
else if(likes.contains(ride[x][y])){
likeCount++;
}
}
return new Student(currentX,currentY,likeCount,blanckCount);
}

static int[] scoreBoard = {0,1,10,100,1000};
static int calculateScore(int currentX, int currentY){
int friendCount = 0;
List<Integer> likes = likeStudents.get(ride[currentX][currentY]);
for(int p=0; p<4; p++){
int x = currentX + nx[p];
int y = currentY + ny[p];
if(stop(x,y)){
continue;
}
if(likes.contains(ride[x][y])){
friendCount++;
}
}
return scoreBoard[friendCount];
}

private static boolean stop(int x, int y){
return x < 0 || x >= n || y < 0 || y >= n;
}
}
Loading