Skip to content

Commit

Permalink
230916
Browse files Browse the repository at this point in the history
  • Loading branch information
NavyHubb committed Sep 16, 2023
1 parent 4923c27 commit b4d763b
Show file tree
Hide file tree
Showing 6 changed files with 397 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/MST/BOJ_1774_우주신과의교감.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package MST;

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

public class BOJ_1774_우주신과의교감 {

static int N, M;
static int[][] locs;
static int[] start;
static class Node {
int to;
float weight;

public Node(int to, float weight) {
this.to = to;
this.weight = weight;
}
}

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

locs = new int[N+1][2];
for (int i = 1; i <= N; i++) {
st = new StringTokenizer(br.readLine());
locs[i][0] = Integer.parseInt(st.nextToken());
locs[i][1] = Integer.parseInt(st.nextToken());
}

st = new StringTokenizer(br.readLine());
start = new int[2];
start[0] = Integer.parseInt(st.nextToken());
start[1] = Integer.parseInt(st.nextToken());

System.out.printf("%f.2", solution(start));
}

static float solution(int[] start) {
float weightSum = getDist(locs[start[0]], locs[start[1]]);

PriorityQueue<Node> pq = new PriorityQueue<>((o1, o2) -> (int) (o1.weight - o2.weight));
boolean[] visited = new boolean[N+1];

visited[start[0]] = true;
visited[start[1]] = true;

for (int i = 1; i <= N; i++) {
if (visited[i]) continue;

pq.add(new Node(i, getDist(locs[start[0]], locs[i])));
}
for (int i = 1; i <= N; i++) {
if (visited[i]) continue;

pq.add(new Node(i, getDist(locs[start[1]], locs[i])));
}

int cnt = 1;
while (!pq.isEmpty()) {
Node cur = pq.poll();
if (visited[cur.to]) continue;

cnt++;
visited[cur.to] = true;
weightSum += cur.weight;

if (cnt == N) return weightSum;

for (int i = 1; i <= N; i++) {
if (visited[i]) continue;

pq.add(new Node(i, getDist(locs[cur.to], locs[i])));
}
}

return weightSum;
}

static float getDist(int[] loc1, int[] loc2) {
return (float) Math.sqrt(Math.pow(loc1[0] - loc2[0], 2) + Math.pow(loc1[1] - loc2[1], 2));
}
}
54 changes: 54 additions & 0 deletions src/binarySearch/BOJ_12015_가장긴증가하는부분수열2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package binarySearch;

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

public class BOJ_12015_가장긴증하는부분수열2 {

static int N;
static int[] arr;

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

N = Integer.parseInt(br.readLine());
arr = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();

System.out.println(solution());
}

static int solution() {
int[] LIS = new int[N];
LIS[0] = arr[0];
int lenghOfLIS = 1;

for (int i = 1; i < N; i++) {
int num = arr[i];

if (num > LIS[lenghOfLIS-1]) { // 현재 수가 LIS의 마지막 값보다 클 경우 추가해준다
lenghOfLIS++;
LIS[lenghOfLIS-1] = num;
} else {
int lo = 0;
int hi = lenghOfLIS;

while (lo < hi) {
int mid = (lo + hi) >> 1; // 2로 나누기

if (LIS[mid] < num) {
lo = mid + 1;
} else {
hi = mid;
}
}

LIS[lo] = num;
}
}

return lenghOfLIS;
}

}
60 changes: 60 additions & 0 deletions src/dp/BOJ_10942_팰린드롬.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package dp;

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

public class BOJ_10942_팰린드롬 {

static int N;
static int[] nums;
static boolean[][] dp; // dp[i][j] : i ~ j 번째 수가 팰린드롬이면 true
static StringBuilder sb = new StringBuilder();

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

N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
nums = new int[N+1];
for (int i = 1; i <= N; i++) {
nums[i] = Integer.parseInt(st.nextToken());
}
dp = new boolean[N+1][N+1];

solution();

int M = Integer.parseInt(br.readLine());
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());

sb.append(dp[from][to] ? 1 : 0).append('\n');
}

System.out.println(sb);
}

