Skip to content

[3주차] 배수빈 #31

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
merged 8 commits into from
Sep 28, 2024
Prev Previous commit
배수빈: [CT] 나무 타이쿤_240927
  • Loading branch information
baexxbin committed Sep 27, 2024
commit 8064d6e9d8197aa01bbc8e3553b42eb877fe7c10
114 changes: 114 additions & 0 deletions CodeTree/2021-2022년/SB_나무_타이쿤.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;

public class SB_나무_타이쿤 {
static int N, M;
static int[][] board;
static int[] dx = {0, -1, -1, -1, 0, 1, 1, 1};
static int[] dy = {1, 1, 0, -1, -1, -1, 0, 1};
static Queue<Node> pills = new ArrayDeque<>();
static boolean[][] check;

private static void movePill(int d, int p){
p %= N;
Copy link
Contributor

Choose a reason for hiding this comment

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

오흐,,, p를 나누기 연산할 생각은 왜 못했을까요,,,,
저는 p를 처음부터 나눌 생각을 못해서 다음 좌표구하는 코드가 x, y 각각 두 줄 씩 있는데,

int nx = ((cur.x + dr[d] * p) + N) % N; 이렇게 한 줄로 나타낼 수 있을 것 같아요!!!!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오호 한 줄표현 감사합니다!!

혹시 삼항연산자 쓰신부분 말씀하시는걸까요..?! 근데 저도 문뜩 int nx = ((cur.x + dr[d] * p) + N) % N;이렇게 한줄로 표현하면 어짜피 모듈러연산하는데 p를 미리 모듈러했어야하나 생각이 드네요....!?!?!?

int num = pills.size();
while (!pills.isEmpty() && num-- > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

우와 큐를 이용해서 푸는 방법은 전혀 떠올리지 못했었는데, 배워갑니다👍🏻👍🏻
혹시 어떻게 큐 사용하는 방법을 떠올리셨는지, 피드백 시간에 듣고 싶습니다!

Node cur = pills.poll();
int nx = (cur.x + (dx[d] * p)) % N;
int ny = (cur.y + (dy[d] * p)) % N;
if (nx < 0) nx= (nx+N)%N;
if (ny < 0) ny= (ny+N)%N;

board[nx][ny]++; // 특수 영양제 땅의 리브로 1증가
pills.offer(new Node(nx, ny));
}

}

private static void growTree(){
check = new boolean[N][N];

while(!pills.isEmpty()){
Node cur = pills.poll();

for(int i=1; i<8; i+=2){ // 각 영양제의 대각선 체크 (홀수)
int nx = cur.x+dx[i];
int ny = cur.y+dy[i];
if((0<=nx && nx<N && 0<=ny && ny<N) && board[nx][ny]>0) board[cur.x][cur.y]++;
Copy link
Contributor

@Jewan1120 Jewan1120 Sep 27, 2024

Choose a reason for hiding this comment

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

경계 체크하는 부분 같은 경우에는 재사용되는 경우가 많으니 따로 함수로 빼면 좀 더 깔끔해질 것 같아요!
저는 이런식으로 boolean을 반환하도록 사용해요

private static boolean isValid(int ny, int nx) {
    return 0 <= nx && nx < N && 0 <= ny && ny < N;
}

}
check[cur.x][cur.y] = true;
}
}

private static void injectPill(){
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] >= 2 && !check[i][j]) {
board[i][j] -= 2;
pills.offer(new Node(i, j));
}
}
}
}

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

board = new int[N][N];
check = new boolean[N][N];
for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
for(int j=0; j<N; j++){
board[i][j] = Integer.parseInt(st.nextToken());
}
}

// 초기 영양제 주입
pills.offer(new Node(N-1, 0));
pills.offer(new Node(N-1, 1));
pills.offer(new Node(N-2, 0));
pills.offer(new Node(N-2, 1));

while(M-- >0){
st = new StringTokenizer(br.readLine());
int d = Integer.parseInt(st.nextToken())-1;
int p = Integer.parseInt(st.nextToken());

// 1. 영양제 움직이기
movePill(d, p);

// 2. 리브로수 성장
growTree();

// 3. 새로운 특수 영양제 투입
injectPill();
}

// 4. 리브로수 높이 총 합
int ans = 0;
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
ans+=board[i][j];
}
}

System.out.println(ans);

}

static class Node{
int x;
int y;
Node(int x, int y){
this.x = x;
this.y = y;
}
}
}