티스토리 뷰
https://www.acmicpc.net/problem/16958
const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n").map(v=>v.split(' ').map(Number));
const [N,T] = input.shift()
const cities = [];
for(let i = 0; i<N; i++){
const [s,x,y] = input.shift();
const city = {
x:x,
y:y,
s:s
}
cities.push(city)
}
const answer = [];
const [M] = input.shift();
for(let i = 0; i<M; i++){
const [a,b] = input.shift();
const A = cities[a-1]
const B = cities[b-1]
let rumTime = Math.abs(A.x-B.x)+Math.abs(A.y-B.y)
if(A.s==1 && B.s==1){
if(T<rumTime) rumTime = T;
}else if(A.s==1 && B.s==0){
const specialCity = cities.filter(v=>v.s==1);
specialCity.forEach(C=>{
let tempRunTime = Math.abs(C.x-B.x)+Math.abs(C.y-B.y)+T
if(tempRunTime<rumTime) rumTime = tempRunTime;
})
}else if(A.s==0 && B.s==1){
const specialCity = cities.filter(v=>v.s==1);
specialCity.forEach(C=>{
let tempRunTime = Math.abs(C.x-A.x)+Math.abs(C.y-A.y)+T
if(tempRunTime<rumTime) rumTime = tempRunTime;
})
}else{
let minA = Infinity;
let minB = Infinity;
const specialCity = cities.filter(v=>v.s==1);
specialCity.forEach(C=>{
let tempRunTimeA = Math.abs(C.x-A.x)+Math.abs(C.y-A.y)
let tempRunTimeB = Math.abs(C.x-B.x)+Math.abs(C.y-B.y)
if(tempRunTimeA<minA) minA = tempRunTimeA;
if(tempRunTimeB<minB) minB = tempRunTimeB;
})
const totlaTime = minA+minB+T;
if(rumTime>totlaTime) rumTime = totlaTime;
}
answer.push(rumTime)
}
console.log(answer.join('\n'))
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 17413번: 단어 뒤집기2 (0) | 2022.01.16 |
---|---|
Node.js)백준 16936번: 나3곱2 ★ (0) | 2022.01.15 |
Node.js)백준 17089번: 세 친구 (0) | 2022.01.12 |
Node.js)백준 16924번: 십자가 찾기 (0) | 2022.01.12 |
Node.js)백준 16943번: 숫자 재배치 (0) | 2022.01.11 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 개발자면접
- 면접질문
- 은둔청년체험
- 다이나밍프로그래밍
- DB 생성
- BFS
- 로드나인
- 투포인터 연습
- 동적프로그래밍
- 다이나믹프로그래밍
- create db
- MOD
- 최소공통조상
- node.js
- 면접비
- 투포인터
- 롱베케이션
- 그래프
- MySQL
- 서버개발
- create databases;
- 서버점검
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함