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 5c52abb

Browse files
Merge pull request #263 from GreatAlgorithm-Study/dahye
[19์ฃผ์ฐจ] ๊ณ ๋‹คํ˜œ
2 parents 5e32fde + 4570caa commit 5c52abb

File tree

6 files changed

+453
-0
lines changed

6 files changed

+453
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.math.*;
4+
5+
/*
6+
* ์‚ฌ์ „
7+
*/
8+
9+
public class DH_1256 {
10+
public static void main(String[] args) throws Exception {
11+
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
15+
int N = Integer.parseInt(st.nextToken()); // a์˜ ๊ฐœ์ˆ˜
16+
int M = Integer.parseInt(st.nextToken()); // z์˜ ๊ฐœ์ˆ˜
17+
BigInteger K = new BigInteger(st.nextToken());
18+
19+
// N๊ฐœ์˜ a์™€ M๊ฐœ์˜ z๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ด ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜
20+
BigInteger totalCnt = getCombCnt(N + M, N);
21+
22+
// ์ „์ฒด ๊ฒฝ์šฐ์˜ ์ˆ˜๋ณด๋‹ค K๊ฐ€ ํฌ๋‹ค๋ฉด ๊ฐ€๋Šฅํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
23+
if(totalCnt.compareTo(K) < 0) {
24+
System.out.println(-1);
25+
return;
26+
}
27+
28+
StringBuilder sb = new StringBuilder();
29+
30+
int currentCnt = N + M;
31+
for(int i = 0; i < currentCnt; i++) {
32+
33+
// a๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ
34+
BigInteger selectACnt = getCombCnt(N + M - 1, N - 1);
35+
36+
// a๋ฅผ ์„ ํƒํ–ˆ์„ ๋•Œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜์™€ ๊ฐ™๊ฑฐ๋‚˜ ์ž‘๋‹ค๋ฉด
37+
// a ์„ ํƒํ•ด์•ผ ๋จ
38+
// ๋™์‹œ์— N์˜ ๊ฐœ์ˆ˜๊ฐ€ 0๋ณด๋‹ค ์ปค์•ผ a๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Œ
39+
if(M == 0 || (K.compareTo(selectACnt) <= 0 && N > 0)) {
40+
sb.append("a");
41+
N--;
42+
} else {
43+
K = K.subtract(selectACnt);
44+
sb.append("z");
45+
M--;
46+
}
47+
}
48+
49+
System.out.println(sb);
50+
}
51+
52+
// ์กฐํ•ฉ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
53+
static BigInteger getCombCnt(int N, int K) {
54+
BigInteger nFactorial = getFactorial(N); // n! ๊ฐ’ ๊ตฌํ•˜๊ธฐ
55+
return nFactorial.divide(getFactorial(K)).divide(getFactorial(N - K));
56+
}
57+
58+
// n! ๊ฐ’ ๊ตฌํ•˜๊ธฐ
59+
static BigInteger getFactorial(int N) {
60+
BigInteger result = BigInteger.ONE;
61+
62+
for(int i = 1; i < N + 1; i++) {
63+
result = result.multiply(BigInteger.valueOf(i));
64+
}
65+
66+
return result;
67+
}
68+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* ๋ฏธ์นœ ๋กœ๋ด‡
6+
*/
7+
8+
public class DH_1405 {
9+
static int N;
10+
static int[] arr;
11+
static double result;
12+
static boolean[][] visit;
13+
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
14+
15+
public static void main(String[] args) throws Exception {
16+
// System.setIn(new FileInputStream("./input/BOJ1405.txt"));
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
20+
N = Integer.parseInt(st.nextToken());
21+
arr = new int[4];
22+
23+
for(int i = 0; i < 4; i++) arr[i] = Integer.parseInt(st.nextToken());
24+
25+
// ๋ฐฉ๋ฌธ๋ฐฐ์—ด visit
26+
// ์ƒ, ํ•˜, ์ขŒ, ์šฐ์— ๋Œ€ํ•ด ์ตœ๋Œ€ 14์นธ์”ฉ ๊ฐˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ 29๋กœ ์„ค์ •ํ•จ
27+
visit = new boolean[29][29];
28+
visit[14][14] = true;
29+
30+
func(0, 14, 14, 1);
31+
System.out.println(result);
32+
}
33+
34+
35+
static void func(int depth, int r, int c, double percent) {
36+
37+
// ๋๊นŒ์ง€ ๊ฐ”๋‹ค๋ฉด ํ˜„์žฌ ํ™•๋ฅ ์„ ์ „์ฒด ๊ฒฐ๊ณผ์— ๋”ํ•ด์ฃผ๊ธฐ
38+
if(depth == N) {
39+
result += percent;
40+
return;
41+
}
42+
43+
44+
// 0: ๋™, 1: ์„œ, 2: ๋‚จ, 3: ๋ถ
45+
for(int i = 0; i < 4; i++) {
46+
47+
int nr = r + dr[i];
48+
int nc = c + dc[i];
49+
50+
// ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ณณ์ด๊ฑฐ๋‚˜ ๊ฐˆ ์ˆ˜ ์—†๋Š” ๋ฐฉํ–ฅ์ด๋ผ๋ฉด
51+
if(visit[nr][nc] || arr[i] == 0) continue;
52+
visit[nr][nc] = true;
53+
54+
// ๋ ์ง€์ ๊นŒ์ง€ ๊ฐ€๊ธฐ ์œ„ํ•œ ํ™•๋ฅ ์€ ํ•ด๋‹น ์ง€์ ๊นŒ์ง€ ๊ฐ€๊ธฐ ์œ„ํ•ด ์ด๋™ํ•˜๋Š” ๊ฒฝ๋กœ์˜ ํ™•๋ฅ ๋“ค์œผ ๊ณฑํ•ด์ค€ ๊ฐ’
55+
func(depth + 1, r + dr[i], c + dc[i], percent * arr[i] / 100.0);
56+
57+
visit[nr][nc] = false;
58+
}
59+
}
60+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* ํ†ต๋‚˜๋ฌด ์˜ฎ๊ธฐ๊ธฐ
6+
* 4 โ‰ค N โ‰ค 50
7+
*/
8+
9+
public class DH_1938 {
10+
static final boolean VERTICAL = true; // VERTICAL: ์ˆ˜์ง์„ , HORIZONTAL: ์ˆ˜ํ‰์„ 
11+
12+
static class Tree {
13+
int r, c;
14+
boolean isVertical;
15+
16+
public Tree(int r, int c, boolean isVertical) {
17+
this.r = r;
18+
this.c = c;
19+
this.isVertical = isVertical;
20+
}
21+
}
22+
23+
static class Node {
24+
Tree t;
25+
int depth;
26+
public Node(Tree t, int depth) {
27+
this.t = t;
28+
this.depth = depth;
29+
}
30+
}
31+
32+
static Tree start, end;
33+
static int N;
34+
static char[][] map;
35+
public static void main(String[] args) throws Exception {
36+
// System.setIn(new FileInputStream("./input/BOJ1938.txt"));
37+
38+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
39+
N = Integer.parseInt(br.readLine());
40+
41+
map = new char[N][N];
42+
43+
for(int r = 0; r < N; r++) {
44+
String s = br.readLine();
45+
map[r] = s.toCharArray();
46+
}
47+
48+
// ์‹œ์ž‘์ ๊ณผ ๋ ์  ์ฐพ๊ธฐ
49+
findTree(map);
50+
51+
System.out.println(bfs());
52+
}
53+
54+
static int bfs() {
55+
Queue<Node> q = new ArrayDeque<Node>();
56+
57+
boolean[][][] v = new boolean[2][N][N];
58+
59+
// ์„ธ๋กœ๋กœ ์ด์–ด์ ธ ์žˆ๋‹ค๋ฉด 0๋ฒˆ ์ธ๋ฑ์Šค, ๊ฐ€๋กœ๋กœ ์ด์–ด์ ธ ์žˆ๋‹ค๋ฉด 1๋ฒˆ ์ธ๋ฑ์Šค
60+
q.add(new Node(start, 0));
61+
v[start.isVertical ? 0 : 1][start.r][start.c] = true;
62+
63+
while(!q.isEmpty()) {
64+
Node current = q.poll();
65+
66+
// ์ˆ˜์ง๋‚˜๋ฌด์ด๋ฉด 0, ์ˆ˜ํ‰๋‚˜๋ฌด์ด๋ฉด 1
67+
int currentIdx = current.t.isVertical ? 0 : 1;
68+
69+
if(current.t.r == end.r && current.t.c == end.c && current.t.isVertical == end.isVertical) {
70+
return current.depth;
71+
}
72+
73+
// ์ƒ, ํ•˜, ์ขŒ, ์šฐ๋กœ ์ด๋™
74+
for(int d = 0; d < 4; d++) {
75+
int nr = current.t.r + dr[d];
76+
int nc = current.t.c + dc[d];
77+
78+
if(!check(nr, nc) ||
79+
!canPositTree(nr, nc, current.t.isVertical) ||
80+
v[currentIdx][nr][nc] ||
81+
map[nr][nc] == '1') continue;
82+
83+
q.add(new Node(new Tree(nr, nc, current.t.isVertical), current.depth + 1));
84+
v[currentIdx][nr][nc] = true;
85+
}
86+
87+
// 90๋„ ํšŒ์ „
88+
if(v[1 ^ currentIdx][current.t.r][current.t.c] ||
89+
!canRotate(current.t.r, current.t.c)) continue;
90+
91+
q.add(new Node(new Tree(current.t.r, current.t.c, !current.t.isVertical), current.depth + 1));
92+
v[1 ^ currentIdx][current.t.r][current.t.c] = true;
93+
}
94+
95+
return 0;
96+
}
97+
98+
static boolean canRotate(int r, int c) {
99+
100+
// 90๋„ ํšŒ์ „ํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ฑธ๋ฆฌ๋Š”๊ฒŒ ์žˆ๋Š”์ง€ ํ™•์ธ
101+
for(int sr = r - 1; sr < r + 2; sr++) {
102+
for(int sc = c - 1; sc < c + 2; sc++) {
103+
if(!check(sr, sc) || map[sr][sc] == '1') return false;
104+
}
105+
}
106+
107+
return true;
108+
}
109+
110+
// ์„ผํ„ฐ ์–‘ ์˜†์œผ๋กœ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ
111+
static boolean canPositTree(int r, int c, boolean isVertical) {
112+
int cr1 = isVertical ? r - 1: r, cc1 = isVertical ? c : c - 1;
113+
int cr2 = isVertical ? r + 1: r, cc2 = isVertical ? c : c + 1;
114+
115+
if(!check(cr1, cc1) || !check(cr2, cc2)) return false;
116+
if(map[cr1][cc1] == '1' || map[cr2][cc2] == '1') return false;
117+
return true;
118+
}
119+
120+
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
121+
122+
123+
// 3๊ฐœ๋กœ ์—ฐ์†๋˜์–ด ์žˆ๋Š” ๋‚˜๋ฌด๋“ค ์ฐพ๊ธฐ
124+
// ์‹œ์ž‘์ ๊ณผ ๋ ์  ๋ชจ๋‘ ์ฐพ๊ธฐ
125+
static void findTree(char[][] map) {
126+
127+
for(int r = 0; r < N; r++) {
128+
for(int c = 0; c < N; c++) {
129+
130+
if(map[r][c] == 'B' && start == null) {
131+
for(int i = 0; i < 2; i++) {
132+
start = getFindTree(r, c, i, map, map[r][c]);
133+
if(start != null) break;
134+
}
135+
}
136+
137+
if(map[r][c] == 'E' && end == null) {
138+
for(int i = 0; i < 2; i++) {
139+
end = getFindTree(r, c, i, map, map[r][c]);
140+
if(end != null) break;
141+
}
142+
}
143+
}
144+
}
145+
}
146+
147+
static Tree getFindTree(int r, int c, int i, char[][] map, char ch) {
148+
149+
// i == 0 -> ์„ธ๋กœ
150+
// i == 1 -> ๊ฐ€๋กœ
151+
int cr1 = i == 0 ? r - 1: r, cc1 = i == 0 ? c : c - 1;
152+
int cr2 = i == 0 ? r + 1: r, cc2 = i == 0 ? c : c + 1;
153+
154+
if(!check(cr1, cc1) || !check(cr2, cc2)) return null;
155+
156+
if(map[cr1][cc1] != ch || map[cr2][cc2] != ch) return null;
157+
158+
// ์„ธ๋กœ
159+
if(i == 0)return new Tree(r, c, VERTICAL);
160+
return new Tree(r, c, !VERTICAL);
161+
}
162+
163+
static boolean check(int r, int c) {
164+
return r >= 0 && r < N && c >= 0 && c < N;
165+
}
166+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* ํ–‰๋ ฌ ๊ณฑ์…ˆ ์ˆœ์„œ
6+
*/
7+
8+
public class DH_11049 {
9+
10+
static class Matrix {
11+
int r, c;
12+
public Matrix(int r, int c) {
13+
this.r = r;
14+
this.c = c;
15+
}
16+
}
17+
18+
static int[][] dp;
19+
20+
public static void main(String[] args) throws Exception {
21+
System.setIn(new FileInputStream("./input/BOJ11049.txt"));
22+
23+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
24+
int N = Integer.parseInt(br.readLine());
25+
26+
Matrix[] matrix = new Matrix[N];
27+
dp = new int[N][N]; // dp[i][j]: i๋ฒˆ์งธ ํ–‰๋ ฌ์—์„œ j๋ฒˆ์งธ ํ–‰๋ ฌ๊นŒ์ง€ ๊ณฑํ•  ๋•Œ, ์ตœ์†Œ ์—ฐ์‚ฐ ํšŸ์ˆ˜
28+
29+
StringTokenizer st;
30+
31+
for(int i = 0; i < matrix.length; i++) {
32+
st = new StringTokenizer(br.readLine());
33+
int r = Integer.parseInt(st.nextToken());
34+
int c = Integer.parseInt(st.nextToken());
35+
36+
matrix[i] = new Matrix(r, c);
37+
}
38+
39+
System.out.println(getMinMultifly(matrix, 0, N - 1));
40+
}
41+
42+
// s๋ฒˆ์งธ์—์„œ e๋ฒˆ์งธ๊นŒ์ง€ ํ–‰๋ ฌ์„ ๊ณฑํ–ˆ์„ ๋•Œ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
43+
static int getMinMultifly(Matrix[] matrix, int s, int e) {
44+
if(s == e) return 0; // ํ•œ ๊ฐœ์˜ ํ–‰๋ ฌ์ด ์žˆ์„ ๋•Œ โ†’ ์—ฐ์‚ฐํšŸ์ˆ˜: 0t
45+
if(dp[s][e] != 0) return dp[s][e];
46+
if(s + 1 == e) return matrix[s].r * matrix[e].r * matrix[e].c;
47+
48+
int result = Integer.MAX_VALUE;
49+
50+
for(int i = s; i < e; i++) {
51+
52+
// getMinMultifly(matrix, s, i): s๋ถ€ํ„ฐ i๊นŒ์ง€
53+
// getMinMultifly(matrix, i + 1, e): i + 1๋ถ€ํ„ฐ e๊นŒ์ง€
54+
// matrix[s].r * matrix[i + 1].r * matrix[e].c: i๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆˆ ๋‹ค์Œ ์ตœ์ข…์ ์œผ๋กœ ๋ฐฐ์—ด์ด 2๊ฐœ์˜ ๋ฐฐ์—ด์ด ๋˜์—ˆ์„ ๋•Œ, ๋ฐฐ์—ด ์—ฐ์‚ฐ ํšŸ์ˆ˜
55+
result = Math.min(result,
56+
getMinMultifly(matrix, s, i) + getMinMultifly(matrix, i + 1, e) + (matrix[s].r * matrix[i + 1].r * matrix[e].c));
57+
}
58+
return dp[s][e] = result;
59+
}
60+
61+
}

0 commit comments

Comments
(0)

AltStyle ใซใ‚ˆใฃใฆๅค‰ๆ›ใ•ใ‚ŒใŸใƒšใƒผใ‚ธ (->ใ‚ชใƒชใ‚ธใƒŠใƒซ) /