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 4fd18ee

Browse files
authored
Merge pull request #177 from Jewan1120/main
[13주차] 백제완
2 parents 51ab5c0 + 436b21b commit 4fd18ee

File tree

8 files changed

+390
-0
lines changed

8 files changed

+390
-0
lines changed

‎BOJ/1000-5000번/JW_1194.java‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
import java.util.StringTokenizer;
6+
7+
public class JW_1194 {
8+
9+
static int n, m;
10+
static char[][] board;
11+
12+
static int[] dy = { 1, -1, 0, 0 };
13+
static int[] dx = { 0, 0, 1, -1 };
14+
15+
public static void main(String[] args) throws Exception {
16+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
n = Integer.parseInt(st.nextToken());
19+
m = Integer.parseInt(st.nextToken());
20+
board = new char[n][m];
21+
int sy = 0, sx = 0;
22+
for (int i = 0; i < n; i++) {
23+
String line = br.readLine();
24+
for (int j = 0; j < m; j++) {
25+
board[i][j] = line.charAt(j);
26+
// 초기 위치 설정
27+
if (board[i][j] == '0') {
28+
sy = i;
29+
sx = j;
30+
}
31+
}
32+
}
33+
// 열쇠 상태에 따라 방문 체크를 해주기 위해서 3차원 방문 체크 생성
34+
boolean[][][] visited = new boolean[n][m][1 << 6];
35+
Deque<int[]> dq = new ArrayDeque<>();
36+
dq.offer(new int[] { sy, sx, 0, 0 });
37+
visited[sy][sx][0] = true;
38+
39+
// BFS
40+
while (!dq.isEmpty()) {
41+
int[] cur = dq.poll();
42+
// 현재 좌표, 열쇠 상태, 움직임 횟수
43+
int y = cur[0], x = cur[1], keys = cur[2], moveCnt = cur[3];
44+
// 종료 조건
45+
if (board[y][x] == '1') {
46+
System.out.println(moveCnt);
47+
return;
48+
}
49+
for (int i = 0; i < 4; i++) {
50+
int ny = y + dy[i], nx = x + dx[i];
51+
int newKeys = keys;
52+
53+
// 이동 조건
54+
if (!isValid(ny, nx) || board[ny][nx] == '#') {
55+
continue;
56+
}
57+
58+
// 열쇠일 경우
59+
if (isKey(ny, nx))
60+
// 해당 열쇠 추가
61+
newKeys |= (1 << (board[ny][nx] - 'a'));
62+
63+
// 문일 경우, 열쇠가 없다면 이동 불가
64+
else if (isDoor(ny, nx) && (keys & (1 << (board[ny][nx] - 'A'))) == 0)
65+
continue;
66+
67+
// 현재 열쇠 상태에서 방문하지 않았다면 방문
68+
if (!visited[ny][nx][newKeys]) {
69+
dq.offer(new int[] { ny, nx, newKeys, moveCnt + 1 });
70+
visited[ny][nx][newKeys] = true;
71+
}
72+
}
73+
}
74+
System.out.println(-1);
75+
}
76+
77+
// 열쇠 체크
78+
private static boolean isKey(int y, int x) {
79+
return 'a' <= board[y][x] && board[y][x] <= 'f';
80+
}
81+
82+
// 문 체크
83+
private static boolean isDoor(int y, int x) {
84+
return 'A' <= board[y][x] && board[y][x] <= 'F';
85+
}
86+
87+
// 경계 체크
88+
private static boolean isValid(int y, int x) {
89+
return 0 <= y && y < n && 0 <= x && x < m;
90+
}
91+
}

