HyeLog

๋ฐฑ์ค€_5014_์Šคํƒ€ํŠธ๋งํฌ ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ฐฑ์ค€_5014_์Šคํƒ€ํŠธ๋งํฌ

shj718 2022. 7. 6. 21:13

๐Ÿ™‹‍โ™€๏ธ ๋ฌธ์ œ

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

 

๐Ÿงฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜

์‹œ์ž‘์ ์œผ๋กœ๋ถ€ํ„ฐ ๋ชจ๋“  ์ •์ ์„ ๋ฐฉ๋ฌธํ•˜๋ฉฐ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ 1์ด๊ณ  ๋ˆŒ๋Ÿฌ์•ผ ํ•˜๋Š” ๋ฒ„ํŠผ์˜ ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉ

 

๐Ÿ’ก ์•„์ด๋””์–ด

๊ธฐ๋ณธ์ ์ธ BFS ๋ฌธ์ œ์˜€๋‹ค.

 

๐Ÿ‘ฉ‍๐Ÿ’ป ์ฝ”๋“œ

#include <iostream>
#include <queue>

using namespace std;

bool visited[1000001];
int dist[1000001];

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	// input
	int floor, start, goal, up, down;
	cin >> floor >> start >> goal >> up >> down;
	// BFS
	queue<int> q;
	q.push(start);
	visited[start] = true;
	dist[start] = 0;
	while (!q.empty()) {
		int cur = q.front();
		q.pop();
		int next1 = cur + up;
		int next2 = cur - down;
		if (next1 >= 1 && next1 <= floor && visited[next1] == false) {
			q.push(next1);
			visited[next1] = true;
			dist[next1] = dist[cur] + 1;
		}
		if (next2 >= 1 && next2 <= floor && visited[next2] == false) {
			q.push(next2);
			visited[next2] = true;
			dist[next2] = dist[cur] + 1;
		}
	}
	// output
	if (visited[goal]) {
		cout << dist[goal] << '\n';
	}
	else {
		cout << "use the stairs" << '\n';
	}
	return 0;
}