练习二 1013 升降机问题
2016-04-25 08:37
288 查看
题目大意:
给出楼层总数,给出起始位置和终点位置。 然后现在 你在一个升降机里面,只能按 上升或者下降,给出 处在每层的时候 升降机运动的层数。也就是说,看样例:
现在处在 1 楼 ,要去 5楼。在 1 楼不可以按 下降,因为不可以降落到 -2 层。如果上升的话,会上升到 4 。也就是说上升或者下降越界的话,就不可以执行。
问 从起点到终点需要的最短次数。如果怎样都到不了没输出 -1
思路:
此题为搜索典型题,从起点进行bfs即可,然后分为上下两种状态。
感想:
这个题都是套路,哈哈
代码如下:
#include<iostream》
#include<queue>
#include<iomanip>
#include<cstdio>
using namespace std;
struct node
{
int x;
int t;
};
int main()
{
int ww[2000],c[2000];
node first,next,m;
queue<node> q;
int n,a,b,i,e;
while(cin>>n&&n!=0)
{
memset(c,0,sizeof(c));
cin>>a>>b;
for(i=1;i<=n;i++)
{
cin>>ww[i];c[i]=0;
}
e=0;
first.x=a;
first.t=0;
q.push(first);
c[first.x]=1;
while(!q.empty())
{
m=q.front();
q.pop();
if(m.x==b)
{
e=1;break;
}
first.x=m.x-ww[m.x];
next.x=m.x+ww[m.x];
if(first.x>0&&first.x<=b&&!c[first.x])
{
first.t=m.t+1;
c[first.x]=1;
q.push(first);
}
if(next.x>0&&next.x<=b&&!c[next.x])
{
next.t=m.t+1;
c[next.t]=1;
q.push(next);
}
}
while(!q.empty())
{
q.pop();
}
if(e)
cout<<m.t<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}
<
4000
p>ACID:00757911
题目大意:
给出楼层总数,给出起始位置和终点位置。 然后现在 你在一个升降机里面,只能按 上升或者下降,给出 处在每层的时候 升降机运动的层数。也就是说,看样例:
现在处在 1 楼 ,要去 5楼。在 1 楼不可以按 下降,因为不可以降落到 -2 层。如果上升的话,会上升到 4 。也就是说上升或者下降越界的话,就不可以执行。
问 从起点到终点需要的最短次数。如果怎样都到不了没输出 -1
思路:
此题为搜索典型题,从起点进行bfs即可,然后分为上下两种状态。
感想:
这个题都是套路,哈哈
代码如下:
#include<iostream》
#include<queue>
#include<iomanip>
#include<cstdio>
using namespace std;
struct node
{
int x;
int t;
};
int main()
{
int ww[2000],c[2000];
node first,next,m;
queue<node> q;
int n,a,b,i,e;
while(cin>>n&&n!=0)
{
memset(c,0,sizeof(c));
cin>>a>>b;
for(i=1;i<=n;i++)
{
cin>>ww[i];c[i]=0;
}
e=0;
first.x=a;
first.t=0;
q.push(first);
c[first.x]=1;
while(!q.empty())
{
m=q.front();
q.pop();
if(m.x==b)
{
e=1;break;
}
first.x=m.x-ww[m.x];
next.x=m.x+ww[m.x];
if(first.x>0&&first.x<=b&&!c[first.x])
{
first.t=m.t+1;
c[first.x]=1;
q.push(first);
}
if(next.x>0&&next.x<=b&&!c[next.x])
{
next.t=m.t+1;
c[next.t]=1;
q.push(next);
}
}
while(!q.empty())
{
q.pop();
}
if(e)
cout<<m.t<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}
<
4000
p>ACID:00757911
相关文章推荐
- stringbuffer和stringbuilder的区别
- datanode数据存放位置研究
- [剑指offer]链表中倒数第k个结点
- String、StringBuffer和StringBulder
- [BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理(状压dp)
- python错误:ImportError: cannot import name Thing2Literal
- 时间戳10位转13位的四种方法
- 慕课网HTML+CSS课程笔记
- GitHubwindows客户端使用
- 日活1亿+:看新浪微博混合云DCP的架构实战-搜狐
- c# foreach 和for循环比较
- Web应用的组件化开发(二)
- 内存错误:Address .... is on thread 1's stack 与创建变量时出现段错误
- 深入理解java异常处理机制
- hbase异常:ClockOutOfSyncException
- linux命令总结
- 进程死锁的危害、导致原因、解决方法
- 这样读源码,不牛X也难
- jquery 语法
- Web应用的组件化开发(一)