HDU 1548---A strange lift(搜索&&最短路)
来源:互联网 发布:ff14高地男捏脸数据 编辑:程序博客网 时间:2024/06/11 11:18
题意:电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层, 给定起点与终点,要求出最少要按几次键。思路:此题较为简单,只有两个方向,那么抽象为二叉树。又因为所找的目标节点已知,且要求最短路,因此将所有情况方法三:记录下来,满足宽度优先原则。代码实现:
方法二:
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- #include <queue>
- using namespace std;
- int n,s,e;
- int ss[205],vis[205];
- struct node
- {
- int x,step;
- };
- int check(int x)
- {
- if(x<=0 || x>n)
- return 1;
- return 0;
- }
- int BFS()
- {
- queue<node> Q;
- node a,next;
- int i;
- a.x = s;
- a.step = 0;
- vis[s] = 1;
- Q.push(a);
- while(!Q.empty())
- {
- a = Q.front();
- Q.pop();
- if(a.x == e)
- return a.step;
- for(i = -1;i<=1;i+=2)
- {
- next = a;
- next.x +=i*ss[next.x];
- if(check(next.x) || vis[next.x])
- continue;
- vis[next.x] = 1;
- next.step++;
- Q.push(next);
- }
- }
- return -1;
- }
- int main()
- {
- int i,j;
- while(~scanf("%d",&n),n)
- {
- scanf("%d%d",&s,&e);
- for(i = 1;i<=n;i++)
- scanf("%d",&ss[i]);
- memset(vis,0,sizeof(vis));
- printf("%d\n",BFS());
- }
- return 0;
- }
要注意的是,这道题是单向边,我们只要让map里面的值全部为1就可以统计次数了
另一种Dijkstra代码:
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- using namespace std;
- const int inf = 1<<30;
- int n;
- int map[205][205];
- int a[205],cnt;
- int vis[205],cast[205];
- void Dijkstra(int s,int e)
- {
- int i,j,min,pos;
- memset(vis,0,sizeof(vis));
- for(i = 0; i<n; i++)
- cast[i] = map[s][i];
- cast[s] = 0;
- vis[s] = 1;
- for(i = 1; i<n; i++)
- {
- min = inf;
- for(j = 0; j<n; j++)
- {
- if(cast[j]<min && !vis[j])
- {
- pos = j;
- min = cast[j];
- }
- }
- if(min == inf)
- break;
- vis[pos] = 1;
- for(j = 0; j<n; j++)
- {
- if(cast[pos]+map[pos][j]<cast[j] && !vis[j])
- cast[j] = cast[pos]+map[pos][j];
- }
- }
- }
- int main()
- {
- int i,j,s,e,x,y;
- while(~scanf("%d",&n),n)
- {
- scanf("%d%d",&s,&e);
- s--,e--;
- for(i = 0; i<n; i++)
- for(j = 0; j<n; j++)
- map[i][j] = inf;
- for(i = 0; i<n; i++)
- {
- scanf("%d",&a[i]);
- if(i+a[i]<n)
- map[i][i+a[i]] = 1;
- if(i-a[i]>=0)
- map[i][i-a[i]] = 1;
- }
- Dijkstra(s,e);
- printf("%d\n",cast[e]==inf?-1:cast[e]);
- }
- return 0;
- }
#include<stdio.h>#include<string.h>#define M 1000000int n,a,b,dis[201],cost[202][201];
void dijkstra(){
int i,j,d,min,mark[201]; for(i=1;i<=n;i++) dis[i]=cost[a][i]; //dis[i]代表源点到i点的距离 memset(mark,0,sizeof(mark)); //标记数组初始化 dis[a]=0;//到本身距离为0 for(i=1;i<n;i++){ min=M; for(j=1;j<=n;j++){//选最短路径 if(!mark[j]&&dis[j]<min){ min=dis[j]; d=j; } } f(min==M)//没有最短路径了,可以返回,当然不返回也行,效率低点 return; mark[d]=1;//把已选的标记 for(j=1;j<=n;j++) if(!mark[j]&&cost[d][j]+dis[d]<dis[j])//核心:有点像状态转移方程 dis[j]=dis[d]+cost[d][j]; }} int main(){
int i,j,k[201]; while(scanf("%d",&n),n){ scanf("%d%d",&a,&b); for(i=1;i<=n;i++){ for(j=1;j<=n;j++) cost[i][j]=M; //初始距离默认为无穷大 } for(i=1;i<=n;i++){ scanf("%d",&k[i]); if(i+k[i]<=n) //合法才处理 cost[i][i+k[i]]=1; //这就是对应能到的层数 if(i-k[i]>=1) cost[i][i-k[i]]=1; } dijkstra(); if(dis[b]<M) //如果没有到达 printf("%d\n",dis[b]); else puts("-1"); } return 0;}
- hdu 1548 A strange lift(搜索or最短路)
- HDU 1548---A strange lift(搜索&&最短路)
- hdu 1548 A strange lift(最短路)
- hdu 1548-A strange lift-最短路-dijkstra
- HDU 1548 A strange lift 最短路变形
- [HDU 1548]A Strange Lift[Dijkstra最短路]
- HDU--杭电--1548--A strange lift--最短路
- hdu 1548 A strange lift(最短路或DFS)
- HDU 1548 A strange lift (BFS或最短路)
- hdu 1548 A strange lift (bfs、最短路)
- HDU 1548 A strange lift(最短路或BFS)
- hdu 1548 A strange lift 最短路(spfa)
- HDU-1548 A strange lift(最短路[Spfa || BFS])
- HDU 1548 A strange lift (简单最短路或BFS)
- HDU 1548 A strange lift(最短路)
- hdu 1548 A strange lift 最短路 spfa模板
- hdu 1548 A strange lift(最短路)
- HDU 1548 A strange lift(构造+最短路Dijkstra)
- JAVA设计模式之单例模式
- C#之直接插入排序
- 最全的机器学习&深度学习入门视频课程集
- 菜鸟之路——Spring MVC(十)配置文件
- VMWare虚拟机提示:锁定文件失败,打不开磁盘的解决办法
- HDU 1548---A strange lift(搜索&&最短路)
- u-boot-2016.09移植(6)-dm9000
- c语言编写,2/1,3/2,5/3,8/5....这个数列的前20 项和
- filter过滤筛选
- 【封装】Fragment切换的方法
- mybatis报错:A query was run and no Result Maps were found for the Mapped Statement
- BOM(byte-order mark):字节顺序标记
- JDBC
- 第十周 小学生百以内算术考试系统