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 98353c1

Browse files
authored
Merge pull request #359 from GreatAlgorithm-Study/jimin
[27주차] 손지민
2 parents 8c325e0 + fde73ec commit 98353c1

File tree

4 files changed

+304
-0
lines changed

4 files changed

+304
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.StringTokenizer;
8+
9+
public class JM_1644 {
10+
static int N;
11+
static List<Integer> primeNumbers;
12+
13+
private static void findPrimeNumber() {
14+
boolean[] isPrime = new boolean[N + 1];
15+
Arrays.fill(isPrime, true);
16+
17+
for (int i = 2; i <= Math.sqrt(N); i++) {
18+
if(!isPrime[i]) continue;
19+
for(int j = i * i; j <= N; j += i) {
20+
isPrime[j] = false;
21+
}
22+
}
23+
24+
for (int i = 2; i <= N; i++) {
25+
if(isPrime[i]) primeNumbers.add(i);
26+
}
27+
}
28+
29+
private static int solve() {
30+
primeNumbers = new ArrayList<>();
31+
findPrimeNumber();
32+
33+
int count = 0;
34+
int lo = 0, hi = 0;
35+
int sum = 0;
36+
while (true) {
37+
if(sum < N) {
38+
if(hi == primeNumbers.size()) break;
39+
sum += primeNumbers.get(hi++);
40+
}
41+
else {
42+
sum -= primeNumbers.get(lo++);
43+
}
44+
45+
if(sum == N) count++;
46+
}
47+
return count;
48+
}
49+
50+
public static void main(String[] args) throws IOException {
51+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
52+
StringTokenizer st = new StringTokenizer(br.readLine());
53+
N = Integer.parseInt(st.nextToken());
54+
System.out.println(solve());
55+
}
56+
}

