Skip to content

Commit 0b2eee3

Browse files
authored
Merge pull request #83 from developHeo515/junsu
Junsu
2 parents b358b59 + f47e2f0 commit 0b2eee3

File tree

31 files changed

+2547
-0
lines changed

31 files changed

+2547
-0
lines changed
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//BOJ10159 저울, 골드4
2+
//플로이드 워샬 알고리즘(DP)
3+
//경로가 정점마다 연결됐는지만 확인하면 된다.
4+
import java.io.*;
5+
import java.util.*;
6+
public class BOJ10159 {
7+
static int N, M;
8+
static int[][] arr;
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
12+
StringTokenizer st;
13+
14+
N = Integer.parseInt(br.readLine());
15+
M = Integer.parseInt(br.readLine());
16+
int INF = 100000000;
17+
arr = new int[N+1][N+1];
18+
19+
for(int i = 0; i < M; i++) {
20+
st = new StringTokenizer(br.readLine());
21+
int a = Integer.parseInt(st.nextToken());
22+
int b = Integer.parseInt(st.nextToken());
23+
arr[a][b] = 1; //연결된거는 1로 표현
24+
}
25+
26+
//플로이드 워샬 알고리즘(DP)
27+
for(int k = 1; k <= N; k++) {
28+
for(int i = 1; i <= N; i++) {
29+
for(int j = 1; j <= N; j++) {
30+
if(i == j) continue;
31+
if(arr[i][k] == 1 && arr[k][j] == 1)
32+
arr[i][j] = 1;
33+
}
34+
}
35+
}
36+
37+
//인접리스트 (i, j)가 1이면 (j, i)도 1로 해준다. 그래야 물건 개수 파악 가능
38+
for(int i = 1; i <= N; i++) {
39+
for(int j = 1; j <= N; j++) {
40+
if(arr[i][j] == 1)
41+
arr[j][i] = 1;
42+
}
43+
}
44+
45+
for(int i = 1; i <= N; i++) {
46+
int ans = 0;
47+
for(int j = 1; j <= N; j++) {
48+
if(i == j) continue;
49+
if(arr[i][j] == 0) ans++; //물건 i와 비교 결과를 알 수 없는 물건의 개수 구하기
50+
}
51+
bw.write(ans+"\n");
52+
}
53+
54+
55+
56+
bw.flush();
57+
bw.close();
58+
br.close();
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//BOJ11404 플로이드, 골드4
2+
//플로이드 워샬 알고리즘(DP)
3+
//INF가 MAX_VALUE인 상태에서 더하면 오버플로우 발생하는 문제가 있었음
4+
import java.io.*;
5+
import java.util.*;
6+
7+
public class BOJ11404 {
8+
static int n, m;
9+
static int[][] arr;
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
StringTokenizer st;
14+
15+
n = Integer.parseInt(br.readLine());
16+
m = Integer.parseInt(br.readLine());
17+
int INF = 987654321; //INF가 MAX_VALUE인 상태에서 더하면 오버플로우 발생하는 문제가 있었음
18+
arr = new int[n+1][n+1];
19+
for(int i = 1; i <= n; i++) {
20+
for(int j = 1; j <= n; j++) {
21+
arr[i][j] = INF;
22+
}
23+
}
24+
25+
for(int i = 0; i < m; i++) {
26+
st = new StringTokenizer(br.readLine());
27+
int a = Integer.parseInt(st.nextToken());
28+
int b = Integer.parseInt(st.nextToken());
29+
int cost = Integer.parseInt(st.nextToken());
30+
// System.out.println(a+" "+b+" "+ cost);
31+
if(arr[a][b] != 0) {
32+
arr[a][b] = Math.min(arr[a][b], cost);
33+
}else {
34+
arr[a][b] = cost;
35+
}
36+
}
37+
38+
// 플로이드 워샬 알고리즘(DP) 구현
39+
for(int k = 1; k <= n; k++) {
40+
for(int i = 1; i <= n; i++) {
41+
for(int j = 1; j <= n; j++) {
42+
if(i == j) continue;
43+
if(arr[i][j] > arr[i][k] + arr[k][j])
44+
arr[i][j] = arr[i][k] + arr[k][j];
45+
}
46+
}
47+
}
48+
49+
for(int i = 1; i <= n; i++) {
50+
for(int j = 1; j <= n; j++) {
51+
if(arr[i][j] == INF)
52+
arr[i][j] = 0;
53+
bw.write(arr[i][j] + " ");
54+
}
55+
bw.write("\n");
56+
}
57+
58+
bw.flush();
59+
bw.close();
60+
br.close();
61+
}
62+
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//BOJ12865 평범한 배낭, 골드5
2+
// DP문제, 이해하는데 시간을 많이 사용하였다.
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class BOJ12865 {
7+
public static void main(String[] args) throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
11+
int T = Integer.parseInt(br.readLine());
12+
for(int tc = 1; tc <= T; tc++) {
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
15+
int N = Integer.parseInt(st.nextToken());
16+
int K = Integer.parseInt(st.nextToken());
17+
18+
int[][] dp = new int[N+1][K+1];
19+
int[] W = new int[N+1];
20+
int[] V = new int[N+1];
21+
22+
for(int i = 1; i <= N; i++) {
23+
st = new StringTokenizer(br.readLine());
24+
W[i] = Integer.parseInt(st.nextToken());
25+
V[i] = Integer.parseInt(st.nextToken());
26+
}
27+
28+
for(int k = 1; k <= K; k++) { //무게
29+
for(int i = 1; i <= N; i++) { // 물품의 수
30+
dp[i][k] = dp[i-1][k]; //기본적으로 이전 아이템의 가치를 저장하고 시작
31+
if(k - W[i] >= 0) { //무게에서 자신의 무게를 뺐을 때 남는 무게가 존재한다면
32+
//이전 물품에서 구한 가치 VS (남은 무게의 가치 + 자신의 가치) 중에서 큰 값을 저장한다.
33+
dp[i][k] = Math.max(dp[i-1][k], V[i] + dp[i-1][k - W[i]]);
34+
}
35+
}
36+
}
37+
38+
bw.write("#"+ tc + " " + dp[N][K] + "\n");
39+
bw.flush();
40+
}
41+
bw.close();
42+
br.close();
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//BOJ12893 적의 적
2+
//유니온 파인드로 각자의 친구들을 연결
3+
// enemy(적) 배열을 활용
4+
// 적대관계인 사람의 적이 있다면 내 친구로(union) 등록한다.
5+
import java.io.*;
6+
import java.util.*;
7+
8+
public class BOJ12893 {
9+
static int N, M;
10+
static int[] parent;
11+
static int[] enemy;
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
17+
N = Integer.parseInt(st.nextToken());
18+
M = Integer.parseInt(st.nextToken());
19+
parent = new int[N+1];
20+
enemy = new int[N+1];
21+
for(int i = 1; i <= N; i++) {
22+
parent[i] = i;
23+
}
24+
25+
boolean flag = true;
26+
for(int i = 0; i < M; i++) {
27+
st = new StringTokenizer(br.readLine());
28+
int a = Integer.parseInt(st.nextToken());
29+
int b = Integer.parseInt(st.nextToken());
30+
31+
if(find(a) == find(b)) {
32+
flag = false;
33+
break;
34+
};
35+
36+
// a한테 적이 있을 시 b랑 친구다
37+
if(enemy[a] != 0) {
38+
union(enemy[a], b);
39+
}else { //a한테 적이 없다면 a의 적으로 b를 등록한다.
40+
enemy[a] = b;
41+
}
42+
43+
//b한테 적이 있을 시 a랑 친구다.
44+
if(enemy[b] != 0) {
45+
union(enemy[b], a);
46+
}else { //b한테 적이 없다면 b의 적으로 a를 등록한다.
47+
enemy[b] = a;
48+
}
49+
}
50+
51+
if(flag) {
52+
bw.write(1+"\n");
53+
}else {
54+
bw.write(0+"\n");
55+
}
56+
57+
58+
bw.flush();
59+
bw.close();
60+
br.close();
61+
}
62+
static boolean union(int a, int b) {
63+
a = find(a);
64+
b = find(b);
65+
66+
if(a == b) return false;
67+
parent[b] = a;
68+
return true;
69+
}
70+
static int find(int x) {
71+
if(parent[x] == x) return x;
72+
return parent[x] = find(parent[x]);
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//BOJ13549 숨바꼭질3, 골드5
2+
//기존 숨바꼭질 문제에서 최단경로 기능이 추가됨
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class BOJ13549 {
7+
static class Node{
8+
int idx;
9+
int time;
10+
public Node(int idx, int time) {
11+
this.idx = idx;
12+
this.time = time;
13+
}
14+
}
15+
16+
static int[] point;
17+
static int N, K, ans;
18+
public static void main(String[] args) throws IOException {
19+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
21+
StringTokenizer st = new StringTokenizer(br.readLine());
22+
23+
N = Integer.parseInt(st.nextToken());
24+
K = Integer.parseInt(st.nextToken());
25+
point = new int[100000+1];
26+
ans = bfs();
27+
28+
bw.write(ans + "\n");
29+
bw.flush();
30+
bw.close();
31+
br.close();
32+
}
33+
static int bfs() {
34+
if(N == K) return 0; // 0, 0일 때 예외 케이스 처리해줘야한다.
35+
36+
Queue<Node> q = new LinkedList<>();
37+
// 시작 time을 1로 해놓고, 결과 출력시 1 빼기. point의 값이 0인 것(방문 안한 곳)과 구별해주기 위해서.
38+
q.add(new Node(N, 1));
39+
point[N] = 1;
40+
41+
while(!q.isEmpty()) {
42+
Node cur = q.poll();
43+
44+
if(cur.idx + 1 >= 0 && cur.idx+1 <= 100000) { //앞으로 한칸
45+
if(point[cur.idx+1] == 0 || point[cur.idx+1] > cur.time+1) {
46+
point[cur.idx+1] = cur.time + 1;
47+
q.add(new Node(cur.idx + 1, cur.time+1));
48+
}
49+
}
50+
51+
if(cur.idx - 1 >= 0 && cur.idx-1 <= 100000) { //뒤로 한칸
52+
if(point[cur.idx-1] == 0 || point[cur.idx-1] > cur.time+1) {
53+
point[cur.idx-1] = cur.time + 1;
54+
q.add(new Node(cur.idx - 1, cur.time+1));
55+
}
56+
}
57+
58+
if(cur.idx * 2 >= 0 && cur.idx*2 <= 100000) { //순간이동
59+
if(point[cur.idx*2] == 0 || point[cur.idx*2] > cur.time) {
60+
point[cur.idx*2] = cur.time;
61+
q.add(new Node(cur.idx * 2, cur.time));
62+
}
63+
}
64+
65+
66+
}
67+
if(point[K] != 0) return point[K] - 1;
68+
return -1;
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//BOJ13913 숨바꼭질4, 골드4
2+
//기존 숨바꼭질 문제에서 BFS 최단거리 경로를 저장하는 배열을 사용
3+
//경로 기록은 순서대로 구하기 위해 stack에 담았다가 pop하면된다.
4+
//최적화 완료
5+
import java.io.*;
6+
import java.util.*;
7+
8+
public class BOJ13913 {
9+
static int[] point, parent;
10+
static int N, K, time;
11+
public static void main(String[] args) throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
14+
StringTokenizer st = new StringTokenizer(br.readLine());
15+
16+
N = Integer.parseInt(st.nextToken());
17+
K = Integer.parseInt(st.nextToken());
18+
point = new int[100000+1];
19+
parent = new int[100000+1];
20+
time = bfs();
21+
22+
bw.write(time+"\n");
23+
24+
//순서대로 구하기 위해 stack에 담았다가 pop하면된다.
25+
Stack<Integer> stack = new Stack<>();
26+
stack.push(K);
27+
int index = K;
28+
29+
while(index != N) {
30+
index = parent[index];
31+
stack.push(index);
32+
}
33+
34+
while(!stack.isEmpty()) {
35+
bw.write(stack.pop() + " ");
36+
}
37+
38+
bw.flush();
39+
bw.close();
40+
br.close();
41+
}
42+
static int bfs() {
43+
if(N == K) return 0; // 0, 0일 때 예외 케이스 처리해줘야한다.
44+
45+
Queue<Integer> q = new LinkedList<>();
46+
q.add(N);
47+
point[N] = 1;
48+
49+
while(!q.isEmpty()) {
50+
int location = q.poll();
51+
52+
// System.out.print(location + "-");
53+
54+
for(int i = 0; i < 3; i++) {
55+
int next;
56+
57+
if(i == 0) next = location - 1;
58+
else if(i == 1) next = location + 1;
59+
else next = location * 2;
60+
61+
if(next < 0 || next > 100000) continue;
62+
63+
if(point[next] == 0) {
64+
q.add(next);
65+
point[next] = point[location] + 1;
66+
parent[next] = location;
67+
}
68+
}
69+
70+
}
71+
if(point[K] != 0) return point[K] - 1;
72+
return -1;
73+
}
74+
}

0 commit comments

Comments
 (0)