1. 程式人生 > >POJ - 3026 Borg Maze BFS加最小生成樹

POJ - 3026 Borg Maze BFS加最小生成樹

sig gre define typedef 處理 fir sin ide get

Borg Maze

題意:

  題目我一開始一直讀不懂。有一個會分身的人,要在一個地圖中踩到所有的A,這個人可以在出發地或者A點任意分身,問最少要走幾步,這個人可以踩遍地圖中所有的A點。

思路:

  感覺就算讀懂了題目,也比較難想到這用到了最小生成樹的知識,因為可以分身,所以每個點可以向其他點都連上邊。可以用bfs預處理出所有的S點,A點的連線,再跑一遍最小生成樹,即可得出答案。這裏有幾點註意,一開始我bfs沒有記錄step,而是直接找到一點後算曼哈頓距離,這是不對的,因為可能是繞了一個圈到了這個點。還有讀完數字再讀字符串的話,以後盡量用getline(cin,str),防止題目數據後面多幾個空格。

技術分享圖片
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     
<stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ #define lson (l , mid , rt << 1) #define
rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//這是一個大根堆q //priority_queue<int,vector<int>,greater<int> >q;//這是一個小根堆q #define fi first #define se second //#define endl ‘\n‘ #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用來壓行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<0||ch>9) f|=(ch==-),ch=getchar(); while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 2009; int fa[maxn*maxn],tot = 0,cnt = 0,t,n,m; bool vis[maxn][maxn]; int dis[maxn][maxn]; struct node { int x,y; }p[maxn]; struct eg{ int u,v,dis; }e[maxn*maxn]; bool cmp(eg a,eg b){ return a.dis < b.dis; } int find(int x){ if(fa[x] == x)return x; return fa[x] = find(fa[x]); } string str[maxn]; int nt[5][5] = { {1,0},{0,1},{-1,0},{0,-1} }; void bfs(int x,int y){ for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ dis[i][j] = inf; vis[i][j] =false; } } queue<pii>q; q.push(pii(x,y)); vis[x][y] = true; dis[x][y] = 0; while(!q.empty()){ pii tmp = q.front();q.pop(); int tx = tmp.fi,ty = tmp.se; for(int i=0; i<4; i++){ int nx = tx + nt[i][0]; int ny = ty + nt[i][1]; if(nx <0||nx >= m || ny <0 || ny >=n)continue; if(str[nx][ny] == #)continue; if(vis[nx][ny])continue; vis[nx][ny] = true; dis[nx][ny] = min(dis[nx][ny], dis[tx][ty]+1); if(str[nx][ny] == A || str[nx][ny] == S){ e[++tot].u = x * n + y; e[tot].v = nx * n + ny; e[tot].dis = dis[nx][ny]; // if(x == 1 && y==1){ // cout<<e[tot].u<<" "<<e[tot].v<<" "<<e[tot].dis<<endl; // } } q.push(pii(nx,ny)); } } } int main(){ OKC; cin>>t; while(t--){ cin>>n>>m; getline(cin,str[0]); tot = 0; for(int i=0; i<m; i++){ getline(cin,str[i]); } for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ if(str[i][j] == A || str[i][j] == S)bfs(i,j); } } for(int i=0; i<=n*m*2; i++)fa[i] = i; sort(e+1,e+1+tot,cmp); int ans = 0; for(int i=1; i<=tot; i++){ int fx = find(e[i].u); int fy = find(e[i].v); if(fx != fy){ ans += e[i].dis; fa[fx] = fy; // debug(e[i].dis); } } cout<<ans<<endl; } return 0; } /* ##### #A#A## # # A# #S ## ##### ##### #AAA### # A# # S ### # # #AAA### ##### */
POJ 3026

POJ - 3026 Borg Maze BFS加最小生成樹