POJ3026 Borg Maze (Prim) (BFS)

Source: Internet
Author: User

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12729 Accepted: 4153

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
"Test Instructions" in a Y-row x-column maze, there are walking path spaces ', non-walking walls ' # ', there are two English letters A and S, now starting from S, require the shortest path l to connect all letters, output the total length of this path L.
"Analysis" at the beginning did not understand test instructions, and later understand, is a simple bfs+prim. But WA had several rounds, so sad, to see discuss. Found a lot of people have been pits, enter the legal entity X, Y and then there are a lot of space, you must get lost in advance.
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#include<functional>#defineMoD 1000000007#defineINF 0x3f3f3f3f#definePi ACOs (-1.0)using namespaceStd;typedefLong Longll;Const intn=1005;Const intm=15005;intN,m,t,kk,ii,cnt,edg[n][n],lowcost[n];intd[4][2]= {0,1,1,0,-1,0,0,-1};intVis[n][n];intNum[n][n];CharW[n][n];structMan {intX,y,step;};voidBFS (man s) {queue<man>Q;  while(!q.empty ()) Q.pop ();    Q.push (s); VIS[S.X][S.Y]=1;  while(!Q.empty ()) {Man T=Q.front ();        Q.pop (); if(w[t.x][t.y]=='A'|| w[t.x][t.y]=='S') {            intu=Num[s.x][s.y]; intv=Num[t.x][t.y]; EDG[U][V]=edg[v][u]=T.step; }         for(intL=0; l<4; l++) {            intxx=t.x+d[l][0],yy=t.y+d[l][1]; if(xx>=0&&xx<n&&yy>=0&&yy<m&&vis[xx][yy]==0&&w[xx][yy]!='#') {man k; k.x=xx,k.y=yy; K.step=t.step+1;                Q.push (k); VIS[XX][YY]=1; }        }    }}voidPrim () { for(intI=0; i<cnt; i++) {Lowcost[i]=Edg[ii][i]; } Lowcost[ii]=-1; intsum=0;  for(intI=1; i<cnt; i++) {        intminn=inf;  for(intj=0; j<cnt; J + +) {            if(lowcost[j]!=-1&&lowcost[j]<Minn) {Minn=Lowcost[j]; KK=J; }} sum+=Minn; LOWCOST[KK]=-1;  for(intj=0; j<cnt; J + +) {            if(edg[j][kk]<Lowcost[j]) {Lowcost[j]=EDG[J][KK]; }}} printf ("%d\n", sum);}intMain () {scanf ("%d",&t);  while(t--) {memset (EDG,0,sizeof(EDG)); memset (Lowcost,0,sizeof(lowcost)); scanf ("%d%d",&m,&N); CharTmp[n];gets (TMP);//must have this, this is the most pit of the subject.         for(intI=0; i<n; i++) {gets (w[i]); } CNT=0;  for(intI=0; i<n; i++) {             for(intj=0; j<m; J + +) {                if(w[i][j]=='A'|| w[i][j]=='S') {Num[i][j]=cnt++; if(w[i][j]=='S') {II=cnt-1; }                }            }        }         for(intI=0; i<n; i++) {             for(intj=0; j<m; J + +) {                if(w[i][j]=='A'|| w[i][j]=='S') {man S; S.x=i; S.y=J; S.step=0; memset (Vis,0,sizeof(VIS));                BFS (s);    }}} Prim (); }    return 0;}
View Code

POJ3026 Borg Maze (Prim) (BFS)

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.