//https://www.acmicpc.net/problem/5014 #include <iostream> |
#include <queue> |
using namespace std; |
int BD[1000001]; |
int F,S,G,U,D; |
int mn=10000003; |
bool bfs(){ |
queue<int> q; |
q.push(S); |
BD[S]=1; |
while(!q.empty()){ |
int now=q.front(); |
q.pop(); |
if(now==G){ |
if(mn>BD[now]) mn=BD[now]; |
return true; |
} |
int nu=now+U; |
int nd=now-D; |
if(nu>0 && nu<=F && !BD[nu]){ |
q.push(nu); |
BD[nu]=BD[now]+1; |
} |
if(nd>0 && nd<=F && !BD[nd]){ |
q.push(nd); |
BD[nd]=BD[now]+1; |
} |
} |
return false; |
} |
int main(){ |
cin >> F >> S >> G >> U >> D; |
if(bfs()) cout << mn-1; |
else cout << "use the stairs"; |
return 0; |
} |
'알고리즘 문제 풀이 > 1DP_과제(~180615)' 카테고리의 다른 글
180208_1963_소수 경로 (0) | 2018.02.08 |
---|---|
180207_2606_바이러스 (0) | 2018.02.07 |
180205_2589_보물섬 (0) | 2018.02.05 |
180203_1010_다리 놓기 (0) | 2018.02.03 |
180202_2146_다리 만들기 (0) | 2018.02.02 |
//https://www.acmicpc.net/problem/5014 #include <iostream> |
#include <queue> |
using namespace std; |
int BD[1000001]; |
int F,S,G,U,D; |
int mn=10000003; |
bool bfs(){ |
queue<int> q; |
q.push(S); |
BD[S]=1; |
while(!q.empty()){ |
int now=q.front(); |
q.pop(); |
if(now==G){ |
if(mn>BD[now]) mn=BD[now]; |
return true; |
} |
int nu=now+U; |
int nd=now-D; |
if(nu>0 && nu<=F && !BD[nu]){ |
q.push(nu); |
BD[nu]=BD[now]+1; |
} |
if(nd>0 && nd<=F && !BD[nd]){ |
q.push(nd); |
BD[nd]=BD[now]+1; |
} |
} |
return false; |
} |
int main(){ |
cin >> F >> S >> G >> U >> D; |
if(bfs()) cout << mn-1; |
else cout << "use the stairs"; |
return 0; |
} |
'알고리즘 문제 풀이 > 1DP_과제(~180615)' 카테고리의 다른 글
180208_1963_소수 경로 (0) | 2018.02.08 |
---|---|
180207_2606_바이러스 (0) | 2018.02.07 |
180205_2589_보물섬 (0) | 2018.02.05 |
180203_1010_다리 놓기 (0) | 2018.02.03 |
180202_2146_다리 만들기 (0) | 2018.02.02 |