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 ...
随机推荐
- 常用的用户状态命令包括:whoami、id、groups、newgrp 等
用户状态命令 常用的用户状态命令包括:whoami.id.groups.newgrp 等.
- 导出C++ dll文件
方法1. 直接新建 Qt dll library, 使用工程自动创建的宏定义 方法2. (1)新建一个Empty的Win32项目(如ExampleDLL),选择Application type 为DL ...
- kafka log4j配置
kafka日志文件分为5种类型,依次为:controller,kafka-request,server,state-change,log-cleaner,不同类型log数据,写到不同文件中: 区别于c ...
- FTP软件Filezilla出现“读取目录列表失败”的解决办法
FTP软件Filezilla出现“读取目录列表失败”情况一般出现在vista/win7系统上,之前在xp上没发现这种情况. 总的来说,不论是打开FTP出现乱码或者显示“读取目录列表失败”均是由字符集引 ...
- Jack Straws(判断线段是否相交 + 并查集)
/** http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1840 题意: 判断线段 ...
- 关于URL重写
1. 为什么要使用url重写? 先从session的原理讲起,为什么我们能够获得session中的东西呢?如果没有禁用cookie的话,那么session创建之后,会将session id的值通过r ...
- Struts2自己定义拦截器实例—登陆权限验证
版本号:struts2.1.6 此实例实现功能:用户须要指定username登陆,登陆成功进入对应页面运行操作,否则返回到登陆页面进行登陆,当直接訪问操作页面(登陆后才干訪问的页面)时则不同意,须返回 ...
- CodeForces 55D Beautiful numbers(数位dp)
数位dp,三个状态,dp[i][j][k],i状态表示位数,j状态表示各个位上数的最小公倍数,k状态表示余数 其中j共有48种状态,最大的是2520,所以状态k最多有2520个状态. #include ...
- Android中的双向链表
1.看源代码必须搞懂Android的数据结构.在init源代码中双向链表listnode使用非常多,它仅仅有prev和next两个指针,没有不论什么数据成员.这个和linux内核的list_head如 ...
- [转] Maven镜像配置
参考:许晓斌的<Maven实战> 镜像是为了提供更快的服务 如图:X就认为是Y的一个镜像. 编辑settings.xml配置中央仓库镜像: <settings> ... < ...