-
[5014] 스타트링크BOJ 2022. 1. 2. 03:42
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..F개의 노드가 있을때, i번 노드는 i-d, i+u번 노드와 연결되어 있다고 모델링해줄 수 있다.
가중치가 전부 동일한 상황에서의 최단거리는 bfs로 구할 수 있겠다.
<소스코드>
1234567891011121314151617181920212223242526272829303132333435363738394041#include <stdio.h>#include <string.h>#include <queue>using namespace std;const int INF = 2121212121;int f, s, g, u, d;int dist[1000001];bool visit[1000001];queue<int> q;int main(void) {int i, next, cur;scanf("%d %d %d %d %d", &f, &s, &g, &u, &d); //최대, 시작, 도착, 위, 아래memset(dist, INF, sizeof(dist));memset(visit, false, sizeof(visit));dist[s] = 0;visit[s] = true;q.push(s);while (!q.empty()) {cur = q.front();q.pop();if (cur == g) break;next = cur + u;if (next >= 1 && next <= f && visit[next] == false) {visit[next] = true;q.push(next);dist[next] = dist[cur] + 1;}next = cur - d;if (next >= 1 && next <= f && visit[next] == false) {visit[next] = true;q.push(next);dist[next] = dist[cur] + 1;}}if (visit[g] == true)printf("%d", dist[g]);elseprintf("use the stairs");return 0;}cs int[]에서 memset은 -1과 0만 가능하다.
다만 위 코드는 dist에 visit[]에 의해 단 한번만 새로운 값을 대입하기에,
예측하기 어려운 값으로 초기화 되었어도 별 상관은 없다.
'BOJ' 카테고리의 다른 글
[22354] 돌 가져가기 (0) 2022.01.04 [8892] 팰린드롬 (0) 2022.01.02 [17070] 파이프 옮기기 1 (0) 2022.01.02 [1574] 룩 어택 (0) 2022.01.01 [18138] 리유나는 세일러복을 좋아해 (0) 2021.12.31