2018ICPC焦作 F. Honeycomb /// BFS
阿新 • • 發佈:2018-12-11
判斷 pre getchar scanf ica amp style else ret
題目大意:
給定n m表示一共n行每行m個蜂巢
求從S到T的最短路徑
input
1 3 4 +---+ +---+ / \ / + +---+ +---+ \ \ / + + S +---+ T + / \ / / + +---+ + + \ \ / +---+ +---+ + / / + +---+ + + \ / +---+ +---+ + \ / \ / +---+ +---+
output
7
如圖所示,其實只要按平常的走迷宮改變一下位移的格數就行了
改成一下的 上,下,左上,右上,左下,右下 的位移格數
如下位移格數,移動後為墻所在的位置,判斷有沒有墻即可判斷能不能通過
int mov[6][2]={ {-2,0},{2,0}, {-1,-3},{-1,3}, {1,-3},{1,3} };
然後將每個蜂巢的中心點當做固定點,即要到達這個蜂巢就將坐標定位在這個蜂巢的中心點
這樣就會發現,兩個中心點的距離其實就是兩倍位移格數
這個思路很好寫 場上想復雜了 直接帶偏隊友思路 引以為戒a...
#include <bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; const int N=1e4+5; char G[N][N]; int n,m,stx,sty; int mov[6][2]={ {-2,0},{2,0}, {-1,-3},{-1,3}, {1,-3},{1,3} }; struct NODE { int x,y,l; }; int bfs() { queue <NODE> q; q.push((NODE){stx,sty,View Code1}); while(!q.empty()) { NODE e=q.front(); q.pop(); for(int i=0;i<6;i++) { int x1=e.x+mov[i][0]; int y1=e.y+mov[i][1]; int x2=x1+mov[i][0]; int y2=y1+mov[i][1]; if(G[x1][y1]==‘ ‘ && G[x2][y2]==‘T‘) return e.l+1; if(G[x1][y1]!=‘ ‘ || G[x2][y2]!=‘ ‘) continue; G[x2][y2]=‘#‘; q.push((NODE){x2,y2,e.l+1}); } } return INF; } int main() { int t; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); getchar(); n=n*4+3, m=m*6+3; for(int i=1;i<=n;i++) { char ch; G[i][0]=‘ ‘; int j=1; while(~scanf("%c",&ch)&&ch!=‘\n‘) { G[i][j]=ch; if(ch==‘S‘) stx=i,sty=j; j++; } G[i][j]=‘\0‘; } int ans=bfs(); if(ans==INF) printf("-1\n"); else printf("%d\n",ans); } return 0; }
2018ICPC焦作 F. Honeycomb /// BFS