Title Description
Oh, one day I had a dream, dreamed of a very strange elevator. Elevators can be parked on each floor of the building, and there is a number Ki (0<=ki<=n) on the first floor (1<=i<=n). Elevators have only four buttons: on, off, up, down. The number of layers above and below equals that number on the current floor. Of course, if the requirements are not met, the corresponding buttons will fail. For example: 3 3 1 2 5 represents Ki (K1=3,k2=3,......), starting from the first floor. On the first floor, press "up" can go to the 4 floor, press "down" is not working, because there is no-2 floor. So, how many buttons do I have to press a few times from floor A to floor B?
input/output format
Input format:
There are two lines in the input file, the first act three positive integers separated by spaces, representing N,a,b (1≤n≤200, 1≤a,b≤n), and the second act N a positive integer separated by a space, representing Ki.
Output format:
Output file only one line, that is, the minimum number of keystrokes, if not reachable, then output-1.
Problem Solving Ideas:
Heard should use recursion, but pushed for a while and did not launch a ghost, so the DFS search.
Here's the code:
#include <iostream>
#include <cstdio>
using namespace Std;
int lift[2008],step[2008];
int n,a,b;
int main ()
{
memset (lift,0,sizeof (lift));
memset (step,0,sizeof (step));
cin>>n>>a>>b;
for (int i=1;i<=n;i++)
{
cin>>lift[i];
Step[i]=-1;
}
step[1]=0;
int l=1,p=0,j=0;
while (l>0&&p<=n)
{
l=0;
for (int i=1;i<=n;i++)
if (step[i]==p)
{
J=i+lift[i];
if (j<=n&&step[j]==-1)
{
step[j]=p+1;
l=1;
}
J=i-lift[i];
if (j>0&&step[j]==-1)
{
step[j]=p+1;
l=1;
}
}
p++;
}
cout<<step[b];
return 0;
}
Strange Elevator