‎BOJ/10001-15000번/JM_14852.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
6+
public class JM_14852 {
7+
static int N;
8+
static final int MOD = 1_000_000_007;
9+
10+
private static long solve() {
11+
if(N == 1) return 2;
12+
else if(N == 2) return 7;
13+
14+
long[] dp = new long[N + 1];
15+
long[] sum = new long[N + 1];
16+
17+
dp[1] = 2;
18+
dp[2] = 7;
19+
sum[2] = 1;
20+
21+
for (int i = 3; i <= N; i++) {
22+
sum[i] = (dp[i - 3] + sum[i - 1]) % MOD;
23+
dp[i] = (2 * dp[i - 1] + 3 * dp[i - 2] + 2 * sum[i]) % MOD;
24+
}
25+
26+
return dp[N];
27+
}
28+
29+
public static void main(String[] args) throws IOException {
30+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
31+
StringTokenizer st = new StringTokenizer(br.readLine());
32+
N = Integer.parseInt(st.nextToken());
33+
System.out.println(solve());
34+
}
35+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
6+
public class JM_나무박멸 {
7+
static int N;
8+
static int M; // 박멸이 진행되는 년 수
9+
static int K; // 제초제의 확산 범위 K
10+
static int C; // 제초제가 남아있는 년 수 C
11+
static int[][] map; // 0(빈칸), -1(벽)
12+
static final int[][] DIR = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
13+
static int[][] herbicide;
14+
15+
private static boolean inRange(int y, int x) {
16+
return 0 <= y && y < N && 0 <= x && x < N;
17+
}
18+
19+
private static void reduceHerbicide() {
20+
for (int i = 0; i < N; i++) {
21+
for (int j = 0; j < N; j++) {
22+
if(herbicide[i][j] <= 0) continue;
23+
herbicide[i][j] -= 1;
24+
}
25+
}
26+
}
27+
28+
private static int[] findMaxRemovedTreePos() {
29+
int[] pos = new int[3];
30+
for (int i = 0; i < N; i++) {
31+
for (int j = 0; j < N; j++) {
32+
if(map[i][j] <= 0) continue;
33+
34+
int removedTree = map[i][j];
35+
for (int d = 4; d < 8; d++) {
36+
int ny = i;
37+
int nx = j;
38+
int k = 0;
39+
while (k++ < K) {
40+
ny += DIR[d][0];
41+
nx += DIR[d][1];
42+
if(!inRange(ny, nx) || map[ny][nx] == 0 || map[ny][nx] == -1) break;
43+
removedTree += map[ny][nx];
44+
}
45+
}
46+
47+
if(removedTree > pos[2]) {
48+
pos[0] = i;
49+
pos[1] = j;
50+
pos[2] = removedTree;
51+
}
52+
}
53+
}
54+
return pos;
55+
}
56+
57+
private static int sprayHerbicide() {
58+
int[] pos = findMaxRemovedTreePos();
59+
int y = pos[0];
60+
int x = pos[1];
61+
int removedTree = pos[2];
62+
63+
map[y][x] = 0;
64+
herbicide[y][x] = C + 1;
65+
for (int d = 4; d < 8; d++) {
66+
int ny = y;
67+
int nx = x;
68+
int k = 0;
69+
while (k++ < K) {
70+
ny += DIR[d][0];
71+
nx += DIR[d][1];
72+
if(!inRange(ny, nx)) break;
73+
herbicide[ny][nx] = C + 1;
74+
if(map[ny][nx] <= 0) break;
75+
map[ny][nx] = 0;
76+
}
77+
}
78+
79+
return removedTree;
80+
}
81+
82+
private static int countReproduction(int y, int x) {
83+
int cnt = 0;
84+
for (int k = 0; k < 4; k++) {
85+
int ny = y + DIR[k][0];
86+
int nx = x + DIR[k][1];
87+
if(!inRange(ny, nx) || map[ny][nx] != 0 || herbicide[ny][nx] > 0) continue;
88+
cnt++;
89+
}
90+
return cnt == 0 ? 0 : map[y][x] / cnt;
91+
}
92+
93+
private static void reproduce() {
94+
int[][] tmp = new int[N][N];
95+
for (int i = 0; i < N; i++) {
96+
for (int j = 0; j < N; j++) {
97+
tmp[i][j] = map[i][j];
98+
}
99+
}
100+
101+
for (int i = 0; i < N; i++) {
102+
for (int j = 0; j < N; j++) {
103+
if(map[i][j] <= 0) continue;
104+
105+
int reproduction = countReproduction(i, j);
106+
if(reproduction == 0) continue;
107+
for (int d = 0; d < 4; d++) {
108+
int ny = i + DIR[d][0];
109+
int nx = j + DIR[d][1];
110+
if(!inRange(ny, nx) || map[ny][nx] != 0 || herbicide[ny][nx] > 0) continue;
111+
tmp[ny][nx] += reproduction;
112+
}
113+
}
114+
}
115+
map = tmp;
116+
}
117+
118+
private static void growUp() {
119+
for (int i = 0; i < N; i++) {
120+
for (int j = 0; j < N; j++) {
121+
if(map[i][j] <= 0) continue;
122+
for (int d = 0; d < 4; d++) {
123+
int ny = i + DIR[d][0];
124+
int nx = j + DIR[d][1];
125+
if(!inRange(ny, nx) || map[ny][nx] <= 0) continue;
126+
map[i][j] += 1;
127+
}
128+
}
129+
}
130+
}
131+
132+
private static int solve() {
133+
int removedTree = 0;
134+
135+
while (M-- > 0) {
136+
reduceHerbicide();
137+
growUp();
138+
reproduce();
139+
removedTree += sprayHerbicide();
140+
}
141+
142+
return removedTree;
143+
}
144+
145+
public static void main(String[] args) throws IOException {
146+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
147+
StringTokenizer st = new StringTokenizer(br.readLine());
148+
N = Integer.parseInt(st.nextToken());
149+
M = Integer.parseInt(st.nextToken());
150+
K = Integer.parseInt(st.nextToken());
151+
C = Integer.parseInt(st.nextToken());
152+
153+
herbicide = new int[N][N];
154+
map = new int[N][N];
155+
for (int i = 0; i < N; i++) {
156+
st = new StringTokenizer(br.readLine());
157+
for (int j = 0; j < N; j++) {
158+
map[i][j] = Integer.parseInt(st.nextToken());
159+
}
160+
}
161+
System.out.println(solve());
162+
}
163+
}

‎Programmers/Level3/JM_ 72413.java‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
public class JM_72413 {
2+
static final int INF = Integer.MAX_VALUE;
3+
static int[][] edge;
4+
5+
public static int floydWarshall(int n, int s, int a, int b) {
6+
for(int k = 1; k <= n; k++) {
7+
for(int i = 1; i <= n; i++) {
8+
if(edge[i][k] == INF) continue;
9+
for(int j = 1; j <= n; j++) {
10+
if(edge[k][j] == INF) continue;
11+
if(edge[i][j] > edge[i][k] + edge[k][j]) {
12+
edge[i][j] = edge[i][k] + edge[k][j];
13+
}
14+
}
15+
}
16+
}
17+
18+
int answer = Math.min(INF, edge[s][a] + edge[s][b]);
19+
for(int i = 1; i <= n; i++) {
20+
if(i == s) continue;
21+
int cost = Math.min(INF, edge[s][i] + edge[i][a] + edge[i][b]);
22+
answer = Math.min(answer, cost);
23+
}
24+
25+
return answer;
26+
}
27+
28+
public static int solution(int n, int s, int a, int b, int[][] fares) {
29+
edge = new int[n + 1][n + 1];
30+
31+
for(int i = 1; i <= n; i++) {
32+
for(int j = 1; j <= n; j++) {
33+
if(i != j) edge[i][j] = INF;
34+
}
35+
}
36+
37+
for(int[] fare : fares) {
38+
edge[fare[0]][fare[1]] = fare[2];
39+
edge[fare[1]][fare[0]] = fare[2];
40+
}
41+
42+
return floydWarshall(n, s, a, b);
43+
}
44+
45+
public static void main(String[] args) {
46+
System.out.println(solution(6, 4, 6, 2, new int[][] {{4, 1, 10}, {3, 5, 24}, {5, 6, 2}, {3, 1, 41}, {5, 1, 24}, {4, 6, 50}, {2, 4, 66}, {2, 3, 22}, {1, 6, 25}}));
47+
System.out.println(solution(7, 3, 4, 1, new int[][] {{5, 7, 9}, {4, 6, 4}, {3, 6, 1}, {3, 2, 3}, {2, 1, 6}}));
48+
System.out.println(solution(6, 4, 5, 6, new int[][] {{2,6,6}, {6,3,7}, {4,6,7}, {6,5,11}, {2,5,12}, {5,3,20}, {2,4,8}, {4,3,9}}));
49+
}
50+
}

0 commit comments

Comments
(0)

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