HDU 1548 A Strange Lift (search-deep search, wide search)

Source: Internet
Author: User
A Strange Lift Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 10384 Accepted Submission (s): 3888


Problem Description There is a strange lift. The lift can stop can at every floor as you want, and there are a number ki (0 <= ki <= N) on every floor. The lift has just, buttons:up, and down. When you are at floor i,if Press the button "up", you'll go up Ki floor,i.e,you'll go to the i+ki th floor,as the SAM E, if you press the button ' down ', you'll go down Ki floor,i.e,you'll go to the i-ki th floor. Of course, the lift can ' t go up high than n,and can ' t go down lower than 1. For example, there are a buliding with 5 floors, and K1 = 3, K2 = 3,K3 = 1,k4 = 2, K5 = 5.Begining from the 1st Floor,you Can press the button "up", and you'll go up to the 4th Floor,and if your press the button "down", the lift can ' t does it, be Cause it can ' t go down to the-2nd floor,as you know, the-2nd floor isn ' t exist.
Here comes the problem:when is on floor a,and you want to go to floor b,how many times at least he have to press the Button ' up ' or ' down '?

Input the input consists of several test Cases.,each test case contains the lines.
The first line contains three integers n, a, B (1 <= n,a,b <=) which describe above,the second line consist N int Egers k1,k2,.... kn.
A single 0 Indicate the end of the input.
Output for each case of the input output a interger, and the least times you had to press the button when you were on floor A,and You want to go to floor b.if your can ' t reach floor b,printf "-1".
Sample Input
5 1 5 3 3 1 2 5 0
Sample Output
3
Very simple deep search, pay attention to a small pruning on it.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
# Include <queue>
#include <climits>

using namespace std;

const int MAX = 202;
int Flr[max],ans;
int Dist[max];
int n,b;

void Dfs (int s,int cnt) {
	if (s>n | | s<1) return;
	if (s==b) {
		if (Cnt<ans) {
			ans = cnt;
			return;
		}
	}
	if (Cnt>=dist[s]) return;
	Dist[s] = cnt;
	DFS (s+flr[s],cnt+1);
	DFS (s-flr[s],cnt+1);
}

int main () {
	//freopen ("In.txt", "R", stdin);

	int a,i,cnt;
	while (scanf ("%d", &n)!=eof && N) {
		scanf ("%d%d", &a,&b);
		for (i=1;i<=n;++i) {
			scanf ("%d", &flr[i]);
			Dist[i] = int_max-10;
		}
		CNT = 0;
		ans = Int_max;
		DFS (A,CNT);
		if (Ans==int_max) {
			printf (" -1\n");
		} else{
			printf ("%d\n", ans);
		}
	}

    return 0;
}


My new blog address Ling Feng Technology station

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.