티스토리 뷰

https://www.acmicpc.net/problem/16958

 

16958번: 텔레포트

2차원 평면 위에 N개의 도시가 있다. 일부 도시는 특별한 도시이다. (r1, c1)에 있는 도시에서 (r2, c2)에 있는 도시로 가는 이동 시간은 |r1 - r2| + |c1 - c2|와 같다. 만약, 두 도시가 특별한 도시라면, 텔

www.acmicpc.net

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
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
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
글 보관함