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 532364a

Browse files
authored
Merge pull request #53 from yeongleej/main
[5주차] 이지영
2 parents 4e52e49 + 18b6182 commit 532364a

File tree

9 files changed

+536
-0
lines changed

9 files changed

+536
-0
lines changed

‎BOJ/1000-5000번/JY_1967.java‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package day1009;
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
public class JY_1967 {
7+
8+
static int N;
9+
static int maxLen, lastNode, ans;
10+
static List<Node>[] nrr;
11+
static boolean[] visited;
12+
static class Node {
13+
int u, dist;
14+
public Node(int u, int dist) {
15+
this.u = u;
16+
this.dist = dist;
17+
}
18+
@Override
19+
public String toString() {
20+
return "Node [u=" + u + ", dist=" + dist + "]";
21+
}
22+
23+
}
24+
25+
public static void main(String[] args) throws IOException{
26+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
27+
StringTokenizer st = new StringTokenizer(br.readLine());
28+
29+
N = Integer.parseInt(st.nextToken());
30+
nrr = new ArrayList[N+1];
31+
for(int i=1; i<N+1; i++) {
32+
nrr[i] = new ArrayList<>();
33+
}
34+
for(int i=0; i<N-1; i++) {
35+
st = new StringTokenizer(br.readLine());
36+
37+
int n1 = Integer.parseInt(st.nextToken());
38+
int n2 = Integer.parseInt(st.nextToken());
39+
int e = Integer.parseInt(st.nextToken());
40+
41+
nrr[n1].add(new Node(n2, e));
42+
nrr[n2].add(new Node(n1, e));
43+
44+
}
45+
46+
// System.out.println(Arrays.toString(nrr));
47+
48+
/**
49+
* 임의의 노드에서 가장 먼 노드를 찾고, 그 노드에서 다시 가장 먼 노드를 찾으면 트리의 지름을 구할 수 있음
50+
* 2번의 dfs 활용
51+
* 1) 임의의 노드에서 가장 먼 노드 찾기
52+
* 2) 찾은 노드로 시작하는 가장 긴 간선의 길이 찾기
53+
* */
54+
55+
// 1. 임의의 노드에서 가장 먼 노드 찾기
56+
// lastNode: 임의의 노드에서 가장 먼 노드의 길이
57+
// maxLen: 가장 먼 노드까지의 간선의 길이
58+
maxLen = Integer.MIN_VALUE;
59+
visited = new boolean[N+1];
60+
visited[1] = true;
61+
lastNode = 0;
62+
dfs(1, 0);
63+
64+
// System.out.println(maxLen+", "+lastNode);
65+
66+
// 2. 찾은 노드로 부터 가장 먼 노드까지의 길이가 트리의 지름
67+
maxLen = Integer.MIN_VALUE;
68+
visited = new boolean[N+1];
69+
visited[lastNode] = true;
70+
dfs(lastNode, 0);
71+
72+
System.out.println(maxLen);
73+
74+
}
75+
public static void dfs(int now, int len) {
76+
if(len > maxLen) {
77+
maxLen = len;
78+
lastNode = now;
79+
}
80+
81+
for(Node next: nrr[now]) {
82+
if(!visited[next.u]) {
83+
visited[next.u] = true;
84+
dfs(next.u, len+next.dist);
85+
}
86+
}
87+
}
88+
89+
}

‎BOJ/1000-5000번/JY_4781.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package day1009;
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
public class JY_4781 {
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
11+
while(true) {
12+
StringTokenizer st = new StringTokenizer(br.readLine());
13+
int N = Integer.parseInt(st.nextToken());
14+
double m = Double.parseDouble(st.nextToken());
15+
16+
if(N == 0) break;
17+
18+
// 실수 -> 정수화 할때, 오차범위 0.5
19+
int M = (int)(m * 100 + 0.5);
20+
int[] dp = new int[M+1];
21+
22+
for(int i=0; i<N; i++) {
23+
st = new StringTokenizer(br.readLine());
24+
int kcal = Integer.parseInt(st.nextToken());
25+
int p = (int)(Double.parseDouble(st.nextToken())*100+0.5);
26+
27+
// dp[j]: 돈 j로 살 수 있는 최대 칼로리
28+
// dp[j] = max(dp[j], dp[j-p]+kcal)
29+
for(int j=p; j<M+1; j++) {
30+
dp[j] = Math.max(dp[j], dp[j-p]+kcal);
31+
}
32+
33+
}
34+
System.out.println(dp[M]);
35+
}
36+
37+
}
38+
39+
}

