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 ae0de32

Browse files
authored
Merge pull request #357 from yeongleej/main
[27주차] 이지영
2 parents 98353c1 + 3e02baa commit ae0de32

File tree

6 files changed

+407
-0
lines changed

6 files changed

+407
-0
lines changed

‎BOJ/1000-5000번/JY_1644.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.io.*;
2+
import java.util.*;
3+
public class JY_1644 {
4+
5+
static int N;
6+
static List<Integer> pList;
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
11+
N = Integer.parseInt(br.readLine());
12+
pList = new ArrayList<>();
13+
findPrime();
14+
15+
// System.out.println(pList);
16+
17+
if(N == 1) {
18+
System.out.println(0);
19+
return;
20+
}
21+
int s = 0;
22+
int e = 0;
23+
int total = pList.get(e);
24+
int cnt = 0;
25+
while(s <= e) {
26+
if(total == N) {
27+
cnt++;
28+
total -= pList.get(s);
29+
s++;
30+
}
31+
else if(total > N) {
32+
total -= pList.get(s);
33+
s++;
34+
}
35+
else {
36+
e++;
37+
if(e >= pList.size()) break;
38+
total += pList.get(e);
39+
}
40+
}
41+
42+
System.out.println(cnt);
43+
44+
}
45+
public static void findPrime() {
46+
boolean[] isPrime = new boolean[N+1];
47+
Arrays.fill(isPrime, true);
48+
isPrime[0] = false;
49+
isPrime[1] = false;
50+
51+
52+
for(int i = 2; i <= Math.sqrt(N); i++){ // 2부터 n의 제곱근까지의 모든 수를 확인
53+
if(isPrime[i]){ // 해당수가 소수라면, 해당수를 제외한 배수들을 모두 false 처리하기
54+
for(int j = i*i; j<= N; j += i){//그 이하의 수는 모두 검사했으므로 i*i부터 시작
55+
isPrime[j] = false;
56+
}
57+
}
58+
}
59+
for(int i=2; i<N+1; i++) {
60+
if(isPrime[i]) pList.add(i);
61+
}
62+
}
63+
64+
}

‎BOJ/10001-15000번/JY_14852.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.io.*;
2+
3+
public class JY_14852 {
4+
static final int INF = 1_000_000_007;
5+
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
int N = Integer.parseInt(br.readLine());
9+
10+
11+
long[] dp = new long[N + 1];
12+
dp[0] = 1;
13+
dp[1] = 2;
14+
15+
long sum = dp[0] + dp[1];
16+
17+
for (int i = 2; i <= N; i++) {
18+
dp[i] = (2 * sum + dp[i - 2]) % INF;
19+
sum = (sum + dp[i]) % INF;
20+
}
21+
22+
System.out.println(dp[N]);
23+
}
24+
}

