Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 706ee40

Browse files
authored
Merge pull request #337 from GreatAlgorithm-Study/jimin
[25주차] 손지민
2 parents 0d0c36f + ceb1173 commit 706ee40

File tree

4 files changed

+313
-0
lines changed

4 files changed

+313
-0
lines changed

‎BOJ/10001-15000번/JM_12784.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayList;
5+
import java.util.StringTokenizer;
6+
7+
/**
8+
* 22%에서 틀림 -> N = 1일 때 고려해주기
9+
*/
10+
public class JM_12784 {
11+
static class Node {
12+
int node;
13+
int dynamite;
14+
15+
public Node(int node, int dynamite) {
16+
this.node = node;
17+
this.dynamite = dynamite;
18+
}
19+
}
20+
static int N;
21+
static int M;
22+
static ArrayList<Node>[] graph;
23+
static final int INF = 987654321;
24+
25+
private static int dfs(int curr, boolean[] visited) {
26+
visited[curr] = true;
27+
28+
int minD = 0;
29+
for(Node next: graph[curr]) {
30+
if (visited[next.node]) continue;
31+
minD += Math.min(next.dynamite, dfs(next.node, visited));
32+
}
33+
return (minD == 0) ? INF : minD;
34+
}
35+
36+
public static void main(String[] args) throws IOException {
37+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
38+
StringTokenizer st = new StringTokenizer(br.readLine());
39+
int T = Integer.parseInt(st.nextToken());
40+
41+
StringBuilder sb = new StringBuilder();
42+
while (T-- > 0) {
43+
st = new StringTokenizer(br.readLine());
44+
N = Integer.parseInt(st.nextToken());
45+
M = Integer.parseInt(st.nextToken());
46+
47+
graph = new ArrayList[N + 1];
48+
for (int i = 0; i <= N; i++) {
49+
graph[i] = new ArrayList<>();
50+
}
51+
52+
for (int i = 0; i < M; i++) {
53+
st = new StringTokenizer(br.readLine());
54+
int u = Integer.parseInt(st.nextToken());
55+
int v = Integer.parseInt(st.nextToken());
56+
int d = Integer.parseInt(st.nextToken());
57+
graph[u].add(new Node(v, d));
58+
graph[v].add(new Node(u, d));
59+
}
60+
61+
if(N == 1) sb.append("0").append("\n");
62+
else sb.append(dfs(1, new boolean[N + 1])).append("\n");
63+
}
64+
System.out.println(sb);
65+
}
66+
}

‎BOJ/30000-35000번/JM_32070.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
6+
public class JM_32070 {
7+
static class Color {
8+
int box;
9+
boolean up;
10+
11+
public Color(int box, boolean up) {
12+
this.box = box;
13+
this.up = up;
14+
}
15+
}
16+
static int N;
17+
static Color[][] colors;
18+
static int[] boxParent;
19+
static int[] subBoxCount;
20+
21+
private static int findParent(int x) {
22+
if(x == boxParent[x]) return x;
23+
return boxParent[x] = findParent(boxParent[x]);
24+
}
25+
26+
private static void union(int u, int v) {
27+
int uP = findParent(u);
28+
int vP = findParent(v);
29+
if(uP < vP) {
30+
boxParent[vP] = uP;
31+
subBoxCount[uP] += subBoxCount[vP];
32+
}
33+
else if(uP > vP) {
34+
boxParent[uP] = vP;
35+
subBoxCount[vP] += subBoxCount[uP];
36+
}
37+
}
38+
39+
private static void makeCycle() {
40+
for (int i = 1; i <= N; i++) {
41+
boxParent[i] = i;
42+
subBoxCount[i] = 1;
43+
}
44+
45+
for (int i = 1; i <= N; i++) { // 같은 색의 공을 담은 box union
46+
union(colors[i][0].box, colors[i][1].box);
47+
}
48+
}
49+
50+
private static boolean isTwoPairsOnTop() {
51+
int[] twoPairs = new int[N + 1];
52+
for (int i = 1; i <= N; i++) {
53+
if(colors[i][0].up && colors[i][1].up) {
54+
int rootIdx = findParent(colors[i][0].box);
55+
twoPairs[rootIdx]++;
56+
}
57+
}
58+
for (int i = 1; i <= N; i++) {
59+
int rootIdx = findParent(colors[i][0].box);
60+
if(twoPairs[rootIdx] >= 2) return true;
61+
}
62+
return false;
63+
}
64+
65+
private static int solve() {
66+
makeCycle();
67+
if(isTwoPairsOnTop()) return -1;
68+
69+
int ans = 0;
70+
boolean[] check = new boolean[N + 1];
71+
for (int i = 1; i <= N; i++) {
72+
int rootIdx = findParent(colors[i][0].box);
73+
if(check[rootIdx]) continue;
74+
75+
check[rootIdx] = true;
76+
77+
if(subBoxCount[rootIdx] >= 2) {
78+
ans += subBoxCount[rootIdx] + 1;
79+
}
80+
}
81+
82+
return ans;
83+
}
84+
85+
public static void main(String[] args) throws IOException {
86+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
87+
StringTokenizer st = new StringTokenizer(br.readLine());
88+
N = Integer.parseInt(st.nextToken());
89+
90+
colors = new Color[N + 1][2];
91+
for (int i = 1; i <= N; i++) {
92+
st = new StringTokenizer(br.readLine());
93+
int a = Integer.parseInt(st.nextToken());
94+
int b = Integer.parseInt(st.nextToken());
95+
if(colors[a][0] == null) colors[a][0] = new Color(i, true);
96+
else colors[a][1] = new Color(i, true);
97+
if(colors[b][0] == null) colors[b][0] = new Color(i, false);
98+
else colors[b][1] = new Color(i, false);
99+
}
100+
101+
boxParent = new int[N + 1];
102+
subBoxCount = new int[N + 1];
103+
System.out.println(solve());
104+
}
105+
}

