hdu 1548A strange lift1(简单的BFS)

来源:互联网 发布:vb最新版 编辑:程序博客网 时间:2024/06/06 07:21

题目分析:很水的BFS,开一个标记数组把到过的电梯,标记下来,防止重复走


#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;  }  


原创粉丝点击