读书人

hdu 1548 A strange lift(容易的bfs)

发布时间: 2012-10-28 09:54:44 作者: rapoo

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): 3839????Accepted Submission(s): 1406

Here comes the problem: when you is on floor A,and you want to go to floor B,how many times at least he havt to press the button "UP" or "DOWN"?

?

#include <iostream>#include <stdio.h>#include <memory.h>#include <queue>using namespace std;int N, A, B;int a[205];bool map[205], flag;struct node{ int x, step;}n1, n2, m;int main(){ int i; while(scanf("%d", &N), N) { if(N == 0) break; scanf("%d %d", &A, &B); for(i = 1; i <= N; i++) { scanf("%d", &a[i]); map[i] = false; } flag = false; n1.x = A; n1.step = 0; queue<node> Q; Q.push(n1); map[n1.x] = true; while(!Q.empty()) { m = Q.front(); Q.pop(); if(m.x == B) //到达目标 { flag = true; break; } n1.x = m.x - a[m.x]; n2.x = m.x + a[m.x]; if(n1.x>0 && n1.x<=B && !map[n1.x]) //下去的 { n1.step = m.step + 1; map[n1.x] = true; //标记 Q.push(n1); } if(n2.x>0 && n2.x<=B && !map[n2.x]) //上去的 { n2.step = m.step + 1; map[n2.x] = true; //标记 Q.push(n2); } } if(flag) printf("%d\n", m.step); else printf("-1\n"); } return 0;}?

读书人网 >编程

热点推荐