Skip to content
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
54 changes: 54 additions & 0 deletions BOJ/1000-5000번/DH_2098.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.io.*;
import java.util.*;

/*
* 외판원 순회
* dp 배열을 -1로 초기화해야되는 이유
* 한 지점에서 다른 지점들을 갈 수 없을 때, INF를 return 하게 되는데
* 이 때, 방문을 한 후 INF인지 방문을 하지 못하는 경우인지 알 수 없게 되어 시간 초과가 발생함
*/

public class DH_2098 {
static int N;
static int[][] W, dp;

static final int INF = Integer.MAX_VALUE >> 1;

static int TSP(int now, int visitedCities) {
if(visitedCities == (1 << N) - 1) {
// 지금 노드에서 시작점(0)까지 갈 수 있는 경로가 없다면 INF 반환
// 갈 수 있는 경로가 있다면 지금 노드에서 0까지 가는 값 반환
return W[now][0] != 0 ? W[now][0]: INF;
}

if(dp[now][visitedCities] != -1) return dp[now][visitedCities];
dp[now][visitedCities] = INF;

for(int next = 0; next < N; next++) {
int city = 1 << next;
// (visitedCites & city) != 0: 이미 간 곳
// W[now][next] == 0: 지금 위치에서 탐색하고자 하는 지점까지 갈 수 있는 길이가 없는 경우
if((visitedCities & city) != 0 || W[now][next] == 0) continue;
dp[now][visitedCities] = Math.min(dp[now][visitedCities], TSP(next, city | visitedCities) + W[now][next]);
}

return dp[now][visitedCities];
}

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

N = Integer.parseInt(br.readLine());
W = new int[N][N];
dp = new int[N][1 << N];

for(int r = 0; r < N; r++) {
st = new StringTokenizer(br.readLine());
for(int c = 0; c < N; c++) W[r][c] = Integer.parseInt(st.nextToken());
Arrays.fill(dp[r], -1);
}

System.out.println(TSP(0, 1 << 0));
}
}
78 changes: 78 additions & 0 deletions BOJ/1000-5000번/DH_2660.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import java.io.*;
import java.util.*;

/*
* 회장뽑기
*/

public class DH_2660 {
static int N, maxDepth;
static ArrayList<Integer> adj[];
static ArrayDeque<int []> q;
static boolean[] v;
static int[] depth; // 각 회원마다 끝까지 탐색했을 때 depth를 저장할 변수
static TreeSet<Integer> set; // 회장이 될 수 있는 사람의 번호를 담음

public static void main(String[] args) throws Exception {
initInput();
solution();

StringBuilder sb = new StringBuilder();

sb.append(maxDepth).append(" ").append(set.size()).append("\n");
for(int i: set) sb.append(i).append(" ");
System.out.println(sb);
}

static void solution() {
for(int i = 1; i < adj.length; i++) {
v = new boolean[N + 1];
q.add(new int[] {i, 0}); // [0]: 학생 번호, [1]: depth 깊이
v[i] = true;

while(!q.isEmpty()) {
int[] current = q.poll();
depth[i] = current[1];

for(int next: adj[current[0]]) {
if(v[next]) continue;
q.add(new int[] {next, current[1] + 1});
v[next] = true;
}
}

if(depth[i] < maxDepth) { // maxDepth 깊이가 작아야 점수가 작아지기 때문에
maxDepth = depth[i];
set.clear();
}

if(depth[i] == maxDepth) set.add(i);
}
}

static void initInput() throws Exception {
System.setIn(new FileInputStream("../AlgorithmStudy/input/BOJ2660.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

q = new ArrayDeque<>();
set = new TreeSet<Integer>();

maxDepth = Integer.MAX_VALUE;
N = Integer.parseInt(br.readLine());
adj = new ArrayList[N + 1];
for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Integer>();

depth = new int[N + 1];

String s;
while(!(s = br.readLine()).equals("-1 -1")) {
st = new StringTokenizer(s);
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());

adj[a].add(b);
adj[b].add(a);
}
}
}
32 changes: 32 additions & 0 deletions BOJ/10001-15000번/DH_13164.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.io.*;
import java.util.*;

/*
* 행복 유치원
*/

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

int N = Integer.parseInt(st.nextToken()); // 원생의 수
int K = Integer.parseInt(st.nextToken()); // 조의 개수

st = new StringTokenizer(br.readLine());

int[] diff = new int[N - 1];

int prev = Integer.parseInt(st.nextToken());
for(int i = 0; i < diff.length; i++) {
int current = Integer.parseInt(st.nextToken());
diff[i] = current - prev;
prev = current;
}

Arrays.sort(diff);
int result = 0;
for(int i = 0; i < N - K; i++) result += diff[i];
System.out.println(result);
}
}
85 changes: 85 additions & 0 deletions BOJ/10001-15000번/DH_14620.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import java.io.*;
import java.util.*;

/*
* 꽃길
*/
public class DH_14620 {
static int N, result;
static int[][] map;
static boolean[][] v;
static int[] dr = {-1 ,1, 0, 0}, dc = {0, 0, -1, 1};

public static void main(String[] args) throws Exception {
initInput();
func(0, 0, 0);
System.out.println(result);
}

static void func(int depth, int idx, int cost) {
if(depth == 3) {
result = Math.min(result, cost);
return;
}

for(int i = idx; i < N * N; i++) {
int r = i / N, c = i % N;

if(!check(r, c)) continue;

v[r][c] = true;
cost += map[r][c] + plant(r, c, true);

func(depth + 1, i + 1, cost);
v[r][c] = false;
cost -= map[r][c] + plant(r, c, false);
}
}

static int plant(int r, int c, boolean status) {
int cost = 0;
for(int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];

v[nr][nc] = status;
cost += map[nr][nc];
}

return cost;
}

static boolean check(int r, int c) {
for(int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];

if(!isValid(nr, nc) || v[nr][nc]) return false;
}

return true;
}

static boolean isValid(int r, int c) {
return r >= 0 && r < N && c >= 0 && c < N;
}

static void initInput() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

N = Integer.parseInt(br.readLine());
map = new int[N][N];
v = new boolean[N][N];

result = Integer.MAX_VALUE;

for(int r = 0; r < N; r++) {
st = new StringTokenizer(br.readLine());
for(int c = 0; c < N; c++) {
map[r][c] = Integer.parseInt(st.nextToken());
}
}

}
}
Loading