[BOJ][Java] 21608. ์ƒ์–ด ์ดˆ๋“ฑํ•™๊ต

2025. 10. 21. 11:36ยท๐Ÿ’ญ Problem Solving/Java

๋ฌธ์ œ

https://www.acmicpc.net/problem/21608

 

๋ฌธ์ œ ์ดํ•ด

  • N×N ๊ต์‹ค์˜ ๋นˆ ์ขŒ์„์— ํ•™์ƒ๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ์•‰ํžŒ๋‹ค.
  • ๊ฐ ํ•™์ƒ์€ ์ข‹์•„ํ•˜๋Š” 4๋ช…์˜ ์นœ๊ตฌ๊ฐ€ ์žˆ๊ณ , ์ขŒ์„ ๋ฐฐ์น˜๋Š” ์•„๋ž˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋”ฐ๋ฅธ๋‹ค:
    1. ์ธ์ ‘ 4์นธ์— ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ์ด ๊ฐ€์žฅ ๋งŽ์€ ์ž๋ฆฌ
    2. ์œ„ ์กฐ๊ฑด์ด ๊ฐ™๋‹ค๋ฉด ์ธ์ ‘ ๋นˆ์นธ์ด ๋งŽ์€ ์ž๋ฆฌ
    3. ๊ทธ๋ž˜๋„ ๊ฐ™๋‹ค๋ฉด ํ–‰ ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ์ž๋ฆฌ
    4. ๊ทธ๋ž˜๋„ ๊ฐ™๋‹ค๋ฉด ์—ด ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ์ž๋ฆฌ
  • ๋ชจ๋“  ํ•™์ƒ์„ ๋ฐฐ์น˜ํ•œ ํ›„ ๋งŒ์กฑ๋„๋ฅผ ๊ณ„์‚ฐ:
    • ์ธ์ ‘ 4์นธ์˜ ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ ์ˆ˜์— ๋”ฐ๋ฅธ ์ ์ˆ˜ = {0, 1, 10, 100, 1000}

 

๊ตฌํ˜„ ํ๋ฆ„

  1. ์ž…๋ ฅ
    • ํ•™์ƒ ๋ฐฐ์น˜ ์ˆœ์„œ students[]
    • preferences: ๊ฐ ํ•™์ƒ → ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ 4๋ช…(Set<Integer>)
    • seats: ๊ต์‹ค ์ขŒ์„ ์ƒํƒœ (N×N)
  2. ๋ฐฐ์น˜
    • findSeat(student)์—์„œ ๋ชจ๋“  ๋นˆ์นธ์„ ๊ฒ€์‚ฌํ•ด ์šฐ์„ ์ˆœ์œ„์— ๊ฐ€์žฅ ๋งž๋Š” ์ขŒ์„์„ ์„ ํƒ
    • ์„ ํƒ๋œ ์ขŒ์„์— ํ•™์ƒ์„ ์•‰ํž˜
  3. ์ถœ๋ ฅ
    • calcSum()์œผ๋กœ ์ตœ์ข… ๋งŒ์กฑ๋„ ํ•ฉ์„ ๊ณ„์‚ฐํ•ด ์ถœ๋ ฅ

 

ํด๋ž˜์Šค/ํ•จ์ˆ˜๋ณ„ ์—ญํ• 

Seat

  • ์ขŒ์„ ์œ„์น˜: r,c
  • ์ธ์ ‘ ์ข‹์•„ํ•˜๋Š” ์นœ๊ตฌ ์ˆ˜: prefer
  • ์ธ์ ‘ ๋นˆ์นธ ์ˆ˜: empty
  • ์ •๋ ฌ(์šฐ์„ ์ˆœ์œ„)์„ compareTo๋กœ ํ‘œํ˜„:
    if (prefer != other.prefer)
        return Integer.compare(other.prefer, prefer);
    if (empty != other.empty)
        return Integer.compare(other.empty, empty);
    if (r != other.r)
        return Integer.compare(r, other.r);
    return Integer.compare(c, other.c);