‎BOJ/1000-5000번/JW_1749.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class JW_1749 {
2+
3+
public static void main(String[] args) throws Exception {
4+
int n = read(), m = read(), max = Integer.MIN_VALUE;
5+
int[][] dp = new int[n + 1][m + 1];
6+
for (int i = 1; i < n + 1; i++)
7+
for (int j = 1; j < m + 1; j++)
8+
// (i, j)까지의 누적합
9+
dp[i][j] = read() + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
10+
for (int i = 1; i < n + 1; i++)
11+
for (int j = 1; j < m + 1; j++)
12+
for (int k = 1; k < i + 1; k++)
13+
for (int l = 1; l < j + 1; l++) {
14+
// 부분 배열의 누적합 구하기
15+
max = Math.max(max, dp[i][j] - dp[k - 1][j] - dp[i][l - 1] + dp[k - 1][l - 1]);
16+
}
17+
System.out.println(max);
18+
}
19+
20+
private static int read() throws Exception {
21+
int c, n = System.in.read() & 15;
22+
boolean m = n == 13;
23+
if (m)
24+
n = System.in.read() & 15;
25+
while ((c = System.in.read()) >= 48)
26+
n = (n << 3) + (n << 1) + (c & 15);
27+
if (c == 13)
28+
System.in.read();
29+
return m ? ~n + 1 : n;
30+
}
31+
}

