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

허준혁 / 10월 2주차 / 목 & 10월 3주차 / 월, 목 #273

Merged
merged 10 commits into from
Oct 19, 2023
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
44 changes: 44 additions & 0 deletions HuhJunHyeok/boj/boj15989.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* [boj] 15989. 1, 2, 3 더하기 4
*/
public class boj15989 {
/**
* n: 만들려고 하는 정수
*/
static int n;
/**
* dp: dp 배열. dp[k][n]은 현재 n번 인덱스에서 사용하는 정수를 더할 때 k값을 만들 수 있는 경우의 수.
* 10_001 ==> 정수 n의 값이 10_000 이하의 양수
* 3 ==> 1~3의 3가지 정수의 합. 0인덱스 1, 1인덱스 2, 2인덱스 3
*/
static int[][] dp = new int[10_001][3];

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();

dp[1][0] = 1; // 1
dp[2][0] = 1; // 1 + 1
dp[2][1] = 1; // 2
dp[3][0] = 1; // 1 + 1 + 1
dp[3][1] = 1; // 1 + 2
dp[3][2] = 1; // 3
for(int i = 4; i <= 10_000; i++) {
dp[i][0] = dp[i - 1][0];
dp[i][1] = dp[i - 2][0] + dp[i - 2][1];
dp[i][2] = dp[i - 3][0] + dp[i - 3][1] + dp[i - 3][2];
}

int T = Integer.parseInt(br.readLine());
while(T-- > 0) {
n = Integer.parseInt(br.readLine());

sb.append(dp[n][0] + dp[n][1] + dp[n][2]).append("\n");
}

System.out.print(sb.toString());
}
}
126 changes: 126 additions & 0 deletions HuhJunHyeok/boj/boj16197.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;

/**
* [boj] 16197. 두 동전
*/
public class boj16197 {
static class Coin {
int r;
int c;

public Coin(int r, int c) {
this.r = r;
this.c = c;
}
}
static class CoinsPositionCount {
int r1;
int c1;
int r2;
int c2;
int pressCount;

public CoinsPositionCount(int r1, int c1, int r2, int c2, int pressCount) {
this.r1 = r1;
this.c1 = c1;
this.r2 = r2;
this.c2 = c2;
this.pressCount = pressCount;
}
}
/**
* N: 보드의 세로 크기, M: 보드의 가로 크기
*/
static int N, M, minPressCount = -1;
/**
* map: N*M 크기의 보드
*/
static char[][] map;
/**
* coins: 코인
*/
static Coin[] coins = new Coin[2];
static boolean[][][][] visited;
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};

public static void main(String[] args) throws Exception {
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 char[N][M];
int coinIdx = 0;
for(int i = 0; i < N; i++) {
String line = br.readLine();
for(int j = 0; j < M; j++) {
map[i][j] = line.charAt(j);

if(map[i][j] == 'o') {
coins[coinIdx++] = new Coin(i, j);
}
}
}

bfs();

System.out.println(minPressCount);
}

public static void bfs() {
Queue<CoinsPositionCount> queue = new ArrayDeque<>();
queue.offer(new CoinsPositionCount(coins[0].r, coins[0].c, coins[1].r, coins[1].c, 0));

visited = new boolean[N][M][N][M];
visited[coins[0].r][coins[0].c][coins[1].r][coins[1].c] = true;

while(!queue.isEmpty()) {
CoinsPositionCount now = queue.poll();

if(now.pressCount >= 10) {
return;
}

for(int i = 0; i < 4; i++) {
int nextR1 = now.r1 + dr[i];
int nextC1 = now.c1 + dc[i];
int nextR2 = now.r2 + dr[i];
int nextC2 = now.c2 + dc[i];

int dropCount = 0; // 떨어진 않은 동전의 수
if(isInMap(nextR1, nextC1) && map[nextR1][nextC1] == '#') {
nextR1 = now.r1;
nextC1 = now.c1;
}
if(!isInMap(nextR1, nextC1)) {
dropCount++;
}
if(isInMap(nextR2, nextC2) && map[nextR2][nextC2] == '#') {
nextR2 = now.r2;
nextC2 = now.c2;
}
if(!isInMap(nextR2, nextC2)) {
dropCount++;
}

if(dropCount == 1) {
minPressCount = now.pressCount + 1;
return;
}
if(dropCount == 0 && !visited[nextR1][nextC1][nextR2][nextC2]) {
queue.offer(new CoinsPositionCount(nextR1, nextC1, nextR2, nextC2, now.pressCount + 1));
visited[nextR1][nextC1][nextR2][nextC2] = true;
}
}
}
}

