Skip to content
Open
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
53 changes: 53 additions & 0 deletions baeck/BOJ_14888.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package baeck;

import java.util.Scanner;
//연산자 끼워넣기
public class BOJ_14888 {
static int N;
static int[] number;
static int[] operator = new int[4];
static int max = Integer.MIN_VALUE;
static int min = Integer.MAX_VALUE;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
number = new int[N];

for(int i = 0; i < N; i++) {
number[i] = sc.nextInt();
}

for(int i = 0; i < 4; i++) {
operator[i] = sc.nextInt();
}

dfs(number[0], 1);

System.out.println(max);
System.out.println(min);
}

static void dfs(int num, int idx) {
if(idx == N) {
max = Math.max(max, num);
min = Math.min(min, num);
return;
}

for(int i = 0; i < 4; i++) {
if(operator[i] > 0) {
operator[i]--;

switch(i) {
case 0: dfs(num + number[idx], idx + 1); break;
case 1: dfs(num - number[idx], idx + 1); break;
case 2: dfs(num * number[idx], idx + 1); break;
case 3: dfs(num / number[idx], idx + 1); break;
}

operator[i]++;
}
}
}
}
75 changes: 75 additions & 0 deletions baeck/BOJ_14889.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package baeck;

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

//스타트와 링크
public class BOJ_14889 {
static int N;
static int[][] arr;
static boolean[] visit;
static int Min = Integer.MAX_VALUE;

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

N = Integer.parseInt(br.readLine());
arr = new int[N][N];
visit = new boolean[N];

for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), " ");
for(int j = 0; j < N; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}

dfs(0, 0);
System.out.println(Min);
}

static void dfs(int idx, int count) {
if(count == N / 2) {
diff();
return;
}

for(int i = idx; i < N; i++) {
if(!visit[i]) {
visit[i] = true;
dfs(i + 1, count + 1);
visit[i] = false;
}
}
}

static void diff() {
int team_start = 0;
int team_link = 0;

for(int i = 0; i < N - 1; i++) {
for(int j = i + 1; j < N; j++) {
if(visit[i] == true && visit[j] == true) {
team_start += arr[i][j];
team_start += arr[j][i];
}
else if(visit[i] == false && visit[j] == false) {
team_link += arr[i][j];
team_link += arr[j][i];
}
}
}

int val = Math.abs(team_start - team_link);

if(val == 0) {
System.out.println(val);
System.exit(0);
}

Min = Math.min(val, Min);
}
}
50 changes: 50 additions & 0 deletions baeck/BOJ_1932.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package baeck;

import java.util.Scanner;

//정수 삼각형
public class BOJ_1932 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] triangle = new int[n][n];
int[][] dp = new int[n][n];

// 삼각형의 각 숫자를 입력 받습니다.
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
triangle[i][j] = scanner.nextInt();
}
}

// dp의 첫 번째 값은 삼각형의 첫 번째 값으로 초기화합니다.
dp[0][0] = triangle[0][0];

for (int i = 1; i < n; i++) {
for (int j = 0; j <= i; j++) {
// 가장 왼쪽에 위치한 경우
if (j == 0) {
dp[i][j] = dp[i - 1][j] + triangle[i][j];
}
// 가장 오른쪽에 위치한 경우
else if (j == i) {
dp[i][j] = dp[i - 1][j - 1] + triangle[i][j];
}
// 그 외의 경우
else {
dp[i][j] = Math.max(dp[i - 1][j - 1], dp[i - 1][j]) + triangle[i][j];
}
}
}

// dp의 마지막 행에서 가장 큰 값 찾기
int max = dp[n - 1][0];
for (int i = 1; i < n; i++) {
max = Math.max(max, dp[n - 1][i]);
}

System.out.println(max);

scanner.close();
}
}
32 changes: 32 additions & 0 deletions baeck/BOJ_20291.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package baeck;

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

