2018 ICPC 焦作現場 F. Honeycomb(BFS求最短路,卡memset)

題意:

給一個蜂巢圖,問從s到t最短路徑長是多少。

思路:

BFS。

1. 不用對地圖進行抽象轉換,直接在原圖上走迷宮:每個格子相鄰的有六個格子,所以每步能走六個方向,把六個方向的座標看好。 

2. 不要用vis[][]記錄走過與否,memset會超時,直接在原圖上標記!

代碼:

#include<bits/stdc++.h>
using namespace std;
//Life is Short!
const int N=1005;
char mp[N*5][N*7];
int len[N*5];
struct node{
    int x,y,step;
};
int dir[6][2]={
    -2,-6,
    -4,0,
    -2,6,
    4,0,
    2,-6,
    2,6
};
int main(){
    int T;
    cin>>T;
    while(T--){
        int r,c,sx,sy;
        cin>>r>>c;
        getchar();//
        for(int i=0;i<4*r+3;++i){
            gets(mp[i]);//
            len[i]=strlen(mp[i]);
            for(int j=0;j<len[i];++j){
                if(mp[i][j]=='S'){
                    sx=i,sy=j;
                    break;
                }
            }
        }
        queue<node>q;
        int ans=-1;
        q.push({sx,sy,1});
        while(!q.empty()&&ans==-1){
            auto e=q.front();
            if(mp[e.x][e.y]=='T'){
                ans=e.step;
                break;
            }
            q.pop();
            for(int i=0;i<6;++i){
                int nx=e.x+dir[i][0],ny=e.y+dir[i][1];
                if(nx>=0&&ny>=0&&nx<4*r+3&&ny<len[nx]&&mp[nx][ny]!='*'&&mp[(e.x+nx)/2][(e.y+ny)/2]!='-'&&mp[(e.x+nx)/2][(e.y+ny)/2]!='/'&&mp[(e.x+nx)/2][(e.y+ny)/2]!='\\'){
                    if(mp[nx][ny]=='T') {ans=e.step+1;break;}
                    mp[nx][ny]='*';
                    q.push({nx,ny,e.step+1});
                }
            }
        }

        printf("%d\n",ans);
    }
    return 0;
}

PS:

1. 看到現場賽的人賽後補這道題,說自己“沒有拿牌子的命”云云,我覺得,還是要多練習吧,經驗都是平時一點一滴積累的,誰都不可能第一次做就很完美地避開所有坑。希望自己即便很難過,也要堅強起來專注於問題本身,反思自己有哪些不足,然後繼續切下一題,不要說自己“沒xx的命”之類的自暴自棄的話。加油(ง •_•)ง呀諸位。

2. 我發現我第一次寫的代碼沒有對邊界進行判斷也A了,數據不強......

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章