λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
κ°œμΈκ³΅λΆ€/Algorithm

[λ°±μ€€][Node.js] 1697번 : μˆ¨λ°”κΌ­μ§ˆ

by πŸ‡λ°•λ΄‰λ΄‰πŸ‡ 2021. 7. 12.

 

 

Algorithm

-  μˆ¨λ°”κΌ­μ§ˆ -

 


 

문제

μˆ˜λΉˆμ΄λŠ” 동생과 μˆ¨λ°”κΌ­μ§ˆμ„ ν•˜κ³  μžˆλ‹€. μˆ˜λΉˆμ΄λŠ” ν˜„μž¬ 점 N(0≤N≤100,000)에 있고, 동생은 점 K(0≤K≤100,000)에 μžˆλ‹€. μˆ˜λΉˆμ΄λŠ” κ±·κ±°λ‚˜ μˆœκ°„μ΄λ™μ„ ν•  수 μžˆλ‹€. λ§Œμ•½, 수빈이의 μœ„μΉ˜κ°€ X일 λ•Œ κ±·λŠ”λ‹€λ©΄ 1초 후에 X-1 λ˜λŠ” X+1둜 μ΄λ™ν•˜κ²Œ λœλ‹€. μˆœκ°„μ΄λ™μ„ ν•˜λŠ” κ²½μš°μ—λŠ” 1초 후에 2*X의 μœ„μΉ˜λ‘œ μ΄λ™ν•˜κ²Œ λœλ‹€.

 

μˆ˜λΉˆμ΄μ™€ λ™μƒμ˜ μœ„μΉ˜κ°€ μ£Όμ–΄μ‘Œμ„ λ•Œ, μˆ˜λΉˆμ΄κ°€ 동생을 찾을 수 μžˆλŠ” κ°€μž₯ λΉ λ₯Έ μ‹œκ°„μ΄ λͺ‡ 초 후인지 κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

 

μž…μΆœλ ₯ μ˜ˆμ‹œ

 

문제 ν’€κΈ°

const readline = require("readline");
const rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout,
});
let input;
let queue = [];
let visited = [];
rl.on("line", function (line) {
	input = line
		.toString()
		.split(" ")
		.map((el) => Number(el));
}).on("close", function () {
	visited = Array(100001).fill(false);
	queue = [[input[0], 0]];
	let index = 0;
	while (true) {
		if (!queue[index]) break;
		let [location, count] = queue[index++];
		if (visited[location]) continue;

		visited[location] = true;
		if (location === input[1]) {
			console.log(count);
			break;
		}

		if (location + 1 <= 100000) queue.push([location + 1, count + 1]);
		if (location - 1 >= 0) queue.push([location - 1, count + 1]);
		if (location * 2 <= 100000) queue.push([location * 2, count + 1]);
	}

	process.exit();
});

 

 

 

λ°˜μ‘ν˜•

λŒ“κΈ€