public static boolean isInMap(int r, int c) {
return 0 <= r && r < N && 0 <= c && c < M;
}
}
128 changes: 128 additions & 0 deletions HuhJunHyeok/boj/boj16234.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Queue;
import java.util.StringTokenizer;

/**
* [boj] 16234. 인구 이동
*/
public class boj16234 {
/**
* Country: 나라를 의미하는 class
*/
static class Country {
int r, c;

public Country(int r, int c) {
this.r = r;
this.c = c;
}
}
/**
* N: 땅의 한 라인의 크기, L: 국경선 여는 인구 차이 하한값, R: 국경선 여는 인구 차이 상한값
* days: 인구 이동이 며칠 동안 발생하는지, openBoarderCountryCnt: 국경이 열린 나라의 인구 수 합
*/
static int N, L, R, days, openBoarderCountryPopulation;
/**
* map: 땅을 나타내는 지도
*/
static int[][] map;
static boolean possibleToMove;
static boolean[][] visited;
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
static ArrayList<Country> countryList = new ArrayList<>();
static Queue<Country> countryQueue = new ArrayDeque<>();

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");

N = Integer.parseInt(st.nextToken());
L = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());

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());
}
}

movePopulation();

System.out.println(days);
}

/**
* bfs를 통해 인구 이동이 가능한지 파악하여 인구 이동이 불가능 할 때까지 인구 이동 진행.
*/
public static void movePopulation() {
while(true) { // 인구 이동이 불가능할 때까지 반복
possibleToMove = false;
visited = new boolean[N][N];

for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(!visited[i][j]) {
// countryQueue = new ArrayDeque<>();
// countryList = new ArrayList<>();
countryList.clear();
openBoarderCountryPopulation = 0;
bfs(i, j);

if(countryList.size() > 1) { // 국경이 열린 나라가 두 나라 이상인 경우 인구 이동이 일어남.
possibleToMove = true;

int avgPopulation = openBoarderCountryPopulation / countryList.size();
for(Country country: countryList) {
map[country.r][country.c] = avgPopulation;
}
}
}
}
}

if(!possibleToMove) {
return;
}
days++;
}
}

public static void bfs(int r, int c) {
countryQueue.offer(new Country(r, c));
countryList.add(new Country(r, c));

visited[r][c] = true;
openBoarderCountryPopulation = map[r][c];

while(!countryQueue.isEmpty()) {
Country nowCountry = countryQueue.poll();

for(int i = 0; i < 4; i ++) {
int nextR = nowCountry.r + dr[i];
int nextC = nowCountry.c + dc[i];

if(!isInMap(nextR, nextC) || visited[nextR][nextC]) {
continue;
}

int diff = Math.abs(map[nowCountry.r][nowCountry.c] - map[nextR][nextC]);
if(L <= diff && diff <= R) {
countryQueue.offer(new Country(nextR, nextC));
countryList.add(new Country(nextR, nextC));

visited[nextR][nextC] = true;
openBoarderCountryPopulation += map[nextR][nextC];
}
}
}
}

public static boolean isInMap(int r, int c) {
return 0 <= r && r < N && 0 <= c && c < N;
}
}
58 changes: 58 additions & 0 deletions HuhJunHyeok/boj/boj16719.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* [boj] 16719. ZOAC
*/
public class boj16719 {
/**
* text: 주어진 문자열
*/
static String text;
/**
* textLength: 문자열의 길이
*/
static int textLength;
/**
* check: 문자열에서 각 index에 해당하는 문자(글자)를 사용 여부 체크.
*/
static boolean[] check;
static StringBuilder sb = new StringBuilder();

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

text = br.readLine();
textLength = text.length();
check = new boolean[textLength];

recur(0, textLength - 1);

System.out.print(sb.toString());
}

public static void recur(int leftIdx, int rightIdx) {
if(leftIdx > rightIdx) {
return;
}

int idx = leftIdx;
for(int i = leftIdx; i <= rightIdx; i++) { // leftIdx ~ rightIdx에서 사전순 가장 빠른 글자 찾기
if(text.charAt(i) < text.charAt(idx)) {
idx = i;
}
}
check[idx] = true;

for(int i = 0; i < textLength; i++) {
if(check[i]) {
sb.append(text.charAt(i));
}
}
sb.append("\n");

// 사전순 가장 빠른 글자를 기준으로 오른쪽, 왼쪽 순서로 확인.
recur(idx + 1, rightIdx);
recur(leftIdx, idx - 1);
}
}
Loading