Skip to content

Commit 3f036ea

Browse files
committed
13주차 코드 업로드
1 parent 63da1c3 commit 3f036ea

File tree

5 files changed

+495
-0
lines changed

5 files changed

+495
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package dynamicprogramming;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class BJ1103 {
9+
10+
static int[][] map, dp;
11+
static boolean[][] visited;
12+
static int N, M, maxResult;
13+
static int[] dx = { -1, 1, 0, 0 };
14+
static int[] dy = { 0, 0, -1, 1 };
15+
16+
public static void main(String[] args) throws IOException {
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
N = Integer.parseInt(st.nextToken());
20+
M = Integer.parseInt(st.nextToken());
21+
22+
map = new int[N][M];
23+
dp = new int[N][M];
24+
visited = new boolean[N][M];
25+
26+
for (int i = 0; i < N; i++) {
27+
String line = br.readLine();
28+
for (int j = 0; j < M; j++) {
29+
if (line.charAt(j) == 'H') {
30+
map[i][j] = 10;
31+
} else {
32+
map[i][j] = line.charAt(j) - '0';
33+
}
34+
35+
dp[i][j] = -1;
36+
}
37+
}
38+
39+
dfs(0, 0);
40+
if (maxResult != -1) {
41+
maxResult = dp[0][0];
42+
}
43+
System.out.println(maxResult);
44+
45+
}
46+
47+
public static int dfs(int x, int y) {
48+
visited[x][y] = true;
49+
50+
if (dp[x][y] == -1) {
51+
dp[x][y] = 0;
52+
}
53+
54+
for (int i = 0; i < 4; i++) {
55+
int newX = x + dx[i] * map[x][y];
56+
int newY = y + dy[i] * map[x][y];
57+
58+
if (!isValid(newX, newY) || map[newX][newY] == 10) {
59+
continue;
60+
}
61+
62+
if (visited[newX][newY]) {
63+
maxResult = -1;
64+
return -1;
65+
}
66+
67+
if (dp[newX][newY] >= 0) {
68+
dp[x][y] = Math.max(dp[newX][newY], dp[x][y]);
69+
} else {
70+
dp[x][y] = Math.max(dfs(newX, newY), dp[x][y]);
71+
}
72+
73+
}
74+
75+
visited[x][y] = false;
76+
77+
if (maxResult == -1) {
78+
return -1;
79+
} else {
80+
return ++dp[x][y];
81+
}
82+
}
83+
84+
public static boolean isValid(int x, int y) {
85+
if (x < 0 || x >= N || y < 0 || y >= M) {
86+
return false;
87+
}
88+
return true;
89+
}
90+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package greedy;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Comparator;
7+
import java.util.PriorityQueue;
8+
import java.util.StringTokenizer;
9+
10+
public class BJ1202 {
11+
12+
static int N, K;
13+
static long result;
14+
static PriorityQueue<Jewel> jewelPQ = new PriorityQueue<Jewel>();
15+
static PriorityQueue<Integer> bags = new PriorityQueue<Integer>();
16+
static PriorityQueue<Integer> selecting = new PriorityQueue<Integer>(new Comparator<Integer>() {
17+
18+
@Override
19+
public int compare(Integer o1, Integer o2) {
20+
// TODO Auto-generated method stub
21+
return o2 - o1;
22+
}
23+
});
24+
25+
static class Jewel implements Comparable<Jewel> {
26+
int weight;
27+
int price;
28+
29+
Jewel(int weight, int price) {
30+
this.weight = weight;
31+
this.price = price;
32+
}
33+
34+
@Override
35+
public int compareTo(Jewel o) {
36+
if (this.weight == o.weight) {
37+
return o.price - this.price;
38+
} else {
39+
return this.weight - o.weight;
40+
}
41+
}
42+
}
43+
44+
public static void main(String[] args) throws IOException {
45+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
46+
StringTokenizer st = new StringTokenizer(br.readLine());
47+
48+
N = Integer.parseInt(st.nextToken());
49+
K = Integer.parseInt(st.nextToken());
50+
51+
for (int i = 0; i < N; i++) {
52+
st = new StringTokenizer(br.readLine());
53+
int M = Integer.parseInt(st.nextToken());
54+
int V = Integer.parseInt(st.nextToken());
55+
56+
jewelPQ.add(new Jewel(M, V));
57+
}
58+
for (int i = 0; i < K; i++) {
59+
bags.add(Integer.parseInt(br.readLine()));
60+
61+
}
62+
63+
while (!bags.isEmpty()) {
64+
int curBag = bags.poll();
65+
66+
while (!jewelPQ.isEmpty() && curBag >= jewelPQ.peek().weight) {
67+
Jewel curJewel = jewelPQ.poll();
68+
69+
selecting.add(curJewel.price);
70+
}
71+
72+
if(!selecting.isEmpty()) {
73+
result+=selecting.poll();
74+
}
75+
76+
}
77+
78+
System.out.println(result);
79+
80+
}
81+
82+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package shortestpath;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.ArrayList;
7+
import java.util.PriorityQueue;
8+
import java.util.StringTokenizer;
9+
10+
public class BJ1238 {
11+
12+
static ArrayList<ArrayList<Node>> graph = new ArrayList<ArrayList<Node>>();
13+
static ArrayList<ArrayList<Node>> graphReversed = new ArrayList<ArrayList<Node>>();
14+
static int N, M, X;
15+
static int[] distance, distanceReversed;
16+
17+
static class Node implements Comparable<Node> {
18+
int node;
19+
int distance;
20+
21+
Node(int node, int distance) {
22+
this.node = node;
23+
this.distance = distance;
24+
}
25+
26+
@Override
27+
public int compareTo(Node o) {
28+
return this.distance - o.distance;
29+
}
30+
31+
}
32+
33+
public static void main(String[] args) throws IOException {
34+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
35+
StringTokenizer st = new StringTokenizer(br.readLine());
36+
N = Integer.parseInt(st.nextToken());
37+
M = Integer.parseInt(st.nextToken());
38+
X = Integer.parseInt(st.nextToken());
39+
40+
distance = new int[N + 1];
41+
distanceReversed = new int[N + 1];
42+
for (int i = 0; i <= N; i++) {
43+
graph.add(new ArrayList<Node>());
44+
graphReversed.add(new ArrayList<Node>());
45+
46+
distance[i] = Integer.MAX_VALUE;
47+
distanceReversed[i] = Integer.MAX_VALUE;
48+
}
49+
50+
for (int i = 0; i < M; i++) {
51+
st = new StringTokenizer(br.readLine());
52+
int from = Integer.parseInt(st.nextToken());
53+
int to = Integer.parseInt(st.nextToken());
54+
int dist = Integer.parseInt(st.nextToken());
55+
56+
graph.get(from).add(new Node(to, dist));
57+
graphReversed.get(to).add(new Node(from, dist));
58+
}
59+
60+
findShortest(X);
61+
findShortestReversed(X);
62+
63+
int[] result = new int[N + 1];
64+
int maxResult = 0;
65+
for (int i = 1; i <= N; i++) {
66+
result[i] = distance[i] + distanceReversed[i];
67+
maxResult = Math.max(maxResult, result[i]);
68+
}
69+
70+
System.out.println(maxResult);
71+
72+
}
73+
74+
public static void findShortest(int startPoint) {
75+
PriorityQueue<Node> queue = new PriorityQueue<Node>();
76+
queue.add(new Node(startPoint, 0));
77+
distance[startPoint]= 0;
78+
79+
while (!queue.isEmpty()) {
80+
Node curNode = queue.poll();
81+
int node = curNode.node;
82+
int dist = curNode.distance;
83+
84+
if (dist > distance[node]) {
85+
continue;
86+
}
87+
88+
ArrayList<Node> tempList = graph.get(node);
89+
for (Node nextNode : tempList) {
90+
if (nextNode.distance + dist < distance[nextNode.node]) {
91+
distance[nextNode.node] = nextNode.distance + dist;
92+
queue.add(new Node(nextNode.node, distance[nextNode.node]));
93+
}
94+
}
95+
96+
}
97+
}
98+
99+
public static void findShortestReversed(int startPoint) {
100+
PriorityQueue<Node> queue = new PriorityQueue<Node>();
101+
queue.add(new Node(startPoint, 0));
102+
distanceReversed[startPoint] = 0;
103+
104+
while (!queue.isEmpty()) {
105+
Node curNode = queue.poll();
106+
int node = curNode.node;
107+
int dist = curNode.distance;
108+
109+
if (dist > distanceReversed[node]) {
110+
continue;
111+
}
112+
113+
ArrayList<Node> tempList = graphReversed.get(node);
114+
for (Node nextNode : tempList) {
115+
if (nextNode.distance + dist < distanceReversed[nextNode.node]) {
116+
distanceReversed[nextNode.node] = nextNode.distance + dist;
117+
queue.add(new Node(nextNode.node, distanceReversed[nextNode.node]));
118+
}
119+
}
120+
121+
}
122+
}
123+
124+
}

0 commit comments

Comments
 (0)