hdu 1548 A strange lift (bfs)
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
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"?
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.
3 3 1 2 5
0
#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 |
C++ |
#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)的更多相关文章
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- 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 ...
- 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 ...
- 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 ...
- HDU 1548 A strange lift(最短路&&bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1548 A strange lift (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- HDU 1548 A strange lift 搜索
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1548 A strange lift (广搜)
题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...
- HDU 1548 A strange lift 题解
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
随机推荐
- [置顶] 推荐12款很棒的HTML5开发框架和开发工具
HTML5 在不同的领域让网页设计更强大的.快速,安全,响应式,互动和美丽,这些优点吸引更多的 Web 开发人员使用 HTML5.HTML5 有许多新的特性功能,允许开发人员和设计师创建应用程序和网站 ...
- phpMyAdmin 完整路径泄露漏洞
漏洞名称: phpMyAdmin 完整路径泄露漏洞 CNNVD编号: CNNVD-201307-650 发布时间: 2013-08-09 更新时间: 2013-08-09 危害等级: 中危 漏洞类 ...
- 【video】m3u8
Safari浏览器识别不了.UC浏览器可以识别. 优酷的Safari浏览器和UC浏览器都可以识别.
- 实战weblogic集群之创建domain,AdminServer
在weblogic安装完后,接下来就可以创建domain,AdminSever了. 第1步: $ cd /app/sinova/Oracle/wlserver_10./common/bin $ ./c ...
- 字符串(后缀自动机):NOI 2016 优秀的拆分
[问题描述] 如果一个字符串可以被拆分为 AABB 的形式,其中 A 和 B 是任意非空字符串, 则我们称该字符串的这种拆分是优秀的. 例如,对于字符串 aabaabaa,如果令 A = aab, B ...
- 数据结构:(平衡树,链表)BZOJ 1588[HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12173 Solved: 4354[Submit][Sta ...
- 【动态规划】【缩点】NCPC 2014 G Outing
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1793 题目大意: 一辆公交车,上面M个座位,N个人(M<=N<=1000) ...
- floyd详解
就不在来回搬了 请看 http://note.youdao.com/groupshare/?token=6422E952998F4381A1534B71359EFA57&gid=1579165 ...
- Solaris下怎样改动文件创建时间及查询
Solaris下怎样改动文件创建时间及查询 实验演示: 1.核对时间 [root@S1011:/]# date Tue Jul 15 21:37:01 CDT 2014 --若时间不对请先按例如以下格 ...
- [AWS S3] Hosting a Static Website on Amazon S3
In this lesson, you will learn how to host a static website on Amazon S3. I will show you how to cre ...