poj 3026 Borg Maze

来源:互联网 发布:网络炒股骗局 编辑:程序博客网 时间:2024/06/17 00:55

Borg Maze
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8729 Accepted: 2929

Description

The Borg is an 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 is linked to the collective by a sophisticated subspace network that insures each member is given constant supervision and guidance. 

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

Input

On the first line of input there is one integer, N <= 50, giving the number of test cases in the input. Each test case starts with a line containg two 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 an alien, and the capital letter ``S'' stands for the start of the search. The perimeter of the maze is always closed, i.e., there is no way to get out from the coordinate of the ``S''. At most 100 aliens are 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

26 5##### #A#A### # A##S  ####### 7 7#####  #AAA####    A## S ####     ##AAA########  

Sample Output

811

这道题目的意思是从S到达所有A路径长度之和最短,但有一个附加条件,也就是可以在S和A分身,这样的话,到达一些点后,这些点都可以作为起点,去达到那些没有到过的点,这个刚好就是prim算法的思想,也就是求最小生成树,不过这道题目需要预处理得到所有A和S两两之间的最短路,这个可以对每一个点进行一次bfs。这题有点坑,因为数字后面有多余的空格,因此光一个getchar()吃回车不够,需要把那些空格一起吃掉。

代码:

#include<cstdio>#include<iostream>#include<cstring>#define Maxn 110using namespace std;struct point{    int x,y,d;    point(){}    point(int xx,int yy,int dd):x(xx),y(yy),d(dd){}}q[Maxn*Maxn];char mat[Maxn][Maxn];int id[Maxn][Maxn],vis[Maxn][Maxn],adj[Maxn][Maxn],lowcost[Maxn];int dx[]={0,0,1,-1};int dy[]={-1,1,0,0};int n,m;void bfs(point p){    memset(vis,0,sizeof vis);    int s=0,e;    q[e=0]=p;    vis[p.x][p.y]=1;    while(s<=e){        point t=q[s++];        for(int i=0;i<4;i++){            int tx=t.x+dx[i],ty=t.y+dy[i];            if(tx<0||ty<0||tx>=n||ty>=m||vis[tx][ty]||mat[tx][ty]=='#') continue;            q[++e]=point(tx,ty,t.d+1);            vis[tx][ty]=1;            if(id[tx][ty]!=-1)                adj[id[p.x][p.y]][id[tx][ty]]=adj[id[tx][ty]][id[p.x][p.y]]=t.d+1;        }    }}void prim(int u,int n){    int ans=0;    for(int i=0;i<n;i++)        lowcost[i]=adj[u][i];    for(int i=1;i<n;i++){        int minn=1<<30,v;        for(int j=0;j<n;j++)            if(lowcost[j]!=-1&&lowcost[j]<minn)                minn=lowcost[j],v=j;        ans+=minn;        for(int j=0;j<n;j++)            lowcost[j]=min(lowcost[j],adj[v][j]);    }    printf("%d\n",ans);}int main(){    int t;    scanf("%d",&t);    while(t--){        scanf("%d%d",&m,&n);        int tot=0;        memset(id,-1,sizeof id);        for(int i=0;i<n;i++){            while(getchar()!='\n');            for(int j=0;j<m;j++){                scanf("%c",&mat[i][j]);                if(mat[i][j]=='A'||mat[i][j]=='S')                    id[i][j]=tot++;            }        }        for(int i=0;i<n;i++)            for(int j=0;j<m;j++)                if(id[i][j]!=-1) bfs(point(i,j,0));        for(int i=0;i<tot;i++) adj[i][i]=-1;        prim(0,tot);    }return 0;}


0 0
原创粉丝点击