HDU 2579 BFS

來源:互聯網
上載者:User

一天一個錯誤,找死人了;

有些東西,看似沒錯;可是online juge 就是給你報錯,那99% 你就是錯了

考慮的東西不全面,錯了也活該;

剛開始visit 弄了二維的,覺得沒錯,因為自己拿著k=2舉例子,其實你換個數,比如k=5 你就會發現,我們這個visit還需要加一維

visit[x][y][z]  表示的是點 (x,y) 在z狀態下是否已經經曆過;這個z為 時間對k的餘數;  比如對於5來說,在同一個點,t=10 和t=5時應該視為一樣的;

對接下去他可能經過#的地方造成的影響是一樣的;

以下是AC代碼;

如果你將//××× 的注釋掉,再將下面注釋的取消注釋,你覺得對了嗎;

我開始也以為是對的;

後來發現,我忽略了一個點,起點。所以如果要那樣做,必須:if(maze[b.x][b.y]=='.'||maze[b.x][b.y]=='Y')

或者你輸入了資料記錄了開始點之後,把Y改為.也行的;

#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;#define maxn 100+3struct point{    int x;    int y;    int t;} st;int n,m,k;char maze[maxn][maxn];bool visit[maxn][maxn][15];int xx[] = {1,-1,0,0};int yy[] = {0,0,1,-1};queue <point> q;inline bool check(int x, int y,int z){    if(x<1||y<1||x>n||y>m||visit[x][y][z%k])        return false;    if(maze[x][y]=='#' && z%k!=0)//×××        return false;//×××    return true;}void BFS(){    while(!q.empty())        q.pop();    q.push(st);    visit[st.x][st.y][0] = true;    point a,b;    while(!q.empty())    {        a = q.front();        q.pop();        for(int i = 0; i < 4; i++)        {            b.x = a.x + xx[i];            b.y = a.y + yy[i];            b.t = a.t + 1;            if(check(b.x,b.y,b.t))            {                if(maze[b.x][b.y]=='G')                {                    printf("%d\n",b.t);                    return ;                }//                if(maze[b.x][b.y]=='.')//                {                    q.push(b);                    visit[b.x][b.y][b.t%k] = true;//                }//                if(maze[b.x][b.y]=='#')//                {//                    if(b.t%k==0)//                    {//                        q.push(b);//                        visit[b.x][b.y][b.t%k] = true;//                    }//                }            }        }    }    puts("Please give me another chance!");    return ;}int main(){    int T;    cin>>T;    while(T--)    {        cin>>n>>m>>k;        for(int i = 1; i <= n; i++)        {            for(int j = 1; j <= m; j++)            {                cin>>maze[i][j];                if(maze[i][j]=='Y')                {                    st.x = i;                    st.y = j;                    st.t = 0;                }            }        }        memset(visit,false,sizeof(visit));        BFS();    }}

聯繫我們

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