怪异的楼梯
来源:互联网 发布:js时间戳转换 编辑:程序博客网 时间:2024/05/19 01:06
有一个怪异的楼梯,每一层只能选择上或者下两种按键,求从起点到终点最少的按键次数!简单的bfs! http://acm.hdu.edu.cn/showproblem.php?pid=1548
#include <iostream>#include <cstdio>#include <queue>#include <string.h>#define N 205using namespace std;int a[N],s,e,n,f[N],flag;int jump[2]={-1,1};struct node{ int x,step;}q,p,r;void init(){ flag=0; cin>>s>>e; for(int i=1;i<=n;i++) cin>>a[i]; memset(f,0,sizeof(f));}int is_b(struct node p){ if(p.x<1 || p.x>n || f[p.x]) return 1; return 0;}void bfs(){ queue<node> que; q.x=s;q.step=0; f[q.x]=1; que.push(q); while(!que.empty()) { q=que.front();que.pop(); for(int k=0;k<2;k++) { p.x=q.x+a[q.x]*jump[k]; if(is_b(p)) continue; f[p.x]=1; p.step=q.step+1; if(p.x==e) { flag=1; cout<<p.step<<endl; return; } que.push(p); } }}int main (){ //freopen("s.txt","r",stdin); while(cin>>n) { if(!n) break; init(); if(s==e) { cout<<0<<endl; continue; } bfs(); if(!flag) cout<<-1<<endl; } return 0;}
- 怪异的楼梯
- 怪异的SQL注入
- 怪异的SQL注入
- 怪异的SQL注入
- 怪异的SQL注入
- 怪异的感觉
- 怪异的问题
- gavinnn,怪异的名字
- 怪异的dhcp
- 怪异的图像转换
- 删除怪异的表
- 怪异的系统自动关机
- 怪异的switch
- 怪异的insight
- 怪异的洗牌
- 怪异的IE - 2
- 怪异的面试题
- 怪异的JavaScript
- WEB-Android两种方式实现文件上传
- Android下载文件显示到ImageView并保存在手机SD卡
- 广播的类型
- service介绍
- 面向对象之封装,继承,多态(上)
- 怪异的楼梯
- 2012中国软件业务收入百强企业发布 (zz.IS2120)
- Android消息机制解析
- VRay
- struts2标签和OGNL表达式
- android 单击弹出Context menu
- Java IO 基本知识
- struts2 中Struts.xml -- constant配置参数的含义
- 面向对象之封装,继承,多态(下)