Elevator Trouble | ||||||
| ||||||
Description | ||||||
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 f 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". | ||||||
Input | ||||||
There are several test cases. For each test case, 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]. | ||||||
Output | ||||||
For each test case,You must reply with the minimum numbers of pushes you must make in order to get from | ||||||
Sample Input | ||||||
10 1 10 2 1 100 2 1 1 0
| ||||||
Sample Output | ||||||
6 use the stairs
| ||||||
Source | ||||||
NCPC2011 |
一道并不是很难的搜索题,不需要剪枝、暴力搜一发就行了、
这个题和hdu 1548是一模一样的题目、大家有兴趣可以做做这个题目、
思路:从起点出发,两种走法,一种向上,一种向下,走到终点输出当前方法的步数。如果一直没有输出,就输出use the stairs
这里直接上AC代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct floor
{
int pos,output;
}now,nex;
int f,s,g,u,d;
int vis[1000005];
void bfs(int x)
{
memset(vis,0,sizeof(vis));
vis[x]=1;
now.pos=x;
now.output=0;
queue<floor>s;
s.push(now);
while(!s.empty())
{
now=s.front();
if(now.pos==g)
{
printf("%d\n",now.output);
return ;
}
s.pop();
for(int i=0;i<2;i++)
{
if(i==0)
{
nex.pos=now.pos+u;
nex.output=now.output+1;
if(nex.pos>=1&&nex.pos<=f&&vis[nex.pos]==0)
{
vis[nex.pos]=1;
s.push(nex);
}
}
if(i==1)
{
nex.pos=now.pos-d;
nex.output=now.output+1;
if(nex.pos>=1&&nex.pos<=f&&vis[nex.pos]==0)
{
vis[nex.pos]=1;
s.push(nex);
}
}
}
}
printf("use the stairs\n");
return ;
}
int main()
{
while(~scanf("%d%d%d%d%d",&f,&s,&g,&u,&d))
{
bfs(s);
}
}