nyist 58 最少步數

來源:互聯網
上載者:User

標籤:os   io   資料   for   ar   div   時間   amp   

最少步數時間限制:3000 ms  |  記憶體限制:65535 KB難度:4 
描述

這有一個迷宮,有0~8行和0~8列:

 1,1,1,1,1,1,1,1,1  1,0,0,1,0,0,1,0,1  1,0,0,1,1,0,0,0,1  1,0,1,0,1,1,0,1,1  1,0,0,0,0,1,0,0,1  1,1,0,1,0,1,0,0,1  1,1,0,1,0,1,0,0,1  1,1,0,1,0,0,0,0,1  1,1,1,1,1,1,1,1,1

0表示道路,1表示牆。

現在輸入一個道路的座標作為起點,再如輸入一個道路的座標作為終點,問最少走幾步才能從起點到達終點?

(註:一步是指從一座標點走到其上下左右相鄰座標點,如:從(3,1)到(4,1)。)

 
輸入
第一行輸入一個整數n(0<n<=100),表示有n組測試資料; 隨後n行,每行有四個整數a,b,c,d(0<=a,b,c,d<=8)分別表示起點的行、列,終點的行、列。
輸出
輸出最少走幾步。
範例輸入
23 1  5 73 1  6 7
範例輸出
1211






///Memory Limit Exceeded

#include <iostream>

#include <queue>

using namespace std;

struct node {   int x, y,step;  };

struct node s,e;

int dd[4][2]={-1,0,1,0,0,-1,0,1},n,ans;

int g[9][9]={  

1,1,1,1,1,1,1,1,1, 

1,0,0,1,0,0,1,0,1,

1,0,0,1,1,0,0,0,1,

1,0,1,0,1,1,0,1,1,

1,0,0,0,0,1,0,0,1, 

1,1,0,1,0,1,0,0,1,

1,1,0,1,0,1,0,0,1, 

1,1,0,1,0,0,0,0,1, 

1,1,1,1,1,1,1,1,1

}; queue <node> q;

 

  void bfs()

{   node t,t2;

    while (! q.empty() )  q.pop();

   q.push(s);

    while ( !q.empty())

    {   t=q.front();

        q.pop();

        if (t.x==e.x && t.y==e.y) {  ans=t.step; return ;   }

         for (int i=0; i<4; i++)         {   t2.x=t.x+dd[i][0];   t2.y=t.y+dd[i][1];   t2.step=t.step+1;

            if ( g[t2.x][t2.y]==0 ) q.push(t2);

         }

    }

}  

 

  int main(int argc, char *argv[])

{

    cin>>n; 

    while (n--)

     {             cin>>s.x>>s.y>>e.x>>e.y;

           s.step=0; 

        ans=0;

          bfs();

        cout<<ans<<endl; 

    }  

  return 0;

}

*******************************************************************************************************

//ACCept

#include <iostream>

#include <queue>

#define N 9

#define M 9

using namespace std;

int map[N][M]= {

    1,1,1,1,1,1,1,1,1,

    1,0,0,1,0,0,1,0,1,

    1,0,0,1,1,0,0,0,1,

    1,0,1,0,1,1,0,1,1,

    1,0,0,0,0,1,0,0,1,

    1,1,0,1,0,1,0,0,1,

    1,1,0,1,0,1,0,0,1,

    1,1,0,1,0,0,0,0,1,

    1,1,1,1,1,1,1,1,1, };

int d[4][2]={-1,0,0,1,1,0,0,-1 };

struct point {

    int x,y,step;

}s,e;

queue <point> my;

int BFS(point s) {

    int i,x,y;

    point t,temp;

    while (!my.empty())    my.pop();

    my.push(s);

    while (!my.empty())

    {

        t=my.front();

        my.pop();

        for (i=0; i<4; i++)

        {             x=t.x+d[i][0];

                       y=t.y+d[i][1];

            if (x==e.x&& y==e.y)              return     t.step+1;

            if (x>=0 && x<N && y>=0 && y<M && map[x][y]==0)

           

         {          temp.x=x;

                               temp.y=y;

                               temp.step=t.step+1;

                                 my.push(temp);

             }

        }

    }

}

int main()

{

    int n,x0,y0,ans;

    cin>>n;

    while (n--)

    {         cin>>s.x>>s.y>>e.x>>e.y;

        s.step=0;

        if (s.x==e.x && s.y==e.y)         ans=0;

        else                                 ans=BFS(s);

        cout<<ans<<endl;

    }

    return 0;

}

 

 

 

 

*************************************************************

Accept

 

 

#include <cstring>

#include <iostream>

#include <queue>

using namespace std;

int ans; int g[9][9]= {

    1,1,1,1,1,1,1,1,1,

    1,0,0,1,0,0,1,0,1,

    1,0,0,1,1,0,0,0,1,

    1,0,1,0,1,1,0,1,1,

    1,0,0,0,0,1,0,0,1,

    1,1,0,1,0,1,0,0,1,

    1,1,0,1,0,1,0,0,1,

    1,1,0,1,0,0,0,0,1,

    1,1,1,1,1,1,1,1,1,

};

int dd[4][2]={-1,0,0,1,1,0,0,-1 };

int bz[10][10];

 

struct node{

    int x,y,step;

};

struct node s,e;

queue <node> q;

int bfs( )

{

    int i,x,y;

    node t,t2;

    q.push(s);

bz[s.x][s.y]=1;

 

    while (!q.empty())

    {         t=q.front();

        q.pop();

        if (t.x==e.x && t.y==e.y)  return t.step;

        for (i=0; i<4; i++)  

       {             x=t.x+dd[i][0];

                      y=t.y+dd[i][1];

                        if (g[x][y]==0&&!bz[x][y])

               {              t2.x=x;    t2.y=y;     t2.step=t.step+1;

                        bz[x][y]=1;

                                q.push(t2);  

           }

        }

    }

}

int main()

{  

   int n;

    cin>>n;

    while (n--)

    {  

while (!q.empty())            q.pop();

     memset(bz,0,sizeof(bz));

        cin>>s.x>>s.y>>e.x>>e.y;

  s.step=0;

        cout<<bfs( )<<endl;

    }  

   return 0;

}

 

 

















聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.