static void solution() {
for (int i = 1; i <= N; i++) {
dp[i][i] = true;
}

for (int i = 1; i <= N-1; i++) {
if (nums[i] == nums[i+1]) {
dp[i][i+1] = true;
}
}

for (int l = 2; l < N; l++) { // l: 팰린드롬인지 검사할 문자열의 시작 인덱스와 끝 인덱스의 차이. 즉, (팰린드롬의 길이 - 1)
for (int i = 1; i <= N-l; i++) { // i: 검사할 문자열의 시작 인덱스
if (nums[i] == nums[i+l] && dp[i+1][i+l-1]) {
dp[i][i+l] = true;
}
}
}
}

}
97 changes: 97 additions & 0 deletions src/graph/BOJ_16724_피리부는사나이.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package graph;

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

/**
* [풀이]
* 모든 칸에 대해 탐색한다(단, 이미 방문한 칸이라면 건너뛴다)
* 임의의 칸을 시작으로 해당 칸의 방향에 따라 칸 이동을 하며 방문처리를 한다
* 연속된 칸은 하나의 시퀀스로 카운트한다(첫 칸의 바로 다음 칸이 이미 방문한 칸이라면 카운트를 하지 않는다)
*
*/
public class BOJ_16724_피리부는사나이 {

static int N, M;
static char[][] map;
static boolean[][] visited;
static boolean[][] check;
static int cnt = 0;
static int[] di = {-1, 1, 0, 0};
static int[] dj = {0, 0, -1, 1};


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 char[N][M];
visited = new boolean[N][M];
check = new boolean[N][M];
for (int i = 0; i < N; i++) {
String str = br.readLine();
for (int j = 0; j < M; j++) {
map[i][j] = str.charAt(j);
}
}

solution();
System.out.println(cnt);
}

static void solution() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (!visited[i][j]) {
track(i, j);
}
}
}
}

static void track(int i, int j) {
visited[i][j] = true; // 현위치 방문처리

int[] next = getNext(i, j); // 다음 위치 계산
if (next[0] < 0 || next[1] < 0 || next[0] > N-1 || next[1] > M-1) return; // 범위를 벗어나면 종료

if (!visited[next[0]][next[1]]) {
track(next[0], next[1]);
} else {
if (!check[next[0]][next[1]]) {
cnt++;
}
}

check[i][j] = true;
}

static int[] getNext(int i, int j) {
int nextI = i;
int nextJ = j;
switch (map[i][j]) {
case 'U':
nextI += di[0];
nextJ += dj[0];
break;
case 'D':
nextI += di[1];
nextJ += dj[1];
break;
case 'L':
nextI += di[2];
nextJ += dj[2];
break;
case 'R':
nextI += di[3];
nextJ += dj[3];
}

return new int[]{nextI, nextJ};
}

}
55 changes: 55 additions & 0 deletions src/math/BOJ_11049_행렬곱셈순서.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package math;

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

/**
* dp[i][j] : i번째 행렬부터 j번째 행렬까지의 곱셈연산의 '최소' 횟수
*/
public class BOJ_11049_행렬곱셈순서 {

static int N;
static int[][] mats;
static int[][] dp;
static final int INF = Integer.MAX_VALUE;

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

N = Integer.parseInt(br.readLine());
mats = new int[N][2];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());

mats[i][0] = Integer.parseInt(st.nextToken());
mats[i][1] = Integer.parseInt(st.nextToken());
}
dp = new int[N][N];

System.out.println(solution());
}

static int solution() {
// 인접한 두 행렬의 곱셈연산 횟수
for (int i = 0; i < N-1; i++) {
dp[i][i+1] = mats[i][0] * mats[i][1] * mats[i+1][1];
}

for (int gap = 2; gap < N; gap++) { // gap: (하나의 괄호에 묶일 행렬의 갯수) - 1
for (int i = 0; i+gap < N; i++) { // i: 괄호의 시작 행렬 인덱스
int j = i + gap; // j: 괄호의 마지막 행렬 인덱스
dp[i][j] = INF;

for (int k = i; k < j; k++) {
dp[i][j] = Math.min(dp[i][j],
dp[i][k] + dp[k+1][j] + (mats[i][0] * mats[k][1] * mats[j][1]));
}
}
}

return dp[0][N-1];
}
}
Loading

0 comments on commit b4d763b

Please sign in to comment.