//파일 정리
public class BOJ_20291 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
HashMap<String, Integer> input = new HashMap<>();
for (int i = 0; i < N; i++) {
String temp = br.readLine();
String[] tmp = temp.split("\\.");
String extension = tmp[tmp.length - 1];

if(input.containsKey(extension)) {
input.put(extension, input.get(extension) + 1);
} else {
input.put(extension, 1);
}
}
List<String> keyList = new ArrayList<>(input.keySet());
Collections.sort(keyList);
for (String key: keyList){
System.out.println(key + " " + input.get(key));
}

}
}
79 changes: 79 additions & 0 deletions baeck/DFSAndBFS/BOJ_17086.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package baeck.DFSAndBFS;

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

//아기상어2
public class BOJ_17086 {
private static int[][] map;
private static int[] dx = {-1, -1, -1, 0, 1, 1, 1, 0};
private static int[] dy = {-1, 0, 1, 1, 1, 0, -1, -1};
private static int N, M;

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];

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

int answer = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
answer = Math.max(answer, bfs(i, j));
}
}

System.out.println(answer);
}

private static int bfs(int startX, int startY) {
Queue<int[]> queue = new LinkedList<>();
boolean[][] visited = new boolean[N][M];
visited[startX][startY] = true;
queue.offer(new int[]{startX, startY, 0});

while (!queue.isEmpty()) {
int[] current = queue.poll();
int x = current[0];
int y = current[1];
int distance = current[2];

// 상어를 만난 경우, 현재까지의 거리 반환
if (map[x][y] == 1) {
return distance;
}

// 상하좌우와 대각선 방향으로 이동
for (int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];

// 유효한 위치이고 아직 방문하지 않은 경우
if (isValid(nx, ny) && !visited[nx][ny]) {
visited[nx][ny] = true;
queue.offer(new int[]{nx, ny, distance + 1});
}
}
}

return 0;
}

// 주어진 위치가 유효한지 확인하는 함수
private static boolean isValid(int x, int y) {
return x >= 0 && x < N && y >= 0 && y < M;
}
}

82 changes: 82 additions & 0 deletions baeck/DFSAndBFS/BOJ_21278.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package baeck.DFSAndBFS;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

//호석이 두마리 치킨
public class BOJ_21278 {
static int N, M; // 건물의 개수(N)와 도로의 개수(M)
static List<Integer>[] graph; // 건물 간의 연결 정보를 저장할 그래프
static int[] chicken = new int[2]; // 치킨집 위치를 저장할 배열
static int sum, min = Integer.MAX_VALUE; // 치킨집 거리의 합(sum)과 최소 거리(min)

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

graph = new List[N + 1];
for (int i = 1; i <= N; i++) {
graph[i] = new ArrayList<>();
}

// 도로 정보 입력 받기
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
graph[a].add(b);
graph[b].add(a);
}

// 모든 치킨집 조합에 대해 거리 합 계산하기
for (int i = 1; i < N; i++) {
for (int j = i + 1; j <= N; j++) {
sum = 0;
for (int k = 1; k <= N; k++) {
if (k == i || k == j) continue;
int dist = bfs(k, i) + bfs(k, j);
sum += dist;
}
// 최소 거리 업데이트
if (sum < min) {
min = sum;
chicken[0] = i;
chicken[1] = j;
}
}
}

// 결과 출력
System.out.println(chicken[0] + " " + chicken[1] + " " + min);
}

// BFS를 사용하여 시작점부터 도착점까지의 최단 거리를 계산하는 메소드
static int bfs(int start, int end) {
Queue<Integer> queue = new LinkedList<>();
boolean[] visited = new boolean[N + 1];
int[] distance = new int[N + 1];

queue.offer(start);
visited[start] = true;

while (!queue.isEmpty()) {
int cur = queue.poll();
if (cur == end) {
return distance[cur];
}
for (int next : graph[cur]) {
if (!visited[next]) {
visited[next] = true;
distance[next] = distance[cur] + 1;
queue.offer(next);
}
}
}

return -1; // 도달할 수 없는 경우
}
}
Loading