HDU1548 A strange lift 奇怪的電梯(BFS+佇列)
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"?
InputThe 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. OutputFor 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
3
分析:
題意:電梯在每一層都有一個數k,只能往上k層或者往下k層。讓你求從m層到n層至少需要嗯多少次電梯按鈕。 1.最短路的變行問題,將層數看做點,能夠互相到達的層看做路徑。 2.也可以用廣搜做,搜尋到達的路徑。#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; const int MAX = 11000; struct Node { int to,step; } node,nextnode; int v[MAX],f[MAX]; int main() { int cas,n,m,i; while(scanf("%d",&cas)&&cas) { scanf("%d%d",&m,&n); memset(v,0,sizeof(v)); for(i=1; i<=cas; i++) scanf("%d",&f[i]);//輸入每層可以上下的層數 queue<Node>qu;//定義佇列 while(!qu.empty()) { qu.pop(); } int t1,t2,flag=0; node.step=0;//起始點 node.to=m; qu.push(node); v[node.to]=1; while(!qu.empty()) { node = qu.front();//取出首元素 qu.pop(); if(node.to == n)//如果到達終點 { printf("%d\n",node.step); flag=1; break; } t1=node.to+f[node.to];//向上走 if(t1<=cas&&!v[t1]) { v[t1]=1; nextnode.to=t1; nextnode.step=node.step+1; qu.push(nextnode); } t1=node.to-f[node.to];//向下走 if(t1>=1&&!v[t1]) { v[t1]=1; nextnode.to=t1; nextnode.step=node.step+1; qu.push(nextnode); } } if(!flag)//如果走不到 printf("-1\n"); } return 0; }
相關推薦
HDU1548 A strange lift 奇怪的電梯(BFS+佇列)
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.T
HDU1548 A strange lift Dijkstra BFS
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
[HDU1548] A strange lift [單源最短路/BFS]
建圖然後跑最短路,注意無解。 #include<cstdio> #include<iostream> #include<algorithm> #include<cstdlib> #include<cctype
A strange lift HDU - 1548 (已AC和未AC版本)
本題我做了兩次,第一次是在出隊的時候進行判斷,如果到達b就返回,第二次是在進隊的時候判斷,如果到達b就返回,但奇怪的是第一次可以提交,第二次的一直WA?,求大佬指點一二 版本1(可以AC) #include <stdio.h> #include
ACM/ICPC 2018亞洲區預選賽北京賽站網路賽 A. Saving Tang Monk II(bfs+dp)
時間限制:1000ms 單點時限:1000ms 記憶體限制:256MB 描述 《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese lit
HDU-1548 A strange lift(最短路[Spfa || BFS])
題目連結: http://acm.hdu.edu.cn/showproblem.php?pid=1548 參考部落格: https://blog.csdn.net/idealism_xxm/article/details/47625691 BFS: //BFS #include
hdu-1548 A strange lift---BFS
set DC ont max 至少 problem ret cout AS 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=1548 題目大意: 有一天桐桐做了一個夢,夢見了一種很奇怪的電梯。大樓的每一層樓都可以停電梯,而且第
HDU 1548 A strange lift
do it 做了 same http std ios bit 兩個 最短路 題目鏈接:HDU 1548 Description There is a strange lift.The lift can stop can at every floor as you want
Hdoj 1548.A strange lift 題解
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.
【2018 ICPC北京網賽】 A Saving Tang Monk II(BFS)
《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng’en during the Min
HDU 1548 A strange lift 題解
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33697 &nbs
hdu 1548 HDU 1548 A strange lift(Dijkstra)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 34036&nb
HDU 1548 A strange lift(Dijkstra)
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int inf = 1<<30; int n;
acm 2 1013 A strange lift
1.1013 2. 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 <
HDU2717-Catch That Cow (BFS入門)
can ble return ems script accep ant following emp 題目傳送門:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/
hdu 4771 求一點遍歷全部給定點的最短路(bfs+dfs)
int esp str 遍歷 code [1] [0 sca cstring 題目如題。題解如題。 因為目標點最多僅僅有4個,先bfs出倆倆最短路(包含起點)。再dfs最短路。)0s1A;(當年弱跪杭州之題,現看如此簡單) #include<iostream>
HDU - 1142 A Walk Through the Forest(Dijkstra+DFS)
names sin mes 兩個 blog 如果能 const esp c++ 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 題意:從1到達2,路徑要盡可能短(就先一遍dijkstra),並且要求每次距離2的路徑要
NYOJ - 1015 二部圖(bfs/dfs)
for esp 題意 const ring cto style ios line 題目鏈接:點我點我 題意:二分圖判斷問題 題解:兩種解法,模擬下匹配過程。 1 //二分圖匹配dfs 2 #include <cstring> 3 #inclu
A - 敵兵布陣(HDU 1166)
ext 查詢 desc color borde print amp truct 思路 A - 敵兵布陣 HDU - 1166 思路:線段樹單點修改+區間查詢。 #include<cstdio> #include<cstr
bzoj3212: Pku3468 A Simple Problem with Integers(線段樹)
while printf scan font geo main post align sim 3212: Pku3468 A Simple Problem with Integers 題目:傳送門 題解: 感謝Rose_max大佬的傾情相