POJ Borg Maze (bfs+ minimum spanning tree)

Source: Internet
Author: User

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10428 Accepted: 3463

Description

The Borg is a immensely powerful race of enhanced humanoids from the Delta quadrant of the Galaxy. The Borg Collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual are linked to the collective by a sophisticated subspace network that insures each member are given con Stant supervision and guidance.

Your task is to help the Borg (yes, really) by developing a program which helps the Borg-estimate the minimal cost of s Canning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is, the beginning of the search is conducted by a large group of over individuals. Whenever an alien was assimilated, or at the beginning of the search, the group could split in and more groups (but their Consciousness is still collective.). The cost of searching a maze are definied as the total distance covered by all the groups involved in the search together. That's, if the original group walks five steps, then splits into both groups each walking three steps, the total distance Is 11=5+3+3.

Input

On the first line of input there are one integer, N <=, giving the number of test cases in the input. Each test case starts with a line containg the integers x, y such that 1 <= x, y <= 50. After this, y lines follow, each which x characters. For each character, a space "' stands for an open space, a hash mark ' # '" stands for an obstructing wall, the capital Letter "A" stand for a alien, and the capital letter "S" stands for the start of the search. The perimeter of the maze is always closed, i.e., and there is the no-to-get out from the coordinate of the ' S '. At most of aliens is present in the maze, and everyone is reachable.

Output

For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive .

Sample Input

5##### #A #a### # a# #S  ####### 7 7##### #AAA # # # # # # # # # # # #     #AAA a##  

Sample Output

811

Source

Svenskt Mästerskap I programmering/norgesmesterskapet 2001 take a,s as the same point, use BFS to generate the distance between all points and run it again. Minimum spanning tree
#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<string>#include<iostream>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<vector>#include<map>#include<stdlib.h>#include<algorithm>#defineLL __int64using namespacestd;Const intmaxn= -+5;Const intinf=0x3f3f3f3f;intKase,n,m,tot;CharMAT[MAXN][MAXN];intA[MAXN][MAXN];intW[MAXN][MAXN];intwis[maxn*MAXN];intVIS[MAXN][MAXN];intD[MAXN];intdir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};Charopt[Ten];structnode{intx, y; intStep;} s;voidBFS (intSxintSy) {memset (Vis,0,sizeof(VIS)); Queue<node>Q;  while(!Q.empty ())    Q.pop (); S.x=sx,s.y=sy,s.step=0; Vis[sx][sy]=1;    Q.push (s);  while(!Q.empty ())        {node Now,next; now=Q.front (); Q.pop ();//printf ("%d%d\n", now.x,now.y);        if(mat[now.x][now.y]=='A'|| mat[now.x][now.y]=='S') W[a[sx][sy]][a[now.x][now.y]]=Now.step;  for(intI=0;i<4; i++) {Next.x=now.x+dir[i][0]; Next.y=now.y+dir[i][1]; if(mat[next.x][next.y]!='#'&&!vis[next.x][next.y] &&1<=next.x && next.x<=n &&1<=next.y && next.y<=m) {next.step=now.step+1; VIS[NEXT.X][NEXT.Y]=1;            Q.push (next); }        }    }}voidPrim () {memset (WIS,0,sizeof(WIS));  for(intI=2; i<tot;i++) d[i]=w[1][i]; wis[1]=1; intans=0;  for(intI=1; i<tot-1; i++)    {        inttmp,minn=INF;  for(intj=1; j<tot;j++)        {            if(!wis[j] && d[j]<Minn) {Minn=D[j]; TMP=J; }        }        if(Minn==inf) Break; WIS[TMP]=1; Ans+=Minn;  for(intj=1; j<tot;j++)            if(!wis[j] && w[tmp][j]<D[j]) d[j]=W[tmp][j]; } printf ("%d\n", ans);}intMain () {//freopen ("In.txt", "R", stdin);scanf"%d",&Kase);  while(kase--) {memset (W,0,sizeof(w)); scanf ("%d%d",&m,&N); Gets (mat[0]); Tot=1; Memset (A,-1,sizeof(a));  for(intI=1; i<=n;i++) {gets (Mat[i]+1); printf ("%s", Mat[i]);  for(intj=1; j<=m;j++)                if(mat[i][j]=='A'|| mat[i][j]=='S') A[i][j]=tot++; }         for(intI=1; i<=n;i++)             for(intj=1; j<=m;j++)                if(a[i][j]!=-1) BFS (I,J);    Prim (); }    return 0;}
View Code

POJ Borg Maze (bfs+ minimum spanning tree)

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.