Skip to content

[강신지-16주차 알고리즘 스터디] #30

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

Open
wants to merge 15 commits into
base: akstp1717/week16
Choose a base branch
from

Conversation

ksinji
Copy link
Contributor

@ksinji ksinji commented May 18, 2025

🚀 싸피 15반 알고리즘 스터디 16주차 [강신지]

📌 문제 풀이 개요

  • 이번 PR에서는 다음 5문제의 풀이를 포함합니다.
  • 각 문제에 대한 풀이 과정과 접근 방식을 설명합니다.

✅ 문제 해결 여부

  • 문제 1
  • 문제 2
  • 문제 3
  • 문제 4
  • 문제 5

💡 풀이 방법

문제 1: 자리배정

문제 난이도
실버 3

문제 유형
구현, 브루트포스

접근 방식 및 풀이
극장 좌석을 2차원 배열로 두고, 좌측 하단에서 시작해 상우하좌 순서로 방향을 바꿔가며 번호를 채워 넣는 방식으로 K번째 관객의 위치를 찾아 해결했습니다.

  int[][] map = new int[R][C];
  int[] dx = {-1, 0, 1, 0}; // 상 우 하 좌
  int[] dy = {0, 1, 0, -1};

  int x = R - 1, y = 0, dir = 0;
  map[x][y] = 1;

  for (int i = 2; i <= K; i++) {
      int nx = x + dx[dir];
      int ny = y + dy[dir];

      // 범위 밖이거나 이미 앉은 자리면 방향 전환
      if (nx < 0 || ny < 0 || nx >= R || ny >= C || map[nx][ny] != 0) {
          dir = (dir + 1) % 4;
          nx = x + dx[dir];
          ny = y + dy[dir];
      }

      map[nx][ny] = i;
      x = nx;
      y = ny;
  }

문제 2: 걷기

문제 난이도
실버 3

문제 유형
수학

접근 방식 및 풀이
세 가지 이동 방식(직선만, 대각선만, 직선+대각선 조합)의 비용을 각각 계산하여 최솟값을 구하는 방식으로 해결했습니다.

// 모두 가로/세로로만 걷는 경우
long case1 = (x + y) * w;

// 모두 대각선으로만 걷는 경우
long max = Math.max(x, y);
long min = Math.min(x, y);
long case2 = 0;

if ((x + y) % 2 == 0) {
    // 짝수라면 전부 대각선 이동 가능
    case2 = Math.max(x, y) * s;
} else {
    // 아닐 경우 대각선으로 max-1 까지 가고, 남은 1칸은 w
    case2 = (max - 1) * s + w;
}

// min만큼 대각선 + 나머지는 직선
long case3 = min * s + (max - min) * w;

// 셋 중 최솟값 선택
minCost = Math.min(case1, Math.min(case2, case3));

문제 3: 빙고

문제 난이도
실버 4

문제 유형
시뮬레이션

접근 방식 및 풀이
사회자가 부른 숫자를 하나씩 체크하며, 체크된 위치를 기준으로 가로, 세로, 대각선의 빙고 라인을 매 호출마다 검사했습니다. 빙고가 3줄 이상 완성되는 순간 호출 횟수를 출력하는 방식으로 해결했습니다.

    // 숫자 찾아 체크
    static void mark(int num) {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (board[i][j] == num) {
                    marked[i][j] = true;
                    return;
                }
            }
        }
    }

    // 빙고 줄 수 카운트
    static int countBingo() {
        int count = 0;

        // 가로
        for (int i = 0; i < 5; i++) {
            boolean bingo = true;
            for (int j = 0; j < 5; j++) {
                if (!marked[i][j]) bingo = false;
            }
            if (bingo) count++;
        }

        // 세로
        for (int j = 0; j < 5; j++) {
            boolean bingo = true;
            for (int i = 0; i < 5; i++) {
                if (!marked[i][j]) bingo = false;
            }
            if (bingo) count++;
        }

        // 대각선 '\'
        boolean diag1 = true;
        for (int i = 0; i < 5; i++) {
            if (!marked[i][i]) diag1 = false;
        }
        if (diag1) count++;

        // 대각선 '/'
        boolean diag2 = true;
        for (int i = 0; i < 5; i++) {
            if (!marked[i][4 - i]) diag2 = false;
        }
        if (diag2) count++;

        return count;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant