Thief of Wealth
시간 제한메모리 제한제출정답맞은 사람정답 비율
1 초256 MB103463638269234.297%

문제

You are on your way to your first job interview as a program tester, and you are already late. The interview is in a skyscraper and you are currently in floor s, where you see an elevator. Upon entering the elvator, you learn that it has only two buttons, marked "UP u" and "DOWN d". You conclude that the UP-button takes the elevator u floors up (if there aren't enough floors, pressing the UP-botton does nothing, or at least so you assume), whereas the DOWN-button takes you d stories down (or none if there aren't enough). Knowing that the interview is at floor g, and that there are only floors in the building, you quickly decide to write a program that gives you the amount of button pushes you need to perform. If you simply cannot reach the correct floor, your program halts with the message "use the stairs".

Given input f, s, g, u and d (floors, start, goal, up, down), find the shortest sequence of button presses you must press in order to get from s to g, given a building of floors, or output "use the stairs" if you cannot get from s to g by the given elevator.

입력

The input will consist of one line, namely f s g u d, where 1 ≤ s, g ≤ f ≤ 1000000 and 0 ≤ u, d ≤ 1000000. The floors are one-indexed, i.e. if there are 10 stories, s and g be in [1, 10].

출력

You must reply with the minimum numbers of pushes you must make in order to get from s to g, or output use the stairs if it is impossible given the configuration of the elvator.



bfs로 위아래 왓다리 갔다리 해서 푸는 문제다.

메모리 아낄려고 visited 배열없이 꼼수썼다가 TLE떠서 그냥 visited주고 풀었다.


#include <bits/stdc++.h>
using namespace std;

int building[1000001];
bool visited[1000001];
queue<int> route;
int main(){

freopen("input.txt","r",stdin);

ios::sync_with_stdio(false);
cin.tie(0);

int F,S,G,U,D;
cin>>F>>S>>G>>U>>D;

for(int i=0; i<1000001; ++i){ building[i]=987654321; }
memset(visited,false,sizeof(visited));
int current_floor;
int cost=0;
building[S]=0; //already visited.
route.push(S);
int step_size;
while(!route.empty()){
step_size = route.size();
for(int i=0; i<step_size; ++i){
current_floor = route.front();
route.pop();
building[current_floor] = min(building[current_floor],cost);
if( (current_floor+U)<=F && (!visited[current_floor+U]) ){
route.push(current_floor+U);
visited[current_floor+U]=!visited[current_floor+U];
}
if( (current_floor-D)>=1 && (!visited[current_floor-D])){
route.push(current_floor-D);
visited[current_floor-D] = !visited[current_floor-D];
}
}
cost++;
}
cout<< (building[G]==987654321 ? "use the stairs" : to_string(building[G]) );
return 0;
}




'개발 > 알고리즘' 카테고리의 다른 글

13549 숨바꼭질3  (1) 2019.04.27
6594 Dungeon Master  (0) 2019.04.25
2146 다리만들기  (0) 2019.04.14
5427 불  (0) 2019.04.14
2573 빙산  (0) 2019.04.14
profile on loading

Loading...