티스토리 뷰

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