findSeat(int student)

  • ๋ชจ๋“  ๋นˆ์นธ์„ ์ˆœํšŒํ•˜๋ฉฐ Seat(i,j, prefer, empty)๋ฅผ ๊ณ„์‚ฐ
  • Comparable ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์žฅ ์šฐ์„ ์ˆœ์œ„ ๋†’์€ ์ขŒ์„์„ ์„ ํƒํ•ด ์ฐฉ์„

findPrefer(int student, int r, int c)

  • (r,c) 4๋ฐฉํ–ฅ์„ ํ™•์ธํ•˜์—ฌ, ๊ทธ ์นธ์— ์•‰์•„์žˆ๋Š” ํ•™์ƒ์ด student๊ฐ€ ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ์ด๋ฉด ์นด์šดํŠธ

findEmpty(int r, int c)

  • (r,c) 4๋ฐฉ์˜ ๋นˆ์นธ(=0) ๊ฐœ์ˆ˜ ์นด์šดํŠธ

calcSum()

  • ๋ชจ๋“  ์นธ์— ๋Œ€ํ•ด findPrefer(seats[i][j], i, j)๋ฅผ ๋‹ค์‹œ ๊ตฌํ•ด ์ ์ˆ˜๋กœ ํ™˜์‚ฐ ํ›„ ๋ˆ„์ 

๋ณต์žก๋„

  • ๊ฐ ํ•™์ƒ์— ๋Œ€ํ•ด N²๊ฐœ์˜ ๋นˆ์นธ์„ ๊ฒ€์‚ฌ, ๋นˆ์นธ๋‹น 4๋ฐฉ ํ™•์ธ(์ƒ์ˆ˜)
    → ์ „์ฒด ์•ฝ O(Nโด) (N ≤ 20์ด๋ผ์„œ ํ†ต๊ณผ)

 

์ฝ”๋“œ

import java.util.*;
import java.io.*;

public class Main {
    static class Seat implements Comparable<Seat> {
        int r, c, prefer, empty;

        public Seat(int r, int c, int prefer, int empty) {
            this.r = r;
            this.c = c;
            this.prefer = prefer;
            this.empty = empty;
        }

        public int compareTo(Seat other) {
            if (prefer != other.prefer)
                return Integer.compare(other.prefer, prefer);

            if (empty != other.empty)
                return Integer.compare(other.empty, empty);

            if (r != other.r)
                return Integer.compare(r, other.r);

            return Integer.compare(c, other.c);
        }
    }

    static int n;
    static int[] students;
    static int[][] seats;
    static Map<Integer, Set<Integer>> preferences;
    static int[] dr = { -1, 1, 0, 0 };
    static int[] dc = { 0, 0, -1, 1 };

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        n = Integer.parseInt(br.readLine());
        students = new int[n * n];
        seats = new int[n][n];
        preferences = new HashMap<>();

        for (int i = 0; i < n * n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int student = Integer.parseInt(st.nextToken());
            students[i] = student;
            Set<Integer> set = new HashSet<>();
            for (int j = 0; j < 4; j++) {
                set.add(Integer.parseInt(st.nextToken()));
            }
            preferences.put(student, set);
        } // ์ž…๋ ฅ ๋

        for (int i = 0; i < n * n; i++) {
            findSeat(students[i]);
        }