‎BOJ/15001-20000번/JY_18513.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class JY_18513 {
5+
6+
static int N, K;
7+
static int[] arr;
8+
static int[] dx = {-1, 1};
9+
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
StringTokenizer st = new StringTokenizer(br.readLine());
13+
14+
N = Integer.parseInt(st.nextToken());
15+
K = Integer.parseInt(st.nextToken());
16+
17+
arr = new int[N];
18+
st = new StringTokenizer(br.readLine());
19+
for (int i = 0; i < N; i++) {
20+
arr[i] = Integer.parseInt(st.nextToken());
21+
}
22+
23+
long ans = bfs();
24+
System.out.println(ans);
25+
}
26+
public static long bfs() {
27+
Queue<int[]> q = new LinkedList<>();
28+
Set<Integer> visited = new HashSet<>();
29+
30+
for(int a: arr) {
31+
q.add(new int[] {a, 0});
32+
visited.add(a);
33+
}
34+
35+
long total = 0;
36+
int cnt = 0;
37+
while(!q.isEmpty()) {
38+
int[] now = q.poll();
39+
for(int i=0; i<2; i++) {
40+
int nx = now[0] + dx[i];
41+
42+
if(visited.contains(nx)) continue;
43+
44+
total += (now[1]+1);
45+
visited.add(nx);
46+
q.add(new int[] {nx, now[1]+1});
47+
cnt++;
48+
49+
// K만큼 설치했으면 중단
50+
if(cnt == K) return total;
51+
}
52+
}
53+
54+
return total;
55+
}
56+
}
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
import java.util.*;
2+
import java.io.*;
3+
public class JY_나무박멸 {
4+
5+
static int N, M, K, C;
6+
static int[][] g;
7+
static int[][] crr;
8+
// 상 좌 하 우, 좌상 좌하 우하 우상
9+
static int[] dx = {-1, 0, 1, 0, -1, 1, 1, -1};
10+
static int[] dy = {0, -1, 0, 1, -1, -1, 1, 1};
11+
static int now;
12+
static int ans;
13+
// 제초제 뿌릴 때, 나무 상태
14+
static class State implements Comparable<State> {
15+
int x, y, tCnt;
16+
17+
public State(int x, int y, int tCnt) {
18+
super();
19+
this.x = x;
20+
this.y = y;
21+
this.tCnt = tCnt;
22+
}
23+
@Override
24+
public int compareTo(State other) {
25+
if(this.tCnt == other.tCnt) {
26+
if(this.x == other.x) {
27+
return this.y - other.y;
28+
}
29+
return this.x - other.x;
30+
}
31+
return other.tCnt - this.tCnt;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return "State [x=" + x + ", y=" + y + ", tCnt=" + tCnt + "]";
37+
}
38+
39+
}
40+
41+
public static void main(String[] args) throws IOException {
42+
// System.setIn(new FileInputStream("src/tree.txt"));
43+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
44+
StringTokenizer st = new StringTokenizer(br.readLine());
45+
46+
N = Integer.parseInt(st.nextToken());
47+
M = Integer.parseInt(st.nextToken());
48+
K = Integer.parseInt(st.nextToken());
49+
C = Integer.parseInt(st.nextToken());
50+
51+
// g: 나무 정보
52+
g = new int[N][N];
53+
// crr: 제초제 유효년도 정보
54+
crr = new int[N][N];
55+
for(int i=0; i<N; i++) {
56+
st = new StringTokenizer(br.readLine());
57+
for(int j=0; j<N; j++) {
58+
g[i][j] = Integer.parseInt(st.nextToken());
59+
}
60+
}
61+
62+
ans = 0;
63+
64+
//---- for문
65+
now = 1;
66+
while(now <= M) {
67+
System.out.println(">> NOW: "+now);
68+
// 1) 나무 성장
69+
growTree();;
70+
71+
// 2) 번식
72+
breed();
73+
74+
// 3) 제초제 뿌리기
75+
ans += spread();
76+
77+
now++;
78+
}
79+
80+
System.out.println(ans);
81+
82+
83+
}
84+
public static void print(int[][] arr) {
85+
for(int i=0; i<arr.length; i++) {
86+
System.out.println(Arrays.toString(arr[i]));
87+
}
88+
System.out.println();
89+
}
90+
public static boolean inRange(int x, int y) {
91+
return x>=0 & x<N && y>=0 && y<N;
92+
}
93+
public static void growTree() {
94+
for(int x=0; x<N; x++) {
95+
for(int y=0; y<N; y++) {
96+
if(g[x][y] <= 0) continue;
97+
// 나무
98+
// 인접 나무 수 찾기
99+
int cnt = 0;
100+
for(int d=0; d<4; d++) {
101+
int nx = x + dx[d];
102+
int ny = y + dy[d];
103+
if(!inRange(nx, ny)) continue;
104+
if(g[nx][ny] <= 0) continue;
105+
cnt++;
106+
}
107+
g[x][y] += cnt;
108+
}
109+
}
110+
}
111+
public static void breed() {
112+
// copy배열 생성
113+
int[][] tmp = new int[N][N];
114+
for(int i=0; i<N; i++) {
115+
for(int j=0; j<N; j++) {
116+
tmp[i][j] = g[i][j];
117+
}
118+
}
119+
120+
// 나무 반복
121+
for(int x=0; x<N; x++) {
122+
for(int y=0; y<N; y++) {
123+
if(g[x][y] <= 0) continue;
124+
// 나무
125+
// 인접한 가능한 4개의 칸
126+
int cnt = 0;
127+
List<int[]> oList = new ArrayList<>();
128+
for(int d=0; d<4; d++) {
129+
int nx = x + dx[d];
130+
int ny = y + dy[d];
131+
if(!inRange(nx, ny)) continue;
132+
// 벽 or 나무
133+
if(g[nx][ny] != 0) continue;
134+
// 제초제 남아 있음
135+
if(crr[nx][ny] >= now) continue;
136+
cnt++;
137+
oList.add(new int[] {nx, ny});
138+
}
139+
if(cnt == 0) continue;
140+
int bTree = g[x][y] / cnt;
141+
142+
// 나무 심기
143+
for(int[] next : oList) {
144+
tmp[next[0]][next[1]] += bTree;
145+
}
146+
147+
}
148+
}
149+
150+
g = tmp;
151+
152+
}
153+
public static int spread() {
154+
PriorityQueue<State> pq = new PriorityQueue<>();
155+
156+
for(int x=0; x<N; x++) {
157+
for(int y=0; y<N; y++) {
158+
if(g[x][y] <= 0) continue;
159+
// 나무
160+
// 박멸 시킬 수 있는 나무 수 찾기
161+
int total = g[x][y];
162+
for(int d=4; d<8; d++) {
163+
for(int k=1; k<K+1; k++) {
164+
int nx = x + dx[d] * k;
165+
int ny = y + dy[d] * k;
166+
if(!inRange(nx, ny)) continue;
167+
// 벽 or 0인 칸
168+
if(g[nx][ny] <= 0) break;
169+
total += g[nx][ny];
170+
}
171+
}
172+
pq.add(new State(x, y, total));
173+
}
174+
}
175+
176+
System.out.println("> "+pq.peek());
177+
// 더이상 나무 없음
178+
if(pq.isEmpty()) {
179+
return 0;
180+
}
181+
// 제초제 뿌릴 나무 선택 및 뿌리기
182+
State tree = pq.poll();
183+
g[tree.x][tree.y] = 0;
184+
crr[tree.x][tree.y] = now + C;
185+
for(int d=4; d<8; d++) {
186+
for(int k=1; k<K+1; k++) {
187+
int nx = tree.x + dx[d] * k;
188+
int ny = tree.y + dy[d] * k;
189+
if(!inRange(nx, ny)) continue;
190+
// 벽 or 0인 칸
191+
if(g[nx][ny] <= 0) {
192+
crr[nx][ny] = now + C;
193+
break;
194+
}
195+
g[nx][ny] = 0;
196+
crr[nx][ny] = now+C;
197+
}
198+
}
199+
200+
return tree.tCnt;
201+
}
202+
203+
}

