A strange lift hdu 1548

來源:互聯網
上載者:User

/*
   終於AC了,很簡單的bfs,我居然卡了那麼久,結果居然是我題目的意思看錯了,
   there is a number Ki(0 <= Ki <= N) on every floor 這句話沒有認真讀,
   我以為是up down 是按題目給我們的順序按的,結果第一次超記憶體,後來用hash標記,wrong。。。
   後來終於看見那句要命的話了。讀懂題目方能開始做題目,切記!!
 */

#include<iostream>//2393959 2010-04-28 14:19:46 Accepted 1548 15MS 296K 1032 B C++ 悔惜晟
#include<queue>
#include<cstring>
using namespace std;

int a, b, n;
bool flag;
int num[205];
bool hash[205];
int dir[2] = {1, -1};
struct node
{
 int i;
 int t;
};

int bfs( int si, int t )
{
 queue<node>q;
 node N, P;
 N.i = si; N.t = 0;
 q.push(N);
 hash[N.i] = true;
 int j;
 
 while(!q.empty())
 {
  N = q.front();
  if(N.i == b)
  {
   flag = true;
   cout<<N.t<<endl;
   break;
  }
  if(N.t == n)
  {
   break;
  }
  q.pop();
  for(j = 0; j < 2; j++)
  {
   
   P.i = N.i + num[N.i] * dir[j];
   if(P.i == b)
   {
    flag = true;
    cout<<N.t + 1<<endl;
    return 1;
   }
   if(hash[P.i])
    continue;
   if(P.i >= 1 && P.i <= n)
   {
    hash[P.i] = true;
    P.t = N.t + 1;
    q.push(P);
   }
  
  }

 }
 return 1;

}
int main()
{
 int  i;
 while(cin>>n && n)
 {
  cin>>a>>b;
  for(i = 1; i <= n; i++)
   cin>>num[i];
  flag = false;
  memset(hash, false, sizeof(hash));
  bfs(a, 0);
  if(!flag)
   cout<<"-1"<<endl;
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.