Skip to content

Commit 9d36b33

Browse files
authored
[feat] 24주차 (#22)
1 parent ff8a550 commit 9d36b33

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.example._24week;
2+
3+
public class NQueens {
4+
5+
private static int[][] map;
6+
private static int n;
7+
private static int answer = 0;
8+
9+
private static final int ROW = 0;
10+
private static final int COL = 1;
11+
12+
public static void main(String[] args) {
13+
int _n = 4;
14+
final int solution = solution(4);
15+
System.out.println(solution);
16+
}
17+
18+
private static int solution(final int _n) {
19+
n = _n;
20+
map = new int[n][n];
21+
final int[][] selectedPositions = new int[n][2];
22+
dfs(0, 0, 0, selectedPositions);
23+
24+
return answer;
25+
}
26+
27+
private static void dfs(final int row, final int col, int depth, int[][] selectedPositions) {
28+
if (depth == n) {
29+
answer++;
30+
return;
31+
}
32+
33+
for (int i = row; i < n; i++) {
34+
for (int j = i == row ? col : 0; j < n; j++) {
35+
if (isInValid(depth,selectedPositions, i, j)) {
36+
continue;
37+
}
38+
39+
selectedPositions[depth] = new int[]{i, j};
40+
dfs(i, j + 1, depth + 1, selectedPositions);
41+
selectedPositions[depth] = null;
42+
}
43+
}
44+
}
45+
46+
private static boolean isInValid(final int depth, final int[][] selectedPositions, final int newPositionRow, final int newPositionCol) {
47+
for (int i = 0; i < depth; i++) {
48+
final int[] selectedPosition = selectedPositions[i];
49+
50+
// 가로
51+
if (selectedPosition[COL] == newPositionCol) {
52+
return true;
53+
}
54+
// 세로
55+
if (selectedPosition[ROW] == newPositionRow) {
56+
return true;
57+
}
58+
// Slash /
59+
if (selectedPosition[ROW] + selectedPosition[COL] == newPositionRow + newPositionCol) {
60+
return true;
61+
}
62+
// backSlash \
63+
if (selectedPosition[ROW] - selectedPosition[COL] == newPositionRow - newPositionCol) {
64+
return true;
65+
}
66+
}
67+
68+
return false;
69+
}
70+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.example._24week;
2+
3+
public class NQueens2 {
4+
5+
private static int n;
6+
private static int answer = 0;
7+
private static int wrong = 0;
8+
9+
private static boolean[] visitedCol;
10+
private static boolean[] visitedBackSlash = new boolean[23];
11+
private static boolean[] visitedSlash = new boolean[23];
12+
13+
public static void main(String[] args) {
14+
final long startTime = System.nanoTime();
15+
final int solution = solution(12);
16+
final long endTime = System.nanoTime();
17+
18+
System.out.println(endTime - startTime);
19+
20+
System.out.println(solution);
21+
System.out.println(wrong);
22+
}
23+
24+
private static int solution(final int _n) {
25+
n = _n;
26+
visitedCol = new boolean[n];
27+
final int[][] selectedPositions = new int[n][2];
28+
dfs(0, selectedPositions);
29+
30+
return answer;
31+
}
32+
33+
private static void dfs(final int row, int[][] selectedPositions) {
34+
if (row == n) {
35+
answer++;
36+
return;
37+
}
38+
39+
for (int newCol = 0; newCol < n; newCol++) {
40+
final int slash = row + newCol;
41+
final int backSlash = row - newCol >= 0 ? row - newCol : newCol - row + 11;
42+
43+
if (isInValid(newCol, slash, backSlash)) {
44+
continue;
45+
}
46+
47+
visitedCol[newCol] = true;
48+
visitedSlash[slash] = true;
49+
visitedBackSlash[backSlash] = true;
50+
51+
selectedPositions[row] = new int[]{row, newCol};
52+
dfs(row + 1, selectedPositions);
53+
54+
visitedCol[newCol] = false;
55+
visitedSlash[slash] = false;
56+
visitedBackSlash[backSlash] = false;
57+
}
58+
}
59+
60+
private static boolean isInValid(final int newPositionCol, final int slash, final int backSlash) {
61+
if (
62+
visitedCol[newPositionCol] ||
63+
visitedSlash[slash] ||
64+
visitedBackSlash[backSlash]
65+
) {
66+
return true;
67+
}
68+
69+
return false;
70+
}
71+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.example._24week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
import java.util.StringTokenizer;
8+
import java.util.stream.Collectors;
9+
10+
public class TrainingCenter {
11+
12+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
14+
public static void main(String[] args) throws IOException {
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
final int N = Integer.parseInt(st.nextToken());
17+
final int M = Integer.parseInt(st.nextToken());
18+
19+
final int[] heights = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
20+
final int[] querys = new int[N];
21+
for (int i = 0; i < M; i++) {
22+
st = new StringTokenizer(br.readLine());
23+
final int a = Integer.parseInt(st.nextToken()) - 1;
24+
final int b = Integer.parseInt(st.nextToken());
25+
final int amount = Integer.parseInt(st.nextToken());
26+
27+
querys[a] += amount;
28+
if (b < N) {
29+
querys[b] -= amount;
30+
}
31+
}
32+
33+
for (int i = 1; i < N; i++) {
34+
querys[i] += querys[i - 1];
35+
}
36+
37+
final StringBuilder sb = new StringBuilder();
38+
for (int i = 0; i < N; i++) {
39+
heights[i] += querys[i];
40+
sb.append(heights[i]).append(" ");
41+
}
42+
43+
System.out.println(sb);
44+
}
45+
}

0 commit comments

Comments
 (0)