Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions src/main/java/org/example/_20week/BufferingTest.java

This file was deleted.

70 changes: 70 additions & 0 deletions src/main/java/org/example/_24week/NQueens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.example._24week;

public class NQueens {

private static int[][] map;
private static int n;
private static int answer = 0;

private static final int ROW = 0;
private static final int COL = 1;

public static void main(String[] args) {
int _n = 4;
final int solution = solution(4);
System.out.println(solution);
}

private static int solution(final int _n) {
n = _n;
map = new int[n][n];
final int[][] selectedPositions = new int[n][2];
dfs(0, 0, 0, selectedPositions);

return answer;
}

private static void dfs(final int row, final int col, int depth, int[][] selectedPositions) {
if (depth == n) {
answer++;
return;
}

for (int i = row; i < n; i++) {
for (int j = i == row ? col : 0; j < n; j++) {
if (isInValid(depth,selectedPositions, i, j)) {
continue;
}

selectedPositions[depth] = new int[]{i, j};
dfs(i, j + 1, depth + 1, selectedPositions);
selectedPositions[depth] = null;
}
}
}

private static boolean isInValid(final int depth, final int[][] selectedPositions, final int newPositionRow, final int newPositionCol) {
for (int i = 0; i < depth; i++) {
final int[] selectedPosition = selectedPositions[i];

// 가로
if (selectedPosition[COL] == newPositionCol) {
return true;
}
// 세로
if (selectedPosition[ROW] == newPositionRow) {
return true;
}
// Slash /
if (selectedPosition[ROW] + selectedPosition[COL] == newPositionRow + newPositionCol) {
return true;
}
// backSlash \
if (selectedPosition[ROW] - selectedPosition[COL] == newPositionRow - newPositionCol) {
return true;
}
}

return false;
}
}
71 changes: 71 additions & 0 deletions src/main/java/org/example/_24week/NQueens2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.example._24week;

public class NQueens2 {

private static int n;
private static int answer = 0;
private static int wrong = 0;

private static boolean[] visitedCol;
private static boolean[] visitedBackSlash = new boolean[23];
private static boolean[] visitedSlash = new boolean[23];

public static void main(String[] args) {
final long startTime = System.nanoTime();
final int solution = solution(12);
final long endTime = System.nanoTime();

System.out.println(endTime - startTime);

System.out.println(solution);
System.out.println(wrong);
}

private static int solution(final int _n) {
n = _n;
visitedCol = new boolean[n];
final int[][] selectedPositions = new int[n][2];
dfs(0, selectedPositions);

return answer;
}

private static void dfs(final int row, int[][] selectedPositions) {
if (row == n) {
answer++;
return;
}

for (int newCol = 0; newCol < n; newCol++) {
final int slash = row + newCol;
final int backSlash = row - newCol >= 0 ? row - newCol : newCol - row + 11;

if (isInValid(newCol, slash, backSlash)) {
continue;
}

visitedCol[newCol] = true;
visitedSlash[slash] = true;
visitedBackSlash[backSlash] = true;

selectedPositions[row] = new int[]{row, newCol};
dfs(row + 1, selectedPositions);

visitedCol[newCol] = false;
visitedSlash[slash] = false;
visitedBackSlash[backSlash] = false;
}
}

private static boolean isInValid(final int newPositionCol, final int slash, final int backSlash) {
if (
visitedCol[newPositionCol] ||
visitedSlash[slash] ||
visitedBackSlash[backSlash]
) {
return true;
}

return false;
}
}
45 changes: 45 additions & 0 deletions src/main/java/org/example/_24week/TrainingCenter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.example._24week;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

public class TrainingCenter {

private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
final int N = Integer.parseInt(st.nextToken());
final int M = Integer.parseInt(st.nextToken());

final int[] heights = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
final int[] querys = new int[N];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
final int a = Integer.parseInt(st.nextToken()) - 1;
final int b = Integer.parseInt(st.nextToken());
final int amount = Integer.parseInt(st.nextToken());

querys[a] += amount;
if (b < N) {
querys[b] -= amount;
}
}

for (int i = 1; i < N; i++) {
querys[i] += querys[i - 1];
}

final StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
heights[i] += querys[i];
sb.append(heights[i]).append(" ");
}

System.out.println(sb);
}
}