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

[27주차] 손지민 #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
KodaHye merged 4 commits into main from jimin
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions BOJ/1000-5000번/JM_1644.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

public class JM_1644 {
static int N;
static List<Integer> primeNumbers;

private static void findPrimeNumber() {
boolean[] isPrime = new boolean[N + 1];
Arrays.fill(isPrime, true);

for (int i = 2; i <= Math.sqrt(N); i++) {
if(!isPrime[i]) continue;
for(int j = i * i; j <= N; j += i) {
isPrime[j] = false;
}
}

for (int i = 2; i <= N; i++) {
if(isPrime[i]) primeNumbers.add(i);
}
}

private static int solve() {
primeNumbers = new ArrayList<>();
findPrimeNumber();

int count = 0;
int lo = 0, hi = 0;
int sum = 0;
while (true) {
if(sum < N) {
if(hi == primeNumbers.size()) break;
sum += primeNumbers.get(hi++);
}
else {
sum -= primeNumbers.get(lo++);
}

if(sum == N) count++;
}
return count;
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
System.out.println(solve());
}
}
35 changes: 35 additions & 0 deletions BOJ/10001-15000번/JM_14852.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class JM_14852 {
static int N;
static final int MOD = 1_000_000_007;

private static long solve() {
if(N == 1) return 2;
else if(N == 2) return 7;

long[] dp = new long[N + 1];
long[] sum = new long[N + 1];

dp[1] = 2;
dp[2] = 7;
sum[2] = 1;

for (int i = 3; i <= N; i++) {
sum[i] = (dp[i - 3] + sum[i - 1]) % MOD;
dp[i] = (2 * dp[i - 1] + 3 * dp[i - 2] + 2 * sum[i]) % MOD;
}

return dp[N];
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
System.out.println(solve());
}
}
163 changes: 163 additions & 0 deletions CodeTree/2021-2022년/JM_나무박멸.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class JM_나무박멸 {
static int N;
static int M; // 박멸이 진행되는 년 수
static int K; // 제초제의 확산 범위 K
static int C; // 제초제가 남아있는 년 수 C
static int[][] map; // 0(빈칸), -1(벽)
static final int[][] DIR = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
static int[][] herbicide;

private static boolean inRange(int y, int x) {
return 0 <= y && y < N && 0 <= x && x < N;
}

private static void reduceHerbicide() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(herbicide[i][j] <= 0) continue;
herbicide[i][j] -= 1;
}
}
}

private static int[] findMaxRemovedTreePos() {
int[] pos = new int[3];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(map[i][j] <= 0) continue;

int removedTree = map[i][j];
for (int d = 4; d < 8; d++) {
int ny = i;
int nx = j;
int k = 0;
while (k++ < K) {
ny += DIR[d][0];
nx += DIR[d][1];
if(!inRange(ny, nx) || map[ny][nx] == 0 || map[ny][nx] == -1) break;
removedTree += map[ny][nx];
}
}

if(removedTree > pos[2]) {
pos[0] = i;
pos[1] = j;
pos[2] = removedTree;
}
}
}
return pos;
}

private static int sprayHerbicide() {
int[] pos = findMaxRemovedTreePos();
int y = pos[0];
int x = pos[1];
int removedTree = pos[2];

map[y][x] = 0;
herbicide[y][x] = C + 1;
for (int d = 4; d < 8; d++) {
int ny = y;
int nx = x;
int k = 0;
while (k++ < K) {
ny += DIR[d][0];
nx += DIR[d][1];
if(!inRange(ny, nx)) break;
herbicide[ny][nx] = C + 1;
if(map[ny][nx] <= 0) break;
map[ny][nx] = 0;
}
}

return removedTree;
}

private static int countReproduction(int y, int x) {
int cnt = 0;
for (int k = 0; k < 4; k++) {
int ny = y + DIR[k][0];
int nx = x + DIR[k][1];
if(!inRange(ny, nx) || map[ny][nx] != 0 || herbicide[ny][nx] > 0) continue;
cnt++;
}
return cnt == 0 ? 0 : map[y][x] / cnt;
}

private static void reproduce() {
int[][] tmp = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
tmp[i][j] = map[i][j];
}
}

for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(map[i][j] <= 0) continue;

int reproduction = countReproduction(i, j);
if(reproduction == 0) continue;
for (int d = 0; d < 4; d++) {
int ny = i + DIR[d][0];
int nx = j + DIR[d][1];
if(!inRange(ny, nx) || map[ny][nx] != 0 || herbicide[ny][nx] > 0) continue;
tmp[ny][nx] += reproduction;
}
}
}
map = tmp;
}

private static void growUp() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(map[i][j] <= 0) continue;
for (int d = 0; d < 4; d++) {
int ny = i + DIR[d][0];
int nx = j + DIR[d][1];
if(!inRange(ny, nx) || map[ny][nx] <= 0) continue;
map[i][j] += 1;
}
}
}
}

private static int solve() {
int removedTree = 0;

while (M-- > 0) {
reduceHerbicide();
growUp();
reproduce();
removedTree += sprayHerbicide();
}

return removedTree;
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());

herbicide = new int[N][N];
map = new int[N][N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
System.out.println(solve());
}
}
50 changes: 50 additions & 0 deletions Programmers/Level3/JM_ 72413.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
public class JM_72413 {
static final int INF = Integer.MAX_VALUE;
static int[][] edge;

public static int floydWarshall(int n, int s, int a, int b) {
for(int k = 1; k <= n; k++) {
for(int i = 1; i <= n; i++) {
if(edge[i][k] == INF) continue;
for(int j = 1; j <= n; j++) {
if(edge[k][j] == INF) continue;
if(edge[i][j] > edge[i][k] + edge[k][j]) {
edge[i][j] = edge[i][k] + edge[k][j];
}
}
}
}

int answer = Math.min(INF, edge[s][a] + edge[s][b]);
for(int i = 1; i <= n; i++) {
if(i == s) continue;
int cost = Math.min(INF, edge[s][i] + edge[i][a] + edge[i][b]);
answer = Math.min(answer, cost);
}

return answer;
Comment on lines +6 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

최단 경로를 구하는거여서 플로이드워셜도 사용할 수 있군요!! 잊고 있었는데, 참고하고 갑니다!

jmxx219 reacted with thumbs up emoji
}

public static int solution(int n, int s, int a, int b, int[][] fares) {
edge = new int[n + 1][n + 1];

for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(i != j) edge[i][j] = INF;
}
}

for(int[] fare : fares) {
edge[fare[0]][fare[1]] = fare[2];
edge[fare[1]][fare[0]] = fare[2];
}

return floydWarshall(n, s, a, b);
}

public static void main(String[] args) {
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}}));
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}}));
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}}));
}
}

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