‎BOJ/5001-10000번/JM_9944.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
6+
public class JM_9944 {
7+
static int N;
8+
static int M;
9+
static char[][] map;
10+
private static final int[][] DIR = {{0, 1}, {-1, 0}, {1, 0}, {0, -1}};
11+
12+
private static boolean isRange(int y, int x) {
13+
return 0 <= y && y < N && 0 <= x && x < M;
14+
}
15+
16+
private static int moveBack(int y, int x, int ny, int nx, int d) {
17+
int moveCnt = 0;
18+
while (!(y == ny && x == nx)) {
19+
map[ny][nx] = '.';
20+
moveCnt += 1;
21+
ny -= DIR[d][0];
22+
nx -= DIR[d][1];
23+
}
24+
return moveCnt;
25+
}
26+
27+
private static int[] move(int y, int x, int d) {
28+
int moveCnt = 0;
29+
while (true) {
30+
int ny = y + DIR[d][0];
31+
int nx = x + DIR[d][1];
32+
if(!isRange(ny, nx) || map[ny][nx] != '.') break;
33+
map[ny][nx] = '#';
34+
moveCnt += 1;
35+
y = ny;
36+
x = nx;
37+
}
38+
return new int[]{y, x, moveCnt};
39+
}
40+
41+
private static int dfs(int y, int x, int blankCnt) {
42+
if(blankCnt == 0) {
43+
return 0;
44+
}
45+
46+
int res = -1;
47+
for (int d = 0; d < 4; d++) {
48+
int[] next = move(y, x, d);
49+
int ny = next[0];
50+
int nx = next[1];
51+
blankCnt -= next[2];
52+
53+
if(!(y == ny && x == nx)) {
54+
int tmp = dfs(ny, nx, blankCnt);
55+
if(tmp != -1 && (res == -1 || res > tmp + 1)) {
56+
res = tmp + 1;
57+
}
58+
}
59+
60+
blankCnt += moveBack(y, x, ny, nx, d);
61+
}
62+
63+
return res;
64+
}
65+
66+
private static int solve(int blankCnt) {
67+
int res = -1;
68+
69+
for (int i = 0; i < N; i++) {
70+
for (int j = 0; j < M; j++) {
71+
if(map[i][j] == '*') continue;
72+
map[i][j] = '#';
73+
int tmp = dfs(i, j, blankCnt - 1);
74+
if(tmp != -1 && (res == -1 || res > tmp)) {
75+
res = tmp;
76+
}
77+
map[i][j] = '.';
78+
}
79+
}
80+
81+
return res;
82+
}
83+
84+
public static void main(String[] args) throws IOException {
85+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
86+
StringTokenizer st;
87+
String line;
88+
89+
int T = 1;
90+
StringBuilder sb = new StringBuilder();
91+
while((line = br.readLine()) != null && line.length() > 0) {
92+
st = new StringTokenizer(line);
93+
N = Integer.parseInt(st.nextToken());
94+
M = Integer.parseInt(st.nextToken());
95+
96+
int blankCnt = 0;
97+
map = new char[N][M];
98+
for (int i = 0; i < N; i++) {
99+
map[i] = br.readLine().toCharArray();
100+
for (int j = 0; j < M; j++) {
101+
if(map[i][j] == '.') blankCnt++;
102+
}
103+
}
104+
sb.append("Case ").append(T).append(": ").append(solve(blankCnt)).append("\n");
105+
T++;
106+
}
107+
System.out.println(sb);
108+
}
109+
}

‎Programmers/Level2/JM_389480.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class JM_389480 {
2+
3+
public static int solution(int[][] info, int n, int m) {
4+
int L = info.length + 1;
5+
boolean[][][] dp = new boolean[L + 1][n][m];
6+
7+
dp[0][0][0] = true;
8+
for (int k = 1; k < L; k++) {
9+
for (int i = 0; i < n; i++) {
10+
for (int j = 0; j < m; j++) {
11+
if(!dp[k - 1][i][j]) continue;
12+
if(i + info[k - 1][0] < n) {
13+
dp[k][i + info[k - 1][0]][j] = true;
14+
}
15+
if(j + info[k - 1][1] < m) {
16+
dp[k][i][j + info[k - 1][1]] = true;
17+
}
18+
}
19+
}
20+
}
21+
22+
int answer = Integer.MAX_VALUE;
23+
for (int i = 0; i < n; i++) {
24+
for (int j = 0; j < m; j++) {
25+
if(dp[L - 1][i][j]) {
26+
answer = Math.min(answer, i);
27+
}
28+
}
29+
}
30+
return answer == Integer.MAX_VALUE ? -1 : answer;
31+
}
32+
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /