From 4029532a40d610eb79fb5e87f63d888c13094196 Mon Sep 17 00:00:00 2001 From: wshyhs Date: Mon, 27 Oct 2025 15:08:05 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=EC=83=81=EC=96=B4=EC=B4=88=EB=93=B1?= =?UTF-8?q?=ED=95=99=EA=B5=90=20=EC=BD=94=EB=93=9C=20=EC=97=85=EB=A1=9C?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Main.java" | 126 ++++++++++++++++++ .../README.md" | 0 2 files changed, 126 insertions(+) create mode 100644 "Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/Main.java" create mode 100644 "Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/README.md" diff --git "a/Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/Main.java" "b/Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/Main.java" new file mode 100644 index 0000000..736a107 --- /dev/null +++ "b/Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/Main.java" @@ -0,0 +1,126 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.StringTokenizer; + +public class Main { + static int [][] graph; + static int N; // 반의 크기 (N*N) + static int[] dr={-1,1,0,0}; + static int[] dc={0,0,-1,1}; + static Map love; //<학생번호, 학생이 좋아하는 친구들의 번호> + static class Seat { //자리에 대한 정보 + int r,c, empty, friend; + + public Seat(int r, int c, int empty, int friend) { + this.r = r; + this.c = c; + this.empty = empty; + this.friend = friend; + } + + } + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + int studentNum= (int)Math.pow(N, 2); + graph = new int[N][N]; + love = new HashMap<>(); + + for(int i=0; i seats = new ArrayList<>(); //모든 자리의 정보를 담을 리스트 + + for(int r=0; r=N || nc>= N) continue; //out of range + for(int i=0; i<4; i++){ + if(graph[nr][nc]==friends[i]){ //r,c 주변 4방위 중 좋아하는 친구가 있다면 + friendCnt++; //좋아하는 친구 +1 + } + } + if(graph[nr][nc]==0) emptyCnt++; // 빈 칸이라면 빈칸 +1 + } + + seats.add(new Seat(r, c, emptyCnt, friendCnt)); //해당 정보를 리스트에 넣어준다 + } + } + + seats.sort((o1,o2)->{//1.친구가 많은 자리 2.빈칸이 많은자리 3. 행이 작은 자리 4. 열이 작은 자리 + if(o1.friend == o2.friend){ + if(o1.empty == o2.empty){ + if(o1.r == o2.r){ + return o1.c - o2.c; //친구, 빈 칸, 행이 같다면 열로 오름차순 + } + return o1.r - o2.r; //친구, 빈 칸이 같다면 열로 오름차순 + } + return o2.empty - o1.empty; //친구가 같다면 빈칸으로 내림차순 + } + return o2.friend - o1.friend; //친구로 내림차순 + }); + + for(Seat s : seats){ + graph[s.r][s.c] = student; //가장 처음 seat가 best seat이므로 해당 칸에 학생을 배치하고 return + return; + } + } + + static int getScore(){ + int score=0; + for(int r=0; r=N || nc>=N ) continue; + + for(int i=0; i<4; i++){ + if(graph[nr][nc]==friends[i]){ + cnt++; //해당 주위에 좋아하는 친구가 있다면 cnt ++ + } + } + } + if(cnt>0){ + score+= Math.pow(10, cnt-1); //10의 (cnt-1)제곱을 score에 더해준다. + } + } + } + + + return score; + } + +} diff --git "a/Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/README.md" "b/Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/README.md" new file mode 100644 index 0000000..e69de29 From 37894ad5d2528a61c1558d1c51ad6010fe962936 Mon Sep 17 00:00:00 2001 From: Hyeonsu KIM Date: Mon, 27 Oct 2025 15:38:07 +0900 Subject: [PATCH 2/3] Create README.md for student seating problem Added a README file explaining the problem, ideas, implementation details, and exploration strategy for seating students in a classroom based on their preferences. --- .../README.md" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git "a/Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/README.md" "b/Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/README.md" index e69de29..71f3eae 100644 --- "a/Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/README.md" +++ "b/Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/README.md" @@ -0,0 +1,37 @@ +## 문제 이해 +* 교실은 N*N의 격자 나타낼 수 있고 (r, c)는 r행 c열을 의미한다. +* 학생은 1번부터 N^2번까지 번호가 매겨져 있다. +* 학생의 번호와 그 학생이 좋아하는 학생 4명의 번호가 주어진다. +* 다음과 같은 규칙을 이용해 정해진 순서대로 학생의 자리를 정하려고 한다. +* 한 칸에는 학생 한 명의 자리만 있을 수 있고, |r1 - r2| + |c1 - c2| = 1을 만족하는 두 칸이 (r1, c1)과 (r2, c2)를 인접한다고 한다. + +* 1. 비어있는 칸 중에서 좋아하는 학생이 인접한 칸에 가장 많은 칸으로 자리를 정한다. +* 2. 1을 만족하는 칸이 여러 개이면, 인접한 칸 중에서 비어있는 칸이 가장 많은 칸으로 자리를 정한다. +* 3. 2를 만족하는 칸도 여러 개인 경우에는 행의 번호가 가장 작은 칸으로, 그러한 칸도 여러 개이면 열의 번호가 가장 작은 칸으로 자리를 정한다. + +* 최종적으로 학생의 만족도를 구한다. +* 학생의 만족도를 구하려면 그 학생과 인접한 칸에 앉은 좋아하는 학생의 수를 구해야 한다. +* 그 값이 0이면 학생의 만족도는 0, 1이면 1, 2이면 10, 3이면 100, 4이면 1000이다. + +## 아이디어 +1. 완전탐색, Map +2. 자리의 정보에 대한 Class생성. 자리배치 Method 구현. +## 구현 +### 상태 +* `Seat` : 자리에 대한 정보를 저장할 Class. 자리의 좌표, 자리에 인접한 빈자리의 수, 자리에 인접한 좋아하는 친구의 수를 저장한다. +* `graph` : 현재 교실의 배치 상태(2차원 배열). (0이면 빈자리) +* `love` : HashMap Key : 학생의 번호 , Value : 학생이 좋아하는 학생 번호 +* `putStudentSeat` : 학생을 교실에 자리배치 하는 Method +* `getScore` : 만족도를 조사하는 Method + + +### 탐색 +* `putStudentSeat`의 파라미터에 교실에 배치할 학생의 번호를 넘겨준다. +* `freinds` 배열에 해당 학생의 좋아하는 학생들을 넣어준다. +* 전체 좌표를 탐색하며 해당 좌표가 비어있다면, 그 좌표의 주변을 분석하여 empty정보와 friend정보를 `Seat seat`객체에 넣어서 `ArrayList seats`에 넣어준다. +* 모든 좌표를 탐색했다면, `Seats`리스트를 문제 조건에 맞게 정렬한다. +* 정렬 후 가장 첫번째 객체 `seat`이 해당 학생의 자리이다. +* 이를 모든 학생에 대하여 진행한다. +* 자리 배치가 끝났다면 `getScore` Method로 최종 만족도를 구한다. +* 모든 좌표에 대하여 인접한 좋아하는 친구의 수를 구하여서 10의 (cnt-1)제곱으로 만족도를 계산하여 `score`에 더해준다. +* 최종 `score`을 반환하여 출력한다. From d4ebe2e766032b9a86a47d4ab7e28e1d98bd5b17 Mon Sep 17 00:00:00 2001 From: Hyeonsu KIM Date: Sun, 2 Nov 2025 21:14:37 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=EC=BD=94=EB=A9=98=ED=8A=B8=20=EB=B0=9B?= =?UTF-8?q?=EA=B3=A0=20=EC=88=98=EC=A0=95=20compare=EC=8B=9C=EA=B0=84=20?= =?UTF-8?q?=EB=B3=B5=EC=9E=A1=EB=8F=84=20=EC=A4=84=EC=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Main.java" | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git "a/Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/Main.java" "b/Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/Main.java" index 736a107..46c2c24 100644 --- "a/Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/Main.java" +++ "b/Essential/21608_\354\203\201\354\226\264\354\264\210\353\223\261\355\225\231\352\265\220/Main.java" @@ -12,7 +12,7 @@ public class Main { static int[] dr={-1,1,0,0}; static int[] dc={0,0,-1,1}; static Map love; //<학생번호, 학생이 좋아하는 친구들의 번호> - static class Seat { //자리에 대한 정보 + static class Seat implements Comparable{ //자리에 대한 정보 int r,c, empty, friend; public Seat(int r, int c, int empty, int friend) { @@ -22,6 +22,21 @@ public Seat(int r, int c, int empty, int friend) { this.friend = friend; } + @Override + public int compareTo(Seat o) { + //1.친구가 많은 자리 2.빈칸이 많은자리 3. 행이 작은 자리 4. 열이 작은 자리 + if(this.friend == o.friend){ + if(this.empty == o.empty){ + if(this.r == o.r){ + return this.c - o.c; //친구, 빈 칸, 행이 같다면 열로 오름차순 + } + return this.r - o.r; //친구, 빈 칸이 같다면 열로 오름차순 + } + return o.empty - this.empty; //친구가 같다면 빈칸으로 내림차순 + } + return o.friend - this.friend; //친구로 내림차순 + } + } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); @@ -51,8 +66,7 @@ public static void main(String[] args) throws IOException { static void putStudentSeat(int student){ int [] friends = love.get(student); - ArrayList seats = new ArrayList<>(); //모든 자리의 정보를 담을 리스트 - + Seat seat = null; // 그 사람의 자리 for(int r=0; r{//1.친구가 많은 자리 2.빈칸이 많은자리 3. 행이 작은 자리 4. 열이 작은 자리 - if(o1.friend == o2.friend){ - if(o1.empty == o2.empty){ - if(o1.r == o2.r){ - return o1.c - o2.c; //친구, 빈 칸, 행이 같다면 열로 오름차순 + if(seat == null){ + seat = new Seat(r, c, emptyCnt, friendCnt); + }else{ + Seat com = new Seat(r, c, emptyCnt, friendCnt); + if(seat.compareTo(com)>0){ + seat = com; } - return o1.r - o2.r; //친구, 빈 칸이 같다면 열로 오름차순 } - return o2.empty - o1.empty; //친구가 같다면 빈칸으로 내림차순 } - return o2.friend - o1.friend; //친구로 내림차순 - }); - - for(Seat s : seats){ - graph[s.r][s.c] = student; //가장 처음 seat가 best seat이므로 해당 칸에 학생을 배치하고 return - return; } + + graph[seat.r][seat.c] =student; } static int getScore(){