首页 > 其他分享 >hdu:A strange lift(bfs)

hdu:A strange lift(bfs)

时间:2023-08-27 15:46:32浏览次数:43  
标签:hdu floor int lift bfs vis strange input

Problem Description
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button “UP” , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button “DOWN” , you will go down Ki floor,i.e,you will 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 is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button “UP”, and you’ll go up to the 4 th floor,and if you press the button “DOWN”, the lift can’t do it, because it can’t go down to the -2 th floor,as you know ,the -2 th floor isn’t exist.
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button “UP” or “DOWN”?

Input
The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,….kn.
A single 0 indicate the end of the input.

Output
For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can’t reach floor B,printf “-1”.

Sample input
5 1 5
3 3 1 2 5
0
Sample output
3

bfs

点击查看代码
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
#define x first
#define y second
int n,a,b;
const int N=210;
int k[N],vis[N];

void bfs()
{
	queue<PII> q;
	q.push({a,0});
	memset(vis,0,sizeof vis);
	while(q.size())
	{
		auto pp=q.front();
		q.pop();
		int p=pp.x,step=pp.y;
		vis[p]=1;
		if(p==b) {cout<<step<<'\n';return ;}
		int up=p+k[p],down=p-k[p];
		if(up>=1&&up<=n&&!vis[up]) q.push({up,step+1});
		if(down>=1&&down<=n&&!vis[down]) q.push({down,step+1});
	}
	cout<<"-1\n";
}

int main()
{
    while(cin>>n>>a>>b,n)
    {
    	for(int i=1;i<=n;++i) cin>>k[i];
    	bfs();
    }
    return 0;
}

标签:hdu,floor,int,lift,bfs,vis,strange,input
From: https://www.cnblogs.com/ruoye123456/p/17660353.html

相关文章

  • leetcode 题库994——bfs典型解法(队列+递归实现)
     classSolution:deforangesRotting(self,grid:list[list[int]])->int:m,n=len(grid),len(grid[0])queue,good=[],[]defbfs(queue,good,m,n,grid):times=0directions=[(-1,0),(1,0),(0,1),(0,-1)]......
  • hdu:一个人的旅行
    ProblemDescription虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中会遇见很多人(白马王子,0),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽......
  • hdu:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
    ProblemDescription急!灾区的食物依然短缺!为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买。请问:你用有限的资金最多能采购多少公斤粮食呢?后记:人生是一个充满了......
  • hdu:Piggy-Bank(背包)
    ProblemDescriptionBeforeACMcandoanything,abudgetmustbepreparedandthenecessaryfinancialsupportobtained.ThemainincomeforthisactioncomesfromIrreversiblyBoundMoney(IBM).Theideabehindissimple.WheneversomeACMmemberhasany......
  • hdu:免费馅饼
    ProblemDescription都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能......
  • hdu:搬寝室
    ProblemDescription搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆,因为n是一个小于2000的整数,实在是太多了,于是xhd决定随便搬2k件过去就行了.但还是会很累,因为2k也不小是一个不大......
  • hdu:不容易系列之(3)—— LELE的RPG难题
    ProblemDescription人称“AC女之杀手”的超级偶像LELE最近忽然玩起了深沉,这可急坏了众多“Cole”(LELE的粉丝,即”可乐”),经过多方打探,某资深Cole终于知道了原因,原来,LELE最近研究起了著名的RPG难题:有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要......
  • hdu:畅通工程(并查集)
    ProblemDescription某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?Input测试输入包含若干......
  • hdu:田忌赛马(贪心,双指针)
    ProblemDescription“田忌赛马”是中国历史上一个著名的故事。大约2300年前,齐国大将田忌喜欢和国王赛马,并且约定:每赢一场,对方就要付200元。假设已知田忌和国王的各自马匹的速度都不相同,请计算田忌最好的结果是什么。Input输入包含多组测试样例。每组样例的第一行是一个整数......
  • hdu:老鼠和猫的交易(贪心)
    ProblemDescription小老鼠准备了M磅的猫粮,准备去和看守仓库的猫做交易,因为仓库里有小老鼠喜欢吃的五香豆。仓库有N个房间;第i个房间有J[i]磅的五香豆,并且需要用F[i]磅的猫粮去交换;老鼠不必交换该房间所有的五香豆,换句话说,它可以用F[i]a%磅的猫粮去换取J[i]a%磅的五香豆,其......