-
Notifications
You must be signed in to change notification settings - Fork 4
[10주차] 이혜원 #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
icegosimperson
merged 10 commits into
GreatAlgorithm-Study:main
from
icegosimperson:main
Nov 16, 2024
Merged
[10주차] 이혜원 #138
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
304fe43
이혜원: [CT] 회전하는 빙하_241111
icegosimperson d6bf6ef
이혜원: [SQL] Second Highest Salary_241112
icegosimperson efddd0f
이혜원: [BOJ] 2955 스도쿠 풀기_241112
icegosimperson cbe1774
docs: 불필요한 flag 변수 삭제
icegosimperson 16eafae
이혜원: [BOJ] 1613 역사_241113
icegosimperson 79969c0
이혜원: [BOJ] 2096 내려가기_241113
icegosimperson 101d091
이혜원: [PG] 150366 표 병합_241114
icegosimperson bb18fa8
이혜원: [SQL] 상품을 구매한 회원 비율 구하기_241114
icegosimperson 76d20bd
이혜원: [PG] 단속 카메라_241115
icegosimperson 49e10d1
이혜원: [PG] 181188 요격 시스템_241115
icegosimperson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
171 changes: 171 additions & 0 deletions
CodeTree/2019-2020년/HW_회전하는_빙하.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class HW_회전하는_빙하 { | ||
static class Node { | ||
int x, y; | ||
public Node(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
} | ||
|
||
static int n, q, L; | ||
static int[][] board, nextboard; | ||
static boolean[][] visited; | ||
static int[] dx = {0, 1, -1, 0}; // 오른쪽, 아래, 위, 왼쪽 | ||
static int[] dy = {1, 0, 0, -1}; | ||
static int max = 0; | ||
|
||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
n = Integer.parseInt(st.nextToken()); // 격자의 크기 (2^n * 2^n) | ||
q = Integer.parseInt(st.nextToken()); // 회전 횟수 | ||
L = (int) Math.pow(2, n); | ||
board = new int[L][L]; | ||
nextboard = new int[L][L]; | ||
|
||
// 보드 입력 | ||
for (int i = 0; i < L; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
for (int j = 0; j < L; j++) { | ||
board[i][j] = Integer.parseInt(st.nextToken()); | ||
} | ||
} | ||
|
||
// 회전 레벨 입력 | ||
st = new StringTokenizer(br.readLine()); | ||
while (q-- > 0) { | ||
int lv = Integer.parseInt(st.nextToken()); | ||
if (lv > 0) rotate(lv); | ||
melt(); | ||
} | ||
|
||
// 1. 남아있는 빙하의 총량 계산 | ||
int sum = 0; | ||
for (int i = 0; i < L; i++) { | ||
for (int j = 0; j < L; j++) { | ||
sum += board[i][j]; | ||
} | ||
} | ||
System.out.println(sum); | ||
|
||
// 2. 가장 큰 얼음 군집 크기 계산 | ||
visited = new boolean[L][L]; | ||
max = 0; | ||
for (int i = 0; i < L; i++) { | ||
for (int j = 0; j < L; j++) { | ||
if (!visited[i][j] && board[i][j] > 0) { | ||
max = Math.max(max, bfs(i, j)); | ||
} | ||
} | ||
} | ||
System.out.println(max); | ||
} | ||
|
||
// 초기화 | ||
public static void initialize() { | ||
for (int i = 0; i < L; i++) { | ||
for (int j = 0; j < L; j++) { | ||
nextboard[i][j] = 0; | ||
} | ||
} | ||
} | ||
|
||
|
||
public static void rotate(int level) { | ||
initialize(); // `nextboard` 초기화 | ||
int boxSize = (int) Math.pow(2, level); | ||
int halfSize = (int) Math.pow(2, level - 1); | ||
|
||
for (int i = 0; i < L; i += boxSize) { // 시계 방향 90도 회전 | ||
for (int j = 0; j < L; j += boxSize) { | ||
move(i, j, halfSize, 0); | ||
move(i, j + halfSize, halfSize, 1); | ||
move(i + halfSize, j, halfSize, 2); | ||
move(i + halfSize, j + halfSize, halfSize, 3); | ||
} | ||
} | ||
|
||
for (int i = 0; i < L; i++) { | ||
for (int j = 0; j < L; j++) { | ||
board[i][j] = nextboard[i][j]; // 배열 복사 | ||
} | ||
} | ||
} | ||
|
||
// size`만큼의 하위 격자를 시계 방향으로 회전 | ||
public static void move(int startX, int startY, int size, int dir) { | ||
for (int i = startX; i < startX + size; i++) { | ||
for (int j = startY; j < startY + size; j++) { | ||
int nx = i + dx[dir] * size; | ||
int ny = j + dy[dir] * size; | ||
if (!isValid(nx, ny)) continue; | ||
nextboard[nx][ny] = board[i][j]; | ||
} | ||
} | ||
} | ||
|
||
public static void melt() { | ||
initialize(); | ||
|
||
for (int i = 0; i < L; i++) { | ||
for (int j = 0; j < L; j++) { | ||
if (board[i][j] != 0 && iceCnt(i, j) < 3) { | ||
nextboard[i][j] = board[i][j] - 1; // -1 | ||
} else { | ||
nextboard[i][j] = board[i][j]; | ||
} | ||
} | ||
} | ||
|
||
for (int i = 0; i < L; i++) { | ||
for (int j = 0; j < L; j++) { | ||
board[i][j] = nextboard[i][j]; | ||
} | ||
} | ||
Comment on lines
+124
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nextboard를 지역 변수로 선언하면 불필요한 반복 줄일 수 있겠네요! 피드백 감사합니다👍 |
||
} | ||
|
||
public static int iceCnt(int x, int y) { | ||
int cnt = 0; | ||
for (int d = 0; d < 4; d++) { | ||
int nx = x + dx[d]; | ||
int ny = y + dy[d]; | ||
|
||
if (isValid(nx, ny) && board[nx][ny] != 0) { | ||
cnt++; | ||
} | ||
} | ||
return cnt; | ||
} | ||
|
||
// BFS : 얼음 군집 크기를 계산 | ||
public static int bfs(int x, int y) { | ||
int size = 1; | ||
Queue<int[]> queue = new LinkedList<>(); | ||
queue.offer(new int[]{x, y}); | ||
visited[x][y] = true; | ||
|
||
while (!queue.isEmpty()) { | ||
int[] pos = queue.poll(); | ||
|
||
for (int d = 0; d < 4; d++) { | ||
int nx = pos[0] + dx[d]; | ||
int ny = pos[1] + dy[d]; | ||
|
||
if (isValid(nx, ny) && !visited[nx][ny] && board[nx][ny] > 0) { | ||
visited[nx][ny] = true; | ||
queue.offer(new int[]{nx, ny}); | ||
size++; | ||
} | ||
} | ||
} | ||
return size; | ||
} | ||
|
||
public static boolean isValid(int x, int y) { | ||
return x >= 0 && y >= 0 && x < L && y < L; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
SQL/10주차/HW_Second Highest Salary.SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
SELECT MAX(Salary) as SecondHighestSalary | ||
FROM (SELECT salary, dense_rank() over(order by salary desc) as rk | ||
FROM Employee | ||
) Employee | ||
where rk = 2; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.