From 82bdab7e6fe4873ad658f1c5e9456bae5f48c445 Mon Sep 17 00:00:00 2001 From: Junseo Kim Date: Thu, 4 Jul 2024 06:56:25 +0900 Subject: [PATCH] =?UTF-8?q?2024-07-04=20=EC=83=81=EC=96=B4=EC=B4=88?= =?UTF-8?q?=EB=93=B1=ED=95=99=EA=B5=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oesnuj/README.md | 1 + "oesnuj/\352\265\254\355\230\204/21608.js" | 78 ++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 "oesnuj/\352\265\254\355\230\204/21608.js" diff --git a/oesnuj/README.md b/oesnuj/README.md index f16f53f..2513849 100644 --- a/oesnuj/README.md +++ b/oesnuj/README.md @@ -11,4 +11,5 @@ | 7차시 | 2024.05.08 | 스택, 큐, 덱 | [queuestack](https://www.acmicpc.net/problem/24511) | [#24](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/24) | | 8차시 | 2024.05.13 | 우선순위 큐 | [카드 정렬하기](https://www.acmicpc.net/problem/1715) | [#27](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/27) | | 9차시 | 2024.05.30 | 구현 | [빙고](https://www.acmicpc.net/problem/2578) | [#30](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/30) | +| 10차시 | 2024.07.04 | 구현 | [상어초등학교](https://www.acmicpc.net/problem/21608) | [#34](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/34) | --- diff --git "a/oesnuj/\352\265\254\355\230\204/21608.js" "b/oesnuj/\352\265\254\355\230\204/21608.js" new file mode 100644 index 0000000..6c471d4 --- /dev/null +++ "b/oesnuj/\352\265\254\355\230\204/21608.js" @@ -0,0 +1,78 @@ +const fs = require('fs'); +const filepath = process.platform === 'linux' ? '/dev/stdin' : './input.txt'; +const input = fs.readFileSync(filepath).toString().trim().split('\n'); +//입력 데이터 정리 +const N = +input.shift(); +const students = input.map(x => { + const [num, ...likes] = x.trim().split(' ').map(Number); + return { num, likes }; +}); + +let board = Array.from({ length: N }, () => Array(N).fill(0)); + +const dr = [-1, 1, 0, 0]; +const dc = [0, 0, -1, 1]; + +main(); + +function main(){ + for(let i=0; i< N**2; i++){ + if(i == 0){ + board[1][1] = students[i].num; //첫학생은 1,1에 무조건 앉는다. + continue; + } + choiceSeat(students[i].num); //학생을 조건에 맞게 앉히기 + } + console.log(calcSatisfy()); //모두 앉은 후 만족도 계산하기 +} + +// 최적의 자리 선택 및 학생 배치 함수 +function choiceSeat(studentNum){ + const neighborInfos = []; //인접 자리 정보를 모으는 배열 + for(let i = 0; i { + if (a.match !== b.match) { + return b.match - a.match; // match 기준 내림차순 정렬 + } else if (a.empty !== b.empty) { + return b.empty - a.empty; // empty 기준 내림차순 정렬 + } else if (a.r !== b.r) { + return a.r - b.r; // r 기준 오름차순 정렬 + } else { + return a.c - b.c; // c 기준 오름차순 정렬 + } + }); + board[neighborInfos[0].r][neighborInfos[0].c] = studentNum; //최적의 위치에 앉히기 +} + +// 특정 자리의 인접 정보 계산 함수 +function getSeatInfo(r, c, studentNum){ + let empty = 0; + let match = 0; + // 학생 번호에 맞는 좋아하는 학생들 찾기 + let studentLikes = students.find(student => student.num === studentNum)?.likes || []; + for(let i = 0; i< 4; i++){ + nr = r + dr[i]; + nc= c + dc[i]; + if(nr < 0 || nc < 0 || nr >= N || nc >= N) continue; + if (board[nr][nc] == 0) empty++; + else if(studentLikes.includes(board[nr][nc])) match++ + } + return { r: r, c: c, empty: empty, match: match }; +} + +//만족도 처리 +function calcSatisfy(){ + let result = 0; + for(let i = 0; i