奇怪的电梯解题报告(广度优先搜索)
2014-08-14 20:27
176 查看
奇怪的电梯Time Limit:10000MS Memory Limit:65536KTotal Submit:56 Accepted:33 Case Time Limit:1000MSDescription 呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第i层楼(1<=i<=N)上有一个数字Ki(0<=Ki<=N)。电梯只有四个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如:3 3 1 2 5代表了Ki(K1=3,K2=3,……),从一楼开始。在一楼,按“上”可以到4楼,按“下”是不起作用的,因为没有-2楼。那么,从A楼到B楼至少要按几次按钮呢?Input输入文件共有二行,第一行为三个用空格隔开的正整数,表示N,A,B(1≤N≤200, 1≤A,B≤N),第二行为N个用空格隔开的正整数,表示Ki。Output输出文件仅一行,即最少按键次数,若无法到达,则输出-1。Sample Input
5 1 5 3 3 1 2 5Sample Output
3
程序:
const maxn=1000;var father,state,a,f:array[1..maxn] of longint; n,a1,b,ans:longint;function check(x:longint):boolean;//判断是否合格(含有判重) begin if (x>0) and (x<=n) and (f[x]=0) then exit(true); check:=false;end;procedure dg(x:longint);//计数 begin if x=0 then exit; dg(father[x]); inc(ans);end;procedure init;//读入 var i:longint; begin readln(n,a1,b); for i:=1 to n do read(a[i]); f[a1]:=1; ans:=0;end;procedure bfs;//广搜 var h,t:longint; begin h:=0; t:=1; state[1]:=a1; father[1]:=0; repeat inc(h); if check(state[h]+a[state[h]]) then begin inc(t); state[t]:=state[h]+a[state[h]]; father[t]:=h; f[state[t]]:=1; if state[t]=b then begin dg(t); break; end; end; if check(state[h]-a[state[h]]) then begin inc(t); state[t]:=state[h]-a[state[h]]; father[t]:=h; f[state[t]]:=1; if state[t]=b then begin dg(t); break; end; end; until h>=t; if h>=t then writeln(-1)//输出 else writeln(ans-1);end;begin init; if a1=b then writeln(0) else bfs;end.
心得:
这道题我已开始做了很久,虽有一些小错误,后来再叫还有问题。原来是忘记判重,以后要记得判重。
相关文章推荐
- 奇怪的电梯解题报告
- [Ahoi2014]奇怪的计算器 解题报告
- ACM-奇怪的电梯(广度优先搜索、AC)
- pku 1111 解题报告 简单的广度优先搜索
- 洛谷Oj-奇怪的电梯-广度优先搜索
- 2833 奇怪的梦境 【解题报告】
- CODEVS P2833 奇怪的梦境 解题报告
- hdu 4505 小Q系列故事——电梯里的爱情 解题报告
- 【网易2017春招】奇怪的表达式求值 解题报告
- 解题报告-HDOJ-1242(广度优先搜索)
- 奇怪的电梯(广度优先搜索)
- 【未出现的字串】解题报告
- A strange lift(解题报告)
- POJ 1012 Joseph 解题报告
- Convert Sorted List to Binary Search Tree解题报告
- 中中纳新 解题报告
- 算法第九周解题报告
- 【集合分组】解题报告
- HDU1010(解题报告)
- NOIP2011 铺地毯 解题报告(水题)