        System.out.println(calcSum());
    }

    static void findSeat(int student) {
        Seat seat = null;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (seats[i][j] > 0) // ์ด๋ฏธ ์•‰์€ ์ž๋ฆฌ
                    continue;

                Seat cur = new Seat(i, j, findPrefer(student, i, j), findEmpty(i, j)); // ๋น„๊ตํ•  ์ขŒ์„

                if (seat == null)
                    seat = cur;

                if (seat.compareTo(cur) > 0) {
                    seat = cur;
                }
            }
        }

        seats[seat.r][seat.c] = student;
    }

    static int findPrefer(int student, int r, int c) {
        int cnt = 0;

        for (int i = 0; i < 4; i++) {
            int nr = r + dr[i];
            int nc = c + dc[i];

            if (nr < 0 || nc < 0 || nr >= n || nc >= n)
                continue;

            if (preferences.get(student).contains(seats[nr][nc]))
                cnt++;
        }

        return cnt;
    }

    static int findEmpty(int r, int c) {
        int cnt = 0;

        for (int i = 0; i < 4; i++) {
            int nr = r + dr[i];
            int nc = c + dc[i];

            if (nr < 0 || nc < 0 || nr >= n || nc >= n)
                continue;

            if (seats[nr][nc] == 0)
                cnt++;
        }

        return cnt;
    }

    static int calcSum() {
        int ans = 0;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                ans += Math.pow(10, findPrefer(seats[i][j], i, j) - 1);
            }
        }

        return ans;
    }
}

'๐Ÿ’ญ Problem Solving > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ][Java] 18808. ์Šคํ‹ฐ์ปค ๋ถ™์ด๊ธฐ  (0) 2025.11.08
[BOJ][Java] 11437. LCA  (0) 2025.10.28
[BOJ][Java] 20056. ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด์™€ ํŒŒ์ด์–ด๋ณผ  (0) 2025.10.15
[BOJ][Java] 16236. ์•„๊ธฐ ์ƒ์–ด  (0) 2025.10.01
[BOJ][Java] 2533. ์‚ฌํšŒ๋ง ์„œ๋น„์Šค(SNS)  (0) 2025.09.24
'๐Ÿ’ญ Problem Solving/Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [BOJ][Java] 18808. ์Šคํ‹ฐ์ปค ๋ถ™์ด๊ธฐ
  • [BOJ][Java] 11437. LCA
  • [BOJ][Java] 20056. ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด์™€ ํŒŒ์ด์–ด๋ณผ
  • [BOJ][Java] 16236. ์•„๊ธฐ ์ƒ์–ด
0=2.
0=2.
  • 0=2.
    0=2
    0=2.
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (104)
      • ๐Ÿ“‚ Project (2)
        • Paint the City (2)
      • ๐Ÿ’ญ Problem Solving (42)
        • C++ (28)
        • Java (14)
      • ๐Ÿ“ Study (17)
        • React (1)
        • Java (16)
      • ๐Ÿ’ป CS (11)
        • ๋ฉด์ ‘์„ ์œ„ํ•œ CS ์ „๊ณต์ง€์‹ ๋…ธํŠธ (2)
        • ์ •๋ณด์ฒ˜๋ฆฌ๊ธฐ์‚ฌ (9)
      • ๐Ÿƒ‍โ™€๏ธ Activities (32)
        • Web Front-End Basic Study (6)
        • 42 Cursus (26)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
    • ๊ธ€์“ฐ๊ธฐ
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    react
    dynamic programming
    ๋ฐฑ์ค€
    github
    ์‹œ๋ฎฌ๋ ˆ์ด์…˜
    knapsack
    C
    BFS
    dfs
    swea
    HTML
    ๋ธŒ๋ฃจํŠธํฌ์Šค
    makefile
    .h
    unity
    ์ •๋ณด์ฒ˜๋ฆฌ๊ธฐ์‚ฌ
    ํŠธ๋ฆฌ
    42๊ฒฝ์‚ฐ
    CSS
    VR
    La Piscine
    ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜
    ์ •๋ ฌ
    java
    CS
    ๋งต
    ๋ฐฑํŠธ๋ž˜ํ‚น
    ๊ตฌํ˜„
    ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
    git
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
0=2.
[BOJ][Java] 21608. ์ƒ์–ด ์ดˆ๋“ฑํ•™๊ต
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”