‎BOJ/5001-10000번/JW_8979.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.Arrays;
2+
3+
public class JW_8879 {
4+
5+
public static void main(String[] args) throws Exception {
6+
int n = read(), k = read();
7+
int[][] arr = new int[n][4];
8+
for (int i = 0; i < n; i++)
9+
arr[i] = new int[] { read(), read(), read(), read() };
10+
// 정렬
11+
Arrays.sort(arr, (o1, o2) -> o2[1] != o1[1] ? o2[1] - o1[1] : o2[2] != o1[2] ? o2[2] - o1[2] : o2[3] - o1[3]);
12+
int rank = 1;
13+
for (int i = 0; i < n; i++) {
14+
// 등수 증가 조건
15+
if (i > 0 && !isSame(arr[i - 1], arr[i]))
16+
rank = i + 1;
17+
// 종료 조건
18+
if (arr[i][0] == k)
19+
break;
20+
}
21+
System.out.println(rank);
22+
}
23+
24+
// 동일 점수 확인
25+
private static boolean isSame(int[] A, int[] B) {
26+
return A[1] == B[1] && A[2] == B[2] && A[3] == B[3];
27+
}
28+
29+
private static int read() throws Exception {
30+
int c, n = System.in.read() & 15;
31+
while ((c = System.in.read()) >= 48)
32+
n = (n << 3) + (n << 1) + (c & 15);
33+
if (c == 13)
34+
System.in.read();
35+
return n;
36+
}
37+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.util.StringTokenizer;
4+
5+
public class JW_술래잡기_체스 {
6+
7+
static int[] dy = { 0, -1, -1, 0, 1, 1, 1, 0, -1 };
8+
static int[] dx = { 0, 0, -1, -1, -1, 0, 1, 1, 1 };
9+
static int max = 0;
10+
11+
public static void main(String[] args) throws Exception {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st;
14+
int[][][] board = new int[4][4][2];
15+
for (int i = 0; i < 4; i++) {
16+
st = new StringTokenizer(br.readLine());
17+
for (int j = 0; j < 4; j++) {
18+
int p = Integer.parseInt(st.nextToken());
19+
int d = Integer.parseInt(st.nextToken());
20+
board[i][j] = new int[] { p, d };
21+
}
22+
}
23+
policeMove(0, 0, 0, board);
24+
System.out.println(max);
25+
}
26+
27+
// 경찰 이동(재귀)
28+
private static void policeMove(int sum, int y, int x, int[][][] board) {
29+
sum += board[y][x][0]; // 해당 좌표의 값을 합
30+
max = Math.max(max, sum); // 최댓값 갱신
31+
board[y][x][0] = 0; // 해당 좌표의 값을 초기화
32+
int d = board[y][x][1]; // 해당 좌표의 방향
33+
doDookMove(y, x, board); // 도둑 이동
34+
// 경찰이 이동할 수 있는 방향을 모두 재귀적으로 탐색
35+
for (int i = 1; i <= 3; i++) {
36+
int ny = y + dy[d] * i, nx = x + dx[d] * i;
37+
// 이동할 수 있다면
38+
if (isValid(ny, nx) && board[ny][nx][0] != 0) {
39+
int[][][] nextBoard = copyBoard(board);
40+
policeMove(sum, ny, nx, nextBoard); // 재귀 호출
41+
}
42+
}
43+
}
44+
45+
// 도둑 이동
46+
private static void doDookMove(int py, int px, int[][][] board) {
47+
// 순서에 따라서 이동
48+
next: for (int p = 1; p <= 16; p++) {
49+
for (int y = 0; y < 4; y++)
50+
for (int x = 0; x < 4; x++)
51+
// 순서에 맞는 좌표를 찾았다면
52+
if (board[y][x][0] == p) {
53+
int d = board[y][x][1];
54+
int ny = y + dy[d], nx = x + dx[d];
55+
// 이동할 수 있는 좌표가 등장할 때까지 회전
56+
while (!isValid(ny, nx) || (ny == py && nx == px)) {
57+
d++;
58+
d %= 9;
59+
if (d == 0)
60+
d = 1;
61+
ny = y + dy[d];
62+
nx = x + dx[d];
63+
}
64+
swap(y, x, ny, nx, board); // 스왑
65+
board[ny][nx][1] = d; // 새로운 방향으로 변경
66+
continue next; // 발견했다면 다음 순서 진행
67+
}
68+
}
69+
}
70+
71+
// 두 좌표에 있는 원소를 스왑해주는 함수
72+
private static void swap(int y, int x, int ny, int nx, int[][][] board) {
73+
int[] temp = board[y][x];
74+
board[y][x] = board[ny][nx];
75+
board[ny][nx] = temp;
76+
}
77+
78+
// 기존의 배열을 복사하여 다음 배열을 만들어주는 함수
79+
private static int[][][] copyBoard(int[][][] board) {
80+
int[][][] nextBord = new int[4][4][2];
81+
for (int i = 0; i < 4; i++)
82+
for (int j = 0; j < 4; j++)
83+
nextBord[i][j] = board[i][j].clone();
84+
return nextBord;
85+
}
86+
87+
// 경계 체크 함수
88+
private static boolean isValid(int y, int x) {
89+
return 0 <= y && y < 4 && 0 <= x && x < 4;
90+
}
91+
}

‎Programmers/Level2/JW_250135.java‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class Solution {
2+
public int solution(int h1, int m1, int s1, int h2, int m2, int s2) {
3+
int answer = 0;
4+
int now = h1 * 3600 + m1 * 60 + s1;
5+
int end = h2 * 3600 + m2 * 60 + s2;
6+
// 시작부터 겹치고 시작할 경우
7+
if (now == 0 || now == 43200)
8+
answer++;
9+
while (now < end) {
10+
double[] nowAngles = calculateAndgles(now);
11+
double[] nextAngles = calculateAndgles(now + 1);
12+
boolean isHourMatched = chkHour(nowAngles, nextAngles);
13+
boolean isMinMatched = chkMin(nowAngles, nextAngles);
14+
if (isHourMatched)
15+
answer++;
16+
if (isMinMatched)
17+
answer++;
18+
// 둘 다 겹칠 경우에는 한번만 체크
19+
if (isHourMatched && isMinMatched)
20+
if (nextAngles[0] == nextAngles[1])
21+
answer--;
22+
now++;
23+
}
24+
return answer;
25+
}
26+
27+
private double[] calculateAndgles(int time) {
28+
double[] angles = new double[3];
29+
double h = time / 3600, m = time % 3600 / 60, s = time % 3600 % 60;
30+
// 시침의 현재 각도
31+
angles[0] = (h % 12) * (360d / 12) + m * (360d / 12 / 60) + s * (360d / 12 / 3600);
32+
// 분침의 현재 각도
33+
angles[1] = m * (360d / 60) + s * (360d / 60 / 60);
34+
// 초침의 현재 각도
35+
angles[2] = s * (360d / 60);
36+
return angles;
37+
}
38+
39+
// 시침 겹침 확인
40+
private boolean chkHour(double[] now, double[] next) {
41+
double nowSec = now[2], nextSec = next[2];
42+
// 1초의 움직임 안에 시침이 포함되는가
43+
if (nowSec < now[0] && next[0] <= nextSec)
44+
return true;
45+
// 59초의 값은 따로 계산 → 원형이기에 각도가 0으로 초기화되기 때문
46+
if (nowSec == 354d && 354d < now[0])
47+
return true;
48+
return false;
49+
}
50+
51+
// 분침 겹침 확인
52+
private boolean chkMin(double[] now, double[] next) {
53+
double nowSec = now[2], nextSec = next[2];
54+
// 1초의 움직임 안에 시침이 포함되는가
55+
if (nowSec < now[1] && next[1] <= nextSec)
56+
return true;
57+
// 59초의 값은 따로 계산 → 원형이기에 각도가 0으로 초기화되기 때문
58+
if (nowSec == 354d && 354d < now[1])
59+
return true;
60+
return false;
61+
}
62+
}

‎Programmers/Level3/JW_150367.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class JW_150367 {
2+
public int[] solution(long[] numbers) {
3+
int[] answer = new int[numbers.length];
4+
for (int i = 0; i < numbers.length; i++) {
5+
String str = toBinaryString(numbers[i]);
6+
// 표현 가능한 포화 이진 트리일 경우 '1'
7+
answer[i] = chkInOrder(str, 0, str.length() - 1) ? 1 : 0;
8+
}
9+
return answer;
10+
}
11+
12+
// 포화 이진 트리 -> 문자열 길이가 2^n - 1 꼴이 되어야 함
13+
private String toBinaryString(long number) {
14+
StringBuilder sb = new StringBuilder(Long.toBinaryString(number));
15+
int n = 1;
16+
while (sb.length() > (1 << n) - 1) {
17+
n++;
18+
}
19+
// 빈 부분 채우기
20+
sb.insert(0, "0".repeat((1 << n) - 1 - sb.length()));
21+
return sb.toString();
22+
}
23+
24+
// 중위 탐색을 진행하면서 불가능한 트리인지 확인
25+
private boolean chkInOrder(String str, int l, int r) {
26+
// 리프노드 일 경우는 True
27+
if (l == r)
28+
return true;
29+
int rootIdx = (l + r) / 2;
30+
// 루트 노드가 0'일 경우에는 자식에 '1'이 있으면 안됨
31+
if (str.charAt(rootIdx) == '0')
32+
for (int i = l; i <= r; i++)
33+
if (str.charAt(i) == '1')
34+
return false;
35+
// 다음 중위 탐색
36+
return chkInOrder(str, l, rootIdx - 1) && chkInOrder(str, rootIdx + 1, r);
37+
}
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
WITH CTE AS (
2+
SELECT
3+
product_id
4+
, new_price
5+
FROM
6+
products
7+
WHERE
8+
(product_id, change_date) IN
9+
(
10+
SELECT
11+
product_id
12+
, MAX(change_date)
13+
FROM
14+
Products
15+
WHERE
16+
change_date <= '2019年08月16日'
17+
GROUP BY
18+
product_id
19+
)
20+
)
21+
22+
SELECT
23+
DISTINCT(A.product_id)
24+
, IFNULL(B.new_price, 10) `price`
25+
FROM
26+
products A
27+
LEFT JOIN CTE B
28+
ON A.product_id = B.product_id

0 commit comments

Comments
(0)

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