HDU 2102 A計劃 BFS求解

來源:互聯網
上載者:User

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=2102


題意簡單,救公主,但是許多細節需要處理,看著資料比較小,又不是求最小時間量,第一反應就是DFS,因為DFS的代碼量一般比較小,可惜TLE了,技術不夠啊。

1,當兩邊都是#時,不能過去,當對面是*時,不能過去

2,不能認為只有這邊是#,對面是 . 是才能過去,因為存在對面為P是的特殊情況,所以這告訴我們,不要任歪曲題意,還是跟著題意走,WA了好多次

附上一個有測試資料的連結:http://blog.csdn.net/liuqiyao_01/article/details/8858182

代碼:

#include<iostream>#include<sstream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<set>#include<map>#include<vector>#include<queue>#include<string>#define LL __int64#define INF 0x7fffffffusing namespace std;struct node{    int z,x,y,step;};char Map[2][12][12];int vis[2][12][12],N,M,T,P[3];int d[][2]={1,0,-1,0,0,1,0,-1};void bfs(){    memset(vis,0,sizeof(vis));    queue<node> Q;    Q.push((node){0,0,0,0});vis[0][0][0]=1;    while(!Q.empty()){        node tp=Q.front();        if(tp.z==P[0] && tp.x==P[1] && tp.y==P[2]){cout<<"YES\n";return;}        Q.pop();        if(tp.step>=T) continue;        for(int p=0;p<4;p++){            int tx=tp.x+d[p][0],ty=tp.y+d[p][1];            if(tx<0 || ty<0 || tx>=N || ty>=M) continue;            if(vis[tp.z][tx][ty] || Map[tp.z][tx][ty]=='*') continue;            if(Map[tp.z][tx][ty]=='#'){                if(Map[1-tp.z][tx][ty]!='#' && Map[1-tp.z][tx][ty]!='*' && !vis[1-tp.z][tx][ty])                    vis[1-tp.z][tx][ty]=1,Q.push((node){1-tp.z,tx,ty,tp.step+1});                vis[tp.z][tx][ty]=1;                continue;            }            Q.push((node){tp.z,tx,ty,tp.step+1});vis[tp.z][tx][ty]=1;        }    }    cout<<"NO\n";}int main(){    //freopen("D:\\in.txt","r",stdin);    int C;cin>>C;    while(C--){        cin>>N>>M>>T;        for(int i=0;i<2;i++) for(int j=0;j<N;j++) for(int k=0;k<M;k++){            cin>>Map[i][j][k];            if(Map[i][j][k]=='P') P[0]=i,P[1]=j,P[2]=k;        }        bfs();    }    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.