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

[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
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit Hold shift + click to select a range
304fe43
이혜원: [CT] 회전하는 빙하_241111
icegosimperson Nov 11, 2024
d6bf6ef
이혜원: [SQL] Second Highest Salary_241112
icegosimperson Nov 12, 2024
efddd0f
이혜원: [BOJ] 2955 스도쿠 풀기_241112
icegosimperson Nov 13, 2024
cbe1774
docs: 불필요한 flag 변수 삭제
icegosimperson Nov 13, 2024
16eafae
이혜원: [BOJ] 1613 역사_241113
icegosimperson Nov 14, 2024
79969c0
이혜원: [BOJ] 2096 내려가기_241113
icegosimperson Nov 14, 2024
101d091
이혜원: [PG] 150366 표 병합_241114
icegosimperson Nov 15, 2024
bb18fa8
이혜원: [SQL] 상품을 구매한 회원 비율 구하기_241114
icegosimperson Nov 15, 2024
76d20bd
이혜원: [PG] 단속 카메라_241115
icegosimperson Nov 15, 2024
49e10d1
이혜원: [PG] 181188 요격 시스템_241115
icegosimperson Nov 15, 2024
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
171 changes: 171 additions & 0 deletions CodeTree/2019-2020년/HW_회전하는_빙하.java
View file Open in desktop
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nextboard를 전역변수로 선언하지 않고, melt() 함수 안에 있는 지역변수로 선언한 뒤, 이 과정을 board = nextboard로 해도 될 것 같습니다!

Copy link
Contributor Author

@icegosimperson icegosimperson Nov 15, 2024

Choose a reason for hiding this comment

The 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
View file Open in desktop
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;

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