Skip to content
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

이상화 / 4기 3주차 / 3문제 #134

Merged
merged 3 commits into from
Oct 11, 2024
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 sanghwa/BOJ/BOJ1106.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package BOJ;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class BOJ1106 {

static long[] dp;
static int[][] cities;
static int target;
static int COST = 0;
static int VALUE = 1;

public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer token = new StringTokenizer(buffer.readLine());

target = Integer.parseInt(token.nextToken());
int cityNum = Integer.parseInt(token.nextToken());
cities = new int[cityNum][2];

for (int i = 0; i < cityNum; i++) {
token = new StringTokenizer(buffer.readLine());
int cost = Integer.parseInt(token.nextToken());
int value = Integer.parseInt(token.nextToken());
cities[i][COST] = cost;
cities[i][VALUE] = value;
}
dp = new long[target + 100];
Arrays.fill(dp, Long.MAX_VALUE);
dp[0] = 0;

doDp();
long result = Long.MAX_VALUE;
for (int i = target; i < dp.length; i++) {
result = Math.min(result, dp[i]);
}
System.out.println(result);
}

static void doDp() {
for (int i = 1; i < dp.length; i++) {
for (int[] city : cities) {
int value = city[VALUE];
int cost = city[COST];
if (i < value || dp[i - value] == Long.MAX_VALUE) {
continue;
}
dp[i] = Math.min(dp[i], dp[i - value] + cost);
}
}
}
}
44 changes: 44 additions & 0 deletions sanghwa/BOJ/BOJ1443.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package BOJ;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ1443 {

static int maxTarget;
static int operationCnt;
static int result = -1;

public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer token = new StringTokenizer(buffer.readLine());

int power = Integer.parseInt(token.nextToken());
maxTarget = (int)Math.pow(10, power);
operationCnt = Integer.parseInt(token.nextToken());

if (operationCnt == 0) {
System.out.println(1);
return;
}

backTracking(0, 1, 9);
System.out.println(result);
}

static void backTracking(int cnt, int currNum, int maxMultiplier) {
if (cnt == operationCnt) {
result = Math.max(currNum, result);
return;
}

for (int i = maxMultiplier; i >= 2; i--) {
long nextNum = (long)currNum * i;
if (nextNum >= maxTarget) continue;
if (nextNum * Math.pow(9, operationCnt - cnt - 1) <= result) break; // 최선의 결과가 현재 결과보다 작으면 중지
backTracking(cnt + 1, (int)nextNum, i); // maxMultiplier값을 다르게 전달하여 4*3, 3*4와 같은 중복연산 방지
}
}
}
103 changes: 103 additions & 0 deletions sanghwa/BOJ/BOJ14948.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package BOJ;

import java.io.*;
import java.util.*;

public class BOJ14948 {
static int N, M;
static int[][] map;
static int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

static class Point {
int x, y, maxLevel;
boolean usedItem;

Point(int x, int y, int maxLevel, boolean usedItem) {
this.x = x;
this.y = y;
this.maxLevel = maxLevel;
this.usedItem = usedItem;
}
}

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());
map = new int[N][M];

int left = 0, right = 0;
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
right = Math.max(right, map[i][j]);
}
}

int result = binarySearch(left, right);
System.out.println(result);
}

// 이분탐색
static int binarySearch(int left, int right) {
int result = right;
while (left <= right) {
int mid = left + (right - left) / 2;
if (canReach(mid)) {
result = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return result;
}

// 문제생각의 전환: 레벨을 기준으로 도착할 수 있는지 판단한다. BFS
static boolean canReach(int level) {
Queue<Point> queue = new LinkedList<>();
boolean[][][] visited = new boolean[N][M][2];
queue.offer(new Point(0, 0, map[0][0], false));
visited[0][0][0] = true;

while (!queue.isEmpty()) {
Point cur = queue.poll();

if (cur.x == N - 1 && cur.y == M - 1) {
return true;
}

for (int[] dir : dirs) {
int nx = cur.x + dir[0];
int ny = cur.y + dir[1];

if (nx >= 0 && nx < N && ny >= 0 && ny < M) {
int newMaxLevel = Math.max(cur.maxLevel, map[nx][ny]);
if (newMaxLevel <= level && !visited[nx][ny][cur.usedItem ? 1 : 0]) {
queue.offer(new Point(nx, ny, newMaxLevel, cur.usedItem));
visited[nx][ny][cur.usedItem ? 1 : 0] = true;
}
}
}

if (!cur.usedItem) {
for (int[] dir : dirs) {
int nx = cur.x + dir[0] * 2;
int ny = cur.y + dir[1] * 2;

if (nx >= 0 && nx < N && ny >= 0 && ny < M && !visited[nx][ny][1]) {
int newMaxLevel = Math.max(cur.maxLevel, map[nx][ny]);
if (newMaxLevel <= level) {
queue.offer(new Point(nx, ny, newMaxLevel, true));
visited[nx][ny][1] = true;
}
}
}
}
}

return false;
}
}