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 4e52e49

Browse files
authored
Merge pull request #66 from yeahdy/main
[5μ£Όμ°¨] μ΄μ˜ˆμ§„
2 parents 8760c68 + 39db8c4 commit 4e52e49

File tree

7 files changed

+557
-0
lines changed

7 files changed

+557
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* μ•Œκ³ λ¦¬μ¦˜: DFS
9+
* μ‹œκ°„λ³΅μž‘λ„: 1 ≀ n ≀ 10,000
10+
* 아이디어:
11+
* νŠΈλ¦¬μ— μ‘΄μž¬ν•˜λŠ” λͺ¨λ“  κ²½λ‘œλ“€ μ€‘μ—μ„œ κ°€μž₯ κΈ΄ κ²ƒμ˜ 길이 κ΅¬ν•˜κΈ°
12+
* DFS 1번: λ£¨νŠΈμ—μ„œ κ°€μž₯ 멀리 μžˆλŠ” λ…Έλ“œ 탐색 (λ£¨νŠΈλŠ” 항상 μ‘΄μž¬ν•˜κΈ° λ•Œλ¬Έμ— λ£¨νŠΈμ—μ„œ μ‹œμž‘)
13+
* DFS 2번: ν•΄λ‹Ή λ…Έλ“œμ—μ„œ κ°€μž₯ λ¨Ό λ…Έλ“œ 탐색 > κ°€μž₯ κΈ΄ 길이 계산
14+
*/
15+
16+
class Node {
17+
int node;
18+
int weight;
19+
20+
public Node(int node, int weight) {
21+
this.node = node;
22+
this.weight = weight;
23+
}
24+
}
25+
26+
public class YJ_1967 {
27+
static List<Node>[] TREE = null;
28+
static boolean[] VISITED = null;
29+
static int FAR_NODE = 0;
30+
static int longLength = 0;
31+
public static void main(String[] args) throws IOException {
32+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
33+
int N = Integer.parseInt(br.readLine());
34+
VISITED = new boolean[N+1];
35+
36+
//List index κ°€ ν•˜λ‚˜μ˜ λ…Έλ“œ λ‹¨μœ„
37+
TREE = new ArrayList[N+1];
38+
for(int i=1; i<N+1; i++){
39+
TREE[i] = new ArrayList<>();
40+
}
41+
42+
//트리 생성
43+
for(int i=1; i<N; i++){
44+
String[] line = br.readLine().split("\\s");
45+
int parent = Integer.parseInt(line[0]);
46+
int child = Integer.parseInt(line[1]);
47+
int weight = Integer.parseInt(line[2]);
48+
49+
TREE[parent].add(new Node(child,weight)); //β˜…λ¬΄λ°©ν–₯ (μžμ‹ λ…Έλ“œλ‘œ 계속 깊게 탐색)
50+
TREE[child].add(new Node(parent,weight)); //β˜…μ–‘λ°©ν–₯ (μžμ‹ λ…Έλ“œμ—μ„œ λΆ€λͺ¨λ…Έλ“œλ₯Ό 타고 λ‹€μ‹œ μœ„λ‘œ 올라였면 탐색)
51+
}
52+
//λ£¨νŠΈλΆ€ν„° μ‹œμž‘ν•˜λ―€λ‘œ 방문처리
53+
int root = 1;
54+
VISITED[root] = true;
55+
56+
//1.λ£¨νŠΈμ—μ„œ κ°€μž₯ 멀리 μžˆλŠ” λ…Έλ“œ 탐색
57+
dfs(root,0);
58+
59+
//2.ν•΄λ‹Ή λ…Έλ“œμ—μ„œ κ°€μž₯ λ¨Ό λ…Έλ“œ 탐색
60+
VISITED = new boolean[N+1];
61+
VISITED[FAR_NODE] = true;
62+
dfs(FAR_NODE,0);
63+
64+
System.out.println(longLength);
65+
br.close();
66+
}
67+
68+
private static void dfs(int start, int sum){
69+
longLength = Math.max(sum,longLength);
70+
if(sum == longLength){
71+
FAR_NODE = start;
72+
}
73+
74+
for(Node data : TREE[start]){
75+
int node = data.node;
76+
int weight = data.weight;
77+
78+
if(!VISITED[node]){
79+
VISITED[node] = true;
80+
dfs(node,sum+weight);
81+
VISITED[node] = false;
82+
}
83+
}
84+
}
85+
86+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package BOJ;
2+
3+
import java.io.*;
4+
import java.util.ArrayDeque;
5+
import java.util.Deque;
6+
7+
/**
8+
* μ•Œκ³ λ¦¬μ¦˜: BFS
9+
* μ‹œκ°„λ³΅μž‘λ„:
10+
* ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€ t ≀ 50 , 편의점 갯수 0 ≀ n ≀ 100
11+
* λͺ¨λ“  지점 κ°„μ˜ 거리 계산 μ‹œ N * (N-1) = O(N^2) > 50 * (100^2) = 50 * 10000 = 500,000 > 10^5 으둜 BFS 풀이가λŠ₯
12+
* */
13+
public class YJ_9205 {
14+
static class Pos {
15+
int x;
16+
int y;
17+
Pos(int x, int y) {
18+
this.x = x;
19+
this.y = y;
20+
}
21+
}
22+
23+
static Deque<Pos> DEQUE;
24+
static int[][] CONVINI;
25+
public static void main(String[] args) throws IOException {
26+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
27+
int t = Integer.parseInt(br.readLine());
28+
29+
for(int i=0; i<t; i++){
30+
int conviniCount = Integer.parseInt(br.readLine());
31+
32+
//μ§‘
33+
String[] home = br.readLine().split("\\s");
34+
DEQUE = new ArrayDeque<>();
35+
DEQUE.offer(new Pos(Integer.parseInt(home[0]), Integer.parseInt(home[1])));
36+
//편의점
37+
CONVINI = new int[conviniCount][2];
38+
for(int j=0; j<conviniCount; j++){
39+
String[] c = br.readLine().split("\\s");
40+
CONVINI[j][0] = Integer.parseInt(c[0]);
41+
CONVINI[j][1] = Integer.parseInt(c[1]);
42+
}
43+
//νŽ˜μŠ€ν‹°λ²Œ
44+
String[] f = br.readLine().split("\\s");
45+
Pos festival = new Pos(Integer.parseInt(f[0]), Integer.parseInt(f[1]));
46+
47+
bfs(conviniCount, festival);
48+
}
49+
br.close();
50+
}
51+
52+
static void bfs(int conviniCount, Pos festival){
53+
final int STAMINA = 1000;
54+
boolean[] visited = new boolean[conviniCount];
55+
56+
while(!DEQUE.isEmpty()){
57+
Pos current = DEQUE.poll();
58+
if(calculateDistance(current,festival) <= STAMINA){
59+
System.out.println("happy");
60+
return;
61+
}
62+
63+
for(int i=0; i<conviniCount; i++){
64+
if(visited[i]){
65+
continue;
66+
}
67+
Pos nextConvini = new Pos(CONVINI[i][0],CONVINI[i][1]);
68+
if(calculateDistance(current,nextConvini) <= STAMINA){
69+
DEQUE.offer(nextConvini);
70+
visited[i] = true;
71+
}
72+
}
73+
}
74+
System.out.println("sad");
75+
}
76+
77+
private static int calculateDistance(Pos current, Pos next){
78+
return Math.abs(current.x-next.x) + Math.abs(current.y-next.y);
79+
}
80+
81+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
class Student {
5+
int x;
6+
int y;
7+
int likeCount;
8+
int blanckCount;
9+
10+
public Student(int x, int y, int likeCount, int blanckCount){
11+
this.x=x;
12+
this.y=y;
13+
this.likeCount=likeCount;
14+
this.blanckCount=blanckCount;
15+
}
16+
17+
public boolean isBestPosition(Student best){
18+
//1. 4λ°©ν–₯ 탐색 "μ’‹μ•„ν•˜λŠ” 친ꡬ"의 μˆ˜κ°€ κ°€μž₯ λ§Žμ€ μœ„μΉ˜λ‘œ 이동
19+
if(this.likeCount != best.likeCount){
20+
return this.likeCount > best.likeCount;
21+
}
22+
//2. 4λ°©ν–₯을 νƒμƒ‰ν•΄μ„œ λΉ„μ–΄μžˆλŠ” μœ„μΉ˜ μΉ΄μš΄νŒ…
23+
else if(this.blanckCount != best.blanckCount){
24+
return this.blanckCount > best.blanckCount;
25+
}
26+
//3. κ·Έ 쀑 ν–‰ λ²ˆν˜Έκ°€ κ°€μž₯ μž‘μ€ μœ„μΉ˜λ‘œ 이동
27+
else if(this.x != best.x){
28+
return this.x < best.x;
29+
}
30+
//4. κ·Έ 쀑 μ—΄ λ²ˆν˜Έκ°€ κ°€μž₯ μž‘μ€ μœ„μΉ˜λ‘œ 이동
31+
return this.y < best.y;
32+
}
33+
34+
}
35+
36+
public class YJ_놀이기ꡬ_νƒ‘μŠΉ {
37+
static int[] numbers; //학생듀 번호
38+
static Map<Integer,List<Integer>> likeStudents = new HashMap<>(); //학생별 μ’‹μ•„ν•˜λŠ” 번호
39+
static int[][] ride; //놀이기ꡬ νƒ‘μŠΉ
40+
static int n=0;
41+
static int TOTAL=0;
42+
public static void main(String[] args) throws IOException{
43+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
44+
n = Integer.parseInt(br.readLine());
45+
ride = new int[n][n];
46+
TOTAL = n*n;
47+
numbers = new int[TOTAL];
48+
49+
//μž…λ ₯κ°’ μ΄ˆκΈ°ν™”
50+
for(int i=0; i<TOTAL; i++){
51+
String[] line = br.readLine().split("\\s");
52+
numbers[i] = Integer.parseInt(line[0]);
53+
54+
//μ’‹μ•„ν•˜λŠ” 학생 4λͺ…
55+
List<Integer> likes = new ArrayList<>();
56+
for(int j=0; j<4; j++){
57+
likes.add(Integer.parseInt(line[j+1]));
58+
}
59+
likeStudents.put(numbers[i],likes);
60+
}
61+
62+
for(int i=0; i<TOTAL; i++){
63+
//학생 1λͺ…을 λͺ¨λ“  ꡬ간을 νƒμƒ‰ν•˜λ©° μ΅œμš°μ„  μžλ¦¬μ— μ•‰νžˆκΈ°
64+
search(numbers[i], likeStudents.get(numbers[i]));
65+
}
66+
67+
//점수판
68+
int score = 0;
69+
for(int i=0; i<n; i++){
70+
for(int j=0; j<n; j++){
71+
score += calculateScore(i,j);
72+
}
73+
}
74+
System.out.println(score);
75+
}
76+
77+
static void search(int number, List<Integer> likes){
78+
Student best = new Student(n,n,-1,-1);
79+
for(int i=0; i<n; i++){
80+
for(int j=0; j<n; j++){
81+
if(ride[i][j] != 0){
82+
continue;
83+
}
84+
Student student = getCountByCondition(likes,i,j);
85+
if(student.isBestPosition(best)){
86+
best = student;
87+
}
88+
}
89+
}
90+
ride[best.x][best.y] = number;
91+
}
92+
93+
static int[] nx = {0,1,0,-1};
94+
static int[] ny = {1,0,-1,0};
95+
private static Student getCountByCondition(List<Integer> likes, int currentX, int currentY){
96+
int blanckCount=0;
97+
int likeCount=0;
98+
99+
for(int i=0; i<4; i++){
100+
int x = currentX + nx[i];
101+
int y = currentY + ny[i];
102+
103+
if(stop(x,y)){
104+
continue;
105+
}
106+
//2.λΉ„μ–΄μžˆλŠ” μœ„μΉ˜ μΉ΄μš΄νŒ…
107+
if(ride[x][y] == 0){
108+
blanckCount++;
109+
}
110+
//1.μ’‹μ•„ν•˜λŠ” 친ꡬ 수 μΉ΄μš΄νŒ…
111+
else if(likes.contains(ride[x][y])){
112+
likeCount++;
113+
}
114+
}
115+
return new Student(currentX,currentY,likeCount,blanckCount);
116+
}
117+
118+
static int[] scoreBoard = {0,1,10,100,1000};
119+
static int calculateScore(int currentX, int currentY){
120+
int friendCount = 0;
121+
List<Integer> likes = likeStudents.get(ride[currentX][currentY]);
122+
for(int p=0; p<4; p++){
123+
int x = currentX + nx[p];
124+
int y = currentY + ny[p];
125+
if(stop(x,y)){
126+
continue;
127+
}
128+
if(likes.contains(ride[x][y])){
129+
friendCount++;
130+
}
131+
}
132+
return scoreBoard[friendCount];
133+
}
134+
135+
private static boolean stop(int x, int y){
136+
return x < 0 || x >= n || y < 0 || y >= n;
137+
}
138+
}

0 commit comments

Comments
(0)

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