Skip to content

Commit 4e52e49

Browse files
authored
Merge pull request #66 from yeahdy/main
[5์ฃผ์ฐจ] ์ด์˜ˆ์ง„
2 parents 8760c68 + 39db8c4 commit 4e52e49

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)