‎BOJ/5001-10000번/JY_9205.java‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package day1011;
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
public class JY_9205 {
7+
8+
static int N;
9+
static int[] dx = {0, 0, 1, -1};
10+
static int[] dy = {1, -1, 0, 0};
11+
static Pos[] stores;
12+
static boolean canGo;
13+
static Pos home, festival;
14+
15+
static class Pos {
16+
int x, y, beer;
17+
18+
public Pos(int x, int y, int beer) {
19+
super();
20+
this.x = x;
21+
this.y = y;
22+
this.beer = beer;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "Pos [x=" + x + ", y=" + y + ", beer=" + beer + "]";
28+
}
29+
30+
}
31+
32+
public static void main(String[] args) throws IOException{
33+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
34+
StringTokenizer st = new StringTokenizer(br.readLine());
35+
36+
int T = Integer.parseInt(st.nextToken());
37+
38+
for(int t=0; t<T; t++) {
39+
st = new StringTokenizer(br.readLine());
40+
N = Integer.parseInt(st.nextToken());
41+
st = new StringTokenizer(br.readLine());
42+
int hx = Integer.parseInt(st.nextToken());
43+
int hy = Integer.parseInt(st.nextToken());
44+
home = new Pos(hx, hy, 20);
45+
46+
stores = new Pos[N];
47+
for(int i=0; i<N; i++) {
48+
st = new StringTokenizer(br.readLine());
49+
int sx = Integer.parseInt(st.nextToken());
50+
int sy = Integer.parseInt(st.nextToken());
51+
stores[i] = new Pos(sx, sy, 0);
52+
}
53+
54+
st = new StringTokenizer(br.readLine());
55+
int fx = Integer.parseInt(st.nextToken());
56+
int fy = Integer.parseInt(st.nextToken());
57+
festival = new Pos(fx, fy, 0);
58+
59+
// BFS 탐색
60+
canGo = false;
61+
bfs();
62+
63+
if(canGo) System.out.println("happy");
64+
else System.out.println("sad");
65+
}
66+
67+
}
68+
public static void bfs() {
69+
Queue<Pos> q = new LinkedList<>();
70+
boolean[] visited = new boolean[N];
71+
q.add(home);
72+
73+
while(!q.isEmpty()) {
74+
Pos now = q.poll();
75+
76+
// 현재 위치에서 페스티벌로 갈 수 있다면 break
77+
if(isGo(now, festival)) {
78+
canGo = true;
79+
break;
80+
}
81+
// 아직 못간다면, 갈 수 있는 편의점 탐색
82+
for(int i=0; i<N; i++) {
83+
if(!visited[i]) {
84+
Pos nextStore = stores[i];
85+
if(isGo(now, nextStore)) {
86+
visited[i] = true;
87+
// 맥주 장전
88+
nextStore.beer = 20;
89+
q.add(nextStore);
90+
}
91+
}
92+
}
93+
}
94+
95+
}
96+
public static boolean isGo(Pos from, Pos to) {
97+
// 현재 맥주로 갈 수 있는 최대거리
98+
int maxDist = from.beer * 50;
99+
// 맨하탄 거리
100+
int distance = Math.abs(from.x-to.x)+Math.abs(from.y-to.y);
101+
// 남은 거리가 더 많으면 불가능
102+
if(distance > maxDist) return false;
103+
return true;
104+
105+
}
106+
107+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class Main {
5+
6+
static int N;
7+
static int[][] g;
8+
static List<Integer>[] srr;
9+
static int[] score = {0, 1, 10, 100, 1000};
10+
static int[] dx = {0, 0, -1, 1};
11+
static int[] dy = {-1, 1, 0, 0};
12+
static class Pos implements Comparable<Pos> {
13+
int x, y, like, empty;
14+
public Pos(int x, int y, int like, int empty) {
15+
this.x = x;
16+
this.y = y;
17+
this.like = like;
18+
this.empty = empty;
19+
}
20+
@Override
21+
public int compareTo(Pos other) {
22+
if(this.like == other.like) {
23+
if(this.empty == other.empty) {
24+
if(this.x == other.x) {
25+
return this.y - other.y;
26+
}
27+
return this.x - other.x;
28+
}
29+
return other.empty - this.empty;
30+
}
31+
return other.like - this.like;
32+
}
33+
@Override
34+
public String toString() {
35+
return this.x+","+this.y+" like:"+this.like+" empty:"+this.empty;
36+
}
37+
}
38+
39+
public static void main(String[] args) throws IOException {
40+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
41+
StringTokenizer st = new StringTokenizer(br.readLine());
42+
43+
N = Integer.parseInt(st.nextToken());
44+
g = new int[N+1][N+1];
45+
46+
// 각 학생의 좋아하는 사람 리스트
47+
srr = new ArrayList[N*N+1];
48+
for(int s=0; s<N*N+1; s++) {
49+
srr[s] = new ArrayList<>();
50+
}
51+
52+
for(int s = 1; s<N*N+1; s++) {
53+
st = new StringTokenizer(br.readLine());
54+
int num = Integer.parseInt(st.nextToken());
55+
56+
for(int i=0; i<4; i++) {
57+
srr[num].add(Integer.parseInt(st.nextToken()));
58+
}
59+
60+
// 빈칸 찾기 & 대입
61+
findPos(num, srr[num]);
62+
}
63+
64+
// print();
65+
66+
// 점수 계산
67+
int ans = calScore();
68+
69+
System.out.println(ans);
70+
}
71+
public static void print() {
72+
for(int i = 0; i<N+1; i++) {
73+
System.out.println(Arrays.toString(g[i]));
74+
}
75+
}
76+
public static boolean inRange(int x, int y) {
77+
return x>0 && x<=N && y>0 && y<=N;
78+
}
79+
public static void findPos(int num, List<Integer> sList) {
80+
PriorityQueue<Pos> pq = new PriorityQueue<>();
81+
82+
for(int i=1; i<N+1; i++) {
83+
for(int j=1; j<N+1; j++) {
84+
if(g[i][j] == 0) {
85+
// 빈칸의 인접칸 중, 좋아하는 사람 수와 빈칸의 개수 계산
86+
int like = calLike(i, j, sList);
87+
int empty = calEmpty(i, j);
88+
pq.add(new Pos(i, j, like, empty));
89+
}
90+
}
91+
}
92+
// 가장 우선순위가 높은 칸 출력
93+
Pos now = pq.poll();
94+
// System.out.println(now);
95+
96+
// 위치 기록
97+
g[now.x][now.y] = num;
98+
}
99+
public static int calLike(int x, int y, List<Integer> sList) {
100+
int cnt = 0;
101+
102+
for(int i=0; i<4; i++) {
103+
int nx = x + dx[i];
104+
int ny = y + dy[i];
105+
if(!inRange(nx, ny)) continue;
106+
if(g[nx][ny] == 0) continue;
107+
108+
for(int s: sList) {
109+
if(g[nx][ny] == s) {
110+
cnt++;
111+
break;
112+
}
113+
}
114+
}
115+
116+
return cnt;
117+
}
118+
public static int calEmpty(int x, int y) {
119+
int cnt = 0;
120+
for(int i=0; i<4; i++) {
121+
int nx = x + dx[i];
122+
int ny = y + dy[i];
123+
if(!inRange(nx, ny)) continue;
124+
if(g[nx][ny] != 0) continue;
125+
126+
cnt++;
127+
}
128+
129+
return cnt;
130+
}
131+
public static int calScore() {
132+
int res = 0;
133+
134+
for(int i=1; i<N+1; i++) {
135+
for(int j=1; j<N+1; j++) {
136+
int num = g[i][j];
137+
List<Integer> sList = srr[num];
138+
int likes = calLike(i, j, sList);
139+
res += score[likes];
140+
}
141+
}
142+
143+
return res;
144+
}
145+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /