A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8550    Accepted Submission(s): 3241

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.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.

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"?

 
Input
The 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.
 
Output
For 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
 
Sample Output
3
 
Recommend
8600
 
题意:n层楼。坐电梯。每层都有一个数字Ti,代表这层可以上或下Ti层。然后问能否从a层到b层。不能输出-1,能就输出最少需要几步。
感想:
1、我什么都不想说。。。诶。。。
     虽然这个题数据量是挺大的。。。用BFS无悬念。但是我觉得用DFS写怎么着也应该是超时啊,怎么会是WA。。。?
 
2、用DFS写。由于电梯上下是并列的选择,
      而dfs有搜索顺序的限制,不能找到就输出,所以不能保证第一次搜到的是最小的。只得用res1、res2记录下tot1、tot2的值。改变搜索顺序搜两次,
      然后返回主函数比较最小值输出。两次dfs()有标记变量,记录找到没。如果都没找到,两个标记变量flag1、flag2就应该都是0,此时输出-1。
 
     主函数中判断如果a==b就直接输出0,反之两次dfs()。
 
     我写了以后怎么想都只会超时,为什么会wa呢。。。。
 
3、其实用bfs()写省去很多判断。比如没有顺序的限制,因为是层次遍历,而且第一个找到的一定是最小步数。比如不需要判断a==b,判断也可以,算是优化剪枝吧。。
 
bfsAC代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std; int t[210],vis[210];
int n,a,b;
struct node
{
int cur;
int steps;
}start; int bfs()
{
queue<node> q;
while(!q.empty())
q.pop();
start.cur=a;
start.steps=0;
vis[a]=1;
q.push(start);
node m,temp;
while(!q.empty())
{
temp=q.front();
q.pop();
if(temp.cur==b)
return temp.steps;
int up=temp.cur+t[temp.cur];
if(!vis[up]&&up<=n)
{
vis[up]=1;
m.cur=up;
m.steps=temp.steps+1;
q.push(m);
}
int down=temp.cur-t[temp.cur];
if(!vis[down]&&down>0)
{
vis[down]=1;
m.cur=down;
m.steps=temp.steps+1;
q.push(m);
}
}
return -1;
} int main()
{
while(scanf("%d",&n)&&n)
{
memset(vis,0,sizeof(vis));
scanf("%d%d",&a,&b);
for(int i=1;i<=n;i++)
scanf("%d",&t[i]);
printf("%d\n",bfs());
}
return 0;
}

8799270

2013-08-02 09:08:53

Accepted

0MS

248K

1149 B

C++

 
dfs写的wa代码,搞不懂怎么会wa。。。。超时我倒是想过。。。。
 
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int t[210];
int tot1,tot2,n,a,b;
bool vis[210];
int flag1,flag2;
int res1,res2; void dfs1(int x)
{
//printf("now=%d\n",x);
//printf("tot=%d\n",tot);
if(flag1) return;
vis[x]=true;
if(x==b)
{
res1=tot1;//printf("%d\n",tot);
flag1=1;
return;
}
else if(x>n||x<1) return;
else
{
if(x+t[x]<=n&&vis[x+t[x]]==false)
{
tot1++;
dfs1(x+t[x]);
tot1--;
if(flag1) return;
}
if(x-t[x]>=1&&vis[x-t[x]]==false)
{
tot1++;
dfs1(x-t[x]);
tot1--;
}
}
} void dfs2(int x)
{
//printf("now=%d\n",x);
if(flag2) return;
vis[x]=true;
if(x==b)
{
res2=tot2;
flag2=1;
return;
}
else if(x>n||x<1) return;
else
{
if(x-t[x]>=1&&vis[x-t[x]]==false)
{
tot2++;
dfs2(x-t[x]);
tot2--;
if(flag2) return;
}
if(x+t[x]<=n&&vis[x+t[x]]==false)
{
tot2++;
dfs2(x+t[x]);
tot2--;
}
}
} int main()
{
int i;
while(scanf("%d",&n)&&n)
{
tot1=tot2=0,flag1=flag2=0;
res1=0,res2=0;
memset(vis,0,sizeof(vis));
scanf("%d%d",&a,&b);
for(i=1;i<=n;i++)
scanf("%d",&t[i]);
if(a==b) printf("0\n");
else if(a>n||b>n) printf("-1\n");
else
{
dfs1(a);
memset(vis,0,sizeof(vis));
dfs2(a);
if(flag1==0&&flag2==0) printf("-1\n");
else if(flag1==0&&flag2) printf("%d\n",res2);
else if(flag1&&flag2==0) printf("%d\n",res1);
else printf("%d\n",res2>=res1?res1:res2);
}
}
return 0;
} /* 5 1 4
2 3 1 2 1 ans=2 5 1 5
2 3 1 2 1 ans=3 5 1 5
3 1 1 2 1 ans=-1 3 2 2
1 1 1 ans=0 */

