250x250
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- svgํ์ผ ๋ค๋ฃจ๊ธฐ
- Redux toolkit
- DP
- ๋๋๊ทธ ์ด๋ฒคํธ
- ์๋ฐ์คํฌ๋ฆฝํธ
- router v6
- TypeScript
- ์ฝ๋ฉํ ์คํธ
- ํ๋ก๊ทธ๋๋จธ์ค
- Node.js
- ๋ถ์คํธ์ปจํผ๋ฐ์ค
- ์ฝํ
- ์นด์นด์ค
- custom hook
- ๋ฆฌ๋์ค ํดํท
- ๋ฐฑ์ค
- ์ด๋ถํ์
- JavaScript
- ๋์ ๊ณํ๋ฒ
- ์๋ฐฉํฅ ์ฐ๊ฒฐ ๋ฆฌ์คํธ
- ์ด๋ฏธ์ง ์์
- js
- ์นด์นด์ค์ฑ์ฉ
- ๋ธ๋ฃจํธํฌ์ค
- icecandidate
- ๊ณผ์ ํ ์คํธ
- ์๊ณ ๋ฆฌ์ฆ
- ๋ถ์คํธ์บ ํ์น๋ชจ๋ฐ์ผ
- ์ฝ๋ ํฌ๋ฉง
- React
Archives
- Today
- Total
๐ฅ dev-ruby
[๋ฐฑ์ค][๋ธ๋ฃจํธํฌ์ค]์นํจ ๋ฐฐ๋ฌ - node.js | javascript | gold5 ๋ณธ๋ฌธ
๋ฐฑ์ค
[๋ฐฑ์ค][๋ธ๋ฃจํธํฌ์ค]์นํจ ๋ฐฐ๋ฌ - node.js | javascript | gold5
ruby_s 2022. 4. 28. 09:46728x90
๋ฐ์ํ
SMALL
๋ฌธ์
https://www.acmicpc.net/problem/15686
ํ์ด
function solve() {
const twoIdxList = [],
oneIdxList = [];
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (city[i][j] === 2) twoIdxList.push([i, j]);
else if (city[i][j] === 1) oneIdxList.push([i, j]);
}
}
function getCombination(arr, num) {
const result = [];
if (num === 1) return arr.map((el) => [el]);
arr.forEach((fixed, index, origin) => {
const rest = origin.slice(index + 1);
const combination = getCombination(rest, num - 1);
const attached = combination.map((el) => [fixed, ...el]);
result.push(...attached);
});
return result;
}
function getMinDistance(chickenIdx) {
let sum = 0;
for (const [oneX, oneY] of oneIdxList) {
let distance = Infinity;
for (const [twoX, twoY] of chickenIdx) {
distance = Math.min(
distance,
Math.abs(oneX - twoX) + Math.abs(oneY - twoY)
);
}
sum += distance;
}
return sum;
}
const combination = getCombination(twoIdxList, m);
let answer = Infinity;
for (const indexs of combination) {
answer = Math.min(answer, getMinDistance(indexs));
}
return answer;
}
const filePath =
process.platform === "linux" ? "/dev/stdin" : "๋ฐฑ์ค/gold/15686/testcase.txt";
const input = require("fs")
.readFileSync(filePath)
.toString()
.trim()
.split("\n")
.map((row) => row.split(" ").map((el) => +el));
const [n, m] = input[0];
const city = input.slice(1);
console.log(solve());
์ด ๋ฌธ์ ๋ ์กฐํฉ๋ง ์ฐ๋ฉด ์ฝ๊ฒ ํธ๋ ๋ฌธ์ ๋ค.
์กฐํฉ์ผ๋ก ์นํจ ์ง(2) ์ค ํ์
์ํค์ง ์์ ์ธ๋ฑ์ค ์กฐํฉ๋ค์ ๋ง๋ค๊ณ , ์ง(1)์์๋ถํฐ ์นํจ ์ง๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ์ญ ๋ฐ๋ณต๋ฌธ ๋๋ ค์ ๊ตฌํ๋ฉด ๋๋ค.
Math.min ์ผ๋ก ๊ณ์ํด์ ์ต์๊ฐ์ ๋ฐ์ํด์ฃผ๋ฉด ํ์ด ๋ !
์ข ๋ ์ฌ์ด ํ์ด๋ฅผ ์์๊ฑฐ๋ ๊ณ ์ณ์ผ ํ ์ฝ๋๊ฐ ์๋ค๋ฉด ๋๊ธ๋ก ์๋ ค์ฃผ์ธ์ ! ๐
728x90
๋ฐ์ํ
LIST