A strange lift (HDU1548)
来源:互联网 发布:win 10 网络初始化失败 编辑:程序博客网 时间:2024/06/05 19:09
HDU 1548 bfs(一维)
题意:电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,
但是不能低于一层或高于n层,给定起点与终点,要求出最少要按几次键。
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;#define N 210struct Node{ int time,flor; Node(){} Node(int _f,int _t){ flor = _f;time = _t; }};queue<Node> q;int k[N],vis[N];int solve(int start,int end,int n){ while(!q.empty()) q.pop(); q.push(Node(start,0)); while(!q.empty()){ Node cur = q.front(); q.pop(); if(cur.flor==end) return cur.time; int up = cur.flor + k[cur.flor]; int down = cur.flor - k[cur.flor]; int tim = cur.time + 1; if(up<=n&&up>=1&&vis[up]==0){ q.push(Node(up,tim)); vis[up] = 1; } if(down<=n&&down>=1&&vis[down]==0){ q.push(Node(down,tim)); vis[down] = 1; } } return -1;}void init(int n){ for(int i = 1;i<=n;i++) vis[i] = k[i] = 0;}int main(){ int n,a,b; while(~scanf("%d%d%d",&n,&a,&b)&&n){ init(n); for(int i = 1;i<=n;i++) scanf("%d",&k[i]); printf("%d\n",solve(a,b,n)); } return 0;}
0 0
- A strange lift (HDU1548)
- hdu1548 A strange lift
- HDU1548 A strange lift
- HDU1548 A strange lift
- hdu1548 A strange lift
- HDU1548---A strange lift
- hdu1548 A strange lift
- hdu1548 a strange lift
- HDU1548-A strange lift
- Hdu1548 A strange lift
- HDU1548 A strange lift
- hdu1548 A strange lift
- HDU1548 A strange lift
- HDU1548-A strange lift
- hdu1548 A strange lift (简单bfs)
- hdu1548 A strange lift(bfs水)
- HDU1548:A strange lift(BFS)
- HDU1548:A strange lift( BFS)
- SwipeRefreshLayout实现上拉下拉刷新
- 对象导论之继承
- 文章标题
- 一些jar的作用
- 第六章 Lighting Models
- A strange lift (HDU1548)
- Tensorflow: recurrent neural network (mnist basic)
- Leetcode 51. N-Queens
- c++ 字符串剪切
- Spring AOP 原理分析 part1
- VR时代的思考
- 异常:android.database.StaleDataException: Attempted to access a cursor after it has been closed
- 使用wireshark抓取bilibili直播的真实地址
- C++ hook