hdu 1548 A strange lift (bfs)的更多相关文章

  1. hdu 1548 A strange lift

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...

  2. hdu 1548 A strange lift 宽搜bfs+优先队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...

  3. HDU 1548 A strange lift (bfs / 最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...

  4. HDU 1548 A strange lift(BFS)

    Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...

  5. HDU 1548 A strange lift(最短路&&bfs)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  6. HDU 1548 A strange lift (Dijkstra)

    A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...

  7. HDU 1548 A strange lift 搜索

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  8. HDU 1548 A strange lift (广搜)

    题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...

  9. HDU 1548 A strange lift 题解

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

随机推荐

  1. Extjs4中tabPanel

    好文章引用:Extjs4 TabPanel例子 感谢原作者...

  2. HDOJ迷宫城堡(判断强连通 tarjan算法)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  3. [转]是String,StringBuffer还是StringBuilder?

    原文链接. 相信大家对 String 和 StringBuffer 的区别也已经很了解了,但是估计还是会有很多同志对这两个类的工作原理有些不清楚的地方,今天我在这里重新把这个概念给大家复习一下,顺便牵 ...

  4. Linux后台进程管理的一些命令小结

    Linux后台进程管理的一些命令:fg.bg.jobs.&.ctrl + z命令,供大家学习参考   一. &加在一个命令的最后,可以把这个命令放到后台执行 ,如gftp &, ...

  5. Nodejs in Visual Studio Code 11.前端工程优化

    1.开始 随着互联网技术的发展,企业应用里到处都是B/S设计,我有幸经历了很多项目有Asp.Net的,有Html/js的,有Silverlight的,有Flex的.很遗憾这些项目很少关注前端优化的问题 ...

  6. 网站搬家后,UC通信失败解决方法

    把应用里边的UC设置信息,类似如下的,复制覆盖config/config_ucenter.php里边的全部信息,多个的话,放在相应的位置就好了 define('UC_CONNECT', 'mysql' ...

  7. Servlet跳转

    方便自己查询,嫌低级的勿喷.... 在Servlet中跳转有两种: 1.客户端跳转 在Servlet中要进行客户端跳转(地址栏的地址信息将发生改变),直接使用HttpServletResponse接口 ...

  8. 七牛云- Java 端 使用

    项目 中需要把 图片放到 图片服务器上托管, 所以使用了七牛, 注册之后每个月 有免费100 万 次get请求,先说说怎么使用: 1 .注册, 获取自己的AK,SK

  9. PHP数据结构预热:PHP的迭代器(转)

    迭代器有时又称光标(cursor)是程式设计的软件设计模式,可在容器物件(container,例如list或vector)上遍访的接口,设计人员无需关心容器物件的内容. 各种语言实作Iterator的 ...

  10. innode 节点

    [root@localhost soft]# ls -i tt1 tt2 xx.c [root@localhost soft]# stat tt1 File: `tt1' Size: 4096 Blo ...