1013
来源:互联网 发布:598营销软件 编辑:程序博客网 时间:2024/05/16 06:02
简单题意:电梯有两个方向,向上和向下,求最短的路径
解题思路:这是一个广度搜索的题
感想:电梯问题做了很多,没想到还可以和搜索联系在一起
AC代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <climits>
using namespace std;
const int MAX = 202;
int flr[MAX],ans;
int dist[MAX];
int n,b;
void dfs(int s,int cnt){
if(s>n || s<1)return;
if(s==b){
if(cnt<ans){
ans = cnt;
return;
}
}
if(cnt>=dist[s])return;
dist[s] = cnt;
dfs(s+flr[s],cnt+1);
dfs(s-flr[s],cnt+1);
}
int main(){
//freopen("in.txt","r",stdin);
int a,i,cnt;
while(scanf("%d",&n)!=EOF && n){
scanf("%d %d",&a,&b);
for(i=1;i<=n;++i){
scanf("%d",&flr[i]);
dist[i] = INT_MAX - 10;
}
cnt = 0;
ans = INT_MAX;
dfs(a,cnt);
if(ans==INT_MAX){
printf("-1\n");
}else{
printf("%d\n",ans);
}
}
return 0;
}
0 0
- 1013
- 1013
- 1013
- 1013
- 1013
- 1013
- 1013
- 1013
- 1013
- 1013
- 1013
- 1013
- 1013
- 1013
- 1013
- 1013
- 1013
- 1013
- linux驱动基础系列--Linux 串口、usb转串口驱动分析
- 你不知道的Bundle
- [汇编语言学习笔记][第四章第一个程序的编写]
- SolrCloud原理介绍 [
- wex简介
- 1013
- uva10905
- 怎样让浏览器变身代码编辑器?
- 深搜算法-1011
- 安卓、第三方分享功能代码;
- 关于扩展Josephus问题
- 基于stm32CubeMX和keil5的stm32f103学习编程
- JSON转javabean(pojo)利器
- leetcode258-Add Digits(非负整数各位相加)