티스토리 뷰
https://www.acmicpc.net/problem/5014
5014번: 스타트링크
첫째 줄에 F, S, G, U, D가 주어진다. (1 ≤ S, G ≤ F ≤ 1000000, 0 ≤ U, D ≤ 1000000) 건물은 1층부터 시작하고, 가장 높은 층은 F층이다.
www.acmicpc.net
class Node {
constructor(item) {
this.item = item;
this.next = null;
}
}
class Queue {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(item) {
const node = new Node(item);
if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
}
this.tail = node;
this.length += 1;
}
pop() {
const popItem = this.head;
this.head = this.head.next;
this.length -= 1;
return popItem.item;
}
}
const fs = require('fs');
const input = fs.readFileSync('./dev/stdin').toString().trim().split('\n');
const [F, S, G, U, D] = input.shift().split(' ').map(Number);
// F 총 층수
// G 목적지
// S 출발지
// U 위
// D 아래
let visited = Array(1000001).fill(Infinity);
const q = new Queue();
q.push([S, 0]);
visited[S] = 0;
while (q.length > 0) {
const [now, cnt] = q.pop();
if (now == G) {
console.log(cnt);
process.exit();
}
const up = now + U;
if (up <= F && visited[up] == Infinity) {
visited[up] = cnt + 1;
q.push([up, cnt + 1]);
}
const down = now - D;
if (down > 0 && visited[down] == Infinity) {
visited[down] = cnt + 1;
q.push([down, cnt + 1]);
}
}
console.log('use the stairs');
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 2163번: 초콜릿 자르기 (0) | 2023.06.24 |
---|---|
Node.js) 백준 10026번: 적록색약 (0) | 2023.06.22 |
Node.js) 백준 4101번: 크냐? (0) | 2023.06.21 |
Node.js) 백준 1926번: 그림 (0) | 2023.06.20 |
Node.js) 백준 2493번: 탑 (0) | 2023.06.20 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- MOD
- 서버개발
- MySQL
- 그래프
- DB 생성
- 로드나인
- 면접질문
- 면접비
- create databases;
- node.js
- create db
- 롱베케이션
- 투포인터
- 다이나믹프로그래밍
- 최소공통조상
- 은둔청년체험
- KMP
- 투포인터 연습
- 동적프로그래밍
- 다이나밍프로그래밍
- BFS
- 서버점검
- 개발자면접
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함