POJ 3026 Borg Maze

Source: Internet
Author: User

Minimum spanning Tree +bfs


Test instructions is to find the smallest spanning tree in the maze that connects all points. Other miscellaneous items are entirely disregarded. It took me a long time to understand test instructions.


I used the Kruskal. Each point is labeled at the time of input. Then BFS every point. Find out all the recent edges, and then the template Kruskal.


Because it is a labyrinth, it is only possible to use BFS to search the shortest path to every point it communicates with.


Just the data a bit of a pit, the proposed array is larger, I submit the time re once. Definitely more than 100 points.


Then after you have entered the length and width. Unexpectedly still have inexplicable space, so can not getchar, directly get a bar, this let me wa once.


And then it came down to AC.


#include <cstdio> #include <cstring> #include <string> #include <queue> #include <algorithm > #include <queue> #include <map> #include <stack> #include <iostream> #include <list># include<set> #include <cmath> #define INF 0x7fffffff#define eps 1e-6using namespace std;int n,m,xl,yl;int cot ; int xx[]= { -1,1,0,0};int yy[]= {0,0,-1,1};struct lx{int u,v,len;} l[50*1001];int fa[1001];bool Vis[151][151];char g[1 51][151];int site[151][151];struct node{int x,y,lv;} point[1001];bool cmp (lx A,lx b) {return a.len<b.len;} int father (int x) {if (x!=fa[x]) return Fa[x]=father (Fa[x]);}    void BFS (node S) {memset (vis,0,sizeof (VIS));    queue<node>q;    Q.push (S);    Vis[s.x][s.y]=1;    int U=SITE[S.X][S.Y];    int num=1;        while (!q.empty ()) {node now,tmp;        Tmp=q.front (); Q.pop ();        int V=SITE[TMP.X][TMP.Y];            if (v>=1&&u!=v) {l[cot].u=u;            L[cot].v=v; l[cot++].len=tmp.lv;        num++;        } if (num==n) break;            for (int k=0;k<4;k++) {int x=tmp.x+xx[k];            int y=tmp.y+yy[k]; if (y>=yl| | y<0| | x>=xl| | x<0| | g[x][y]== ' # ' | |            Vis[x][y]) continue;            Vis[x][y]=1;            now.lv=tmp.lv+1;            Now.x=x,now.y=y;        Q.push (now);    }}}int Main () {int t;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &xl,&yl);        Char str[51];        memset (site,0,sizeof (site));        N=1;        Gets (str);            for (int i=0; i<yl; i++) {gets (str);                for (int j=0; j<xl; J + +) {G[i][j]=str[j]; if (str[j]== ' A ' | |                    str[j]== ' S ') {site[i][j]=n;                    Point[n].x=i,point[n].y=j;                point[n++].lv=0;        }}} cot=0;        n--;  for (int i=1; i<=n; i++)          BFS (Point[i]);        for (int i=1; i<=n; i++) fa[i]=i;        Sort (l,l+cot,cmp);        int ans=0;            for (int i=0;i<cot;i++) {int fu=father (L[I].U);            int Fv=father (L[I].V);            if (FU==FV) continue;            Fa[fv]=fu;        Ans+=l[i].len;    } printf ("%d\n", ans); }}


POJ 3026 Borg Maze

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.