Skip to content

Commit

Permalink
Merge pull request #34 from AlgoLeadMe/10-oesnuj
Browse files Browse the repository at this point in the history
10-oesnuj
  • Loading branch information
oesnuj authored Jul 10, 2024
2 parents d59dc08 + 82bdab7 commit 01519b8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions oesnuj/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
---
78 changes: 78 additions & 0 deletions oesnuj/๊ตฌํ˜„/21608.js
Original file line number Diff line number Diff line change
@@ -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<N; i++){
for(let j =0; j<N; j++){
if(board[i][j] !== 0) continue; //์ด๋ฏธ ์ฐจ์žˆ๋Š” ์ž๋ฆฌ ํŒจ์Šค
neighborInfos.push(getSeatInfo(i, j, studentNum));
}
}
neighborInfos.sort((a, b) => {
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<N; i++){ //๋‚จ์•„์žˆ๋Š” ๋ชจ๋“  ์ž๋ฆฌ์˜ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘
for(let j =0; j<N; j++){
result += Math.floor(10 ** (getSeatInfo(i, j ,board[i][j]).match-1));
}
}
return result;
}
์ถœ์ฒ˜: https://oesnuj.tistory.com/entry/Nodejs-๋ฐฑ์ค€-Javascript-21608-์ƒ์–ด-์ดˆ๋“ฑํ•™๊ต [๋น„ํŠธ๋กœ ๊ทธ๋ฆฌ๋Š” ์„ฑ์žฅ์ผ๊ธฐ:ํ‹ฐ์Šคํ† ๋ฆฌ]

0 comments on commit 01519b8

Please sign in to comment.