‎Programmers/Level3/JY_72413.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.*;
2+
3+
public class JY_72413 {
4+
public int solution(int n, int s, int a, int b, int[][] fares) {
5+
int answer = 100001 * n;
6+
int INF = 100001 * n; // 모든 간선을 지날 때 최댓값
7+
int[][] g = new int[n + 1][n + 1];
8+
9+
for (int[] row : g) {
10+
Arrays.fill(row, INF);
11+
}
12+
13+
// 그래프 초기화
14+
for (int[] fare : fares) {
15+
int n1 = fare[0];
16+
int n2 = fare[1];
17+
g[n1][n2] = fare[2];
18+
g[n2][n1] = fare[2];
19+
}
20+
21+
for (int i = 1; i <= n; i++) {
22+
g[i][i] = 0;
23+
}
24+
25+
// 1. 플로이드 와샬 : 모든 정점까지의 최소거리 구하기
26+
for (int k = 1; k <= n; k++) {
27+
for (int i = 1; i <= n; i++) {
28+
for (int j = 1; j <= n; j++) {
29+
if (g[i][j] > g[i][k] + g[k][j]) {
30+
g[i][j] = g[i][k] + g[k][j];
31+
}
32+
}
33+
}
34+
}
35+
36+
// 2. 중간 지점을 거쳤을 경우도 포함
37+
for (int i = 1; i <= n; i++) {
38+
answer = Math.min(answer, g[i][s] + g[i][a] + g[i][b]);
39+
}
40+
41+
return answer;
42+
}
43+
}

0 commit comments

Comments
(0)

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