Borg Maze

来源:互联网 发布:电脑有网络玩不了游戏 编辑:程序博客网 时间:2024/05/22 23:11

Borg Maze

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 67   Accepted Submission(s) : 21
Problem 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########
 
      题意:给出地图,空格是路,#是墙,让你求从S开始抓到所有外星人A的步数。可以把自己理解成一个军队,你可以在某个岔路口分头行动,一部分去这里,一部分走那里,球总的步数。这个题其实S和A没有什么区别。只要先广搜求出啦每两个点之间的最短路径,再求一个最小生成树就好了。

      注意输入有坑!空格,gets输入最好了,但是有一个回车的问题,在网上找的fflush(stdin)不对啊,多加一个gets()解决。

代码如下:

#if 0                               //广搜出距离    Prim求路径    输入有空格#include<iostream>#include<cstring>#include<stdio.h>#include<queue>using namespace std;struct Node{    int x,y,t;};Node node[2510];int t,n,m,K;char p[51][51];int edge[2550][2550];int dxy[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int ok(int x,int y){    if(x>0&&x<=n && y>0&&y<=m)        return 1;    return 0;}int bfs(int s){    queue<Node>q;    int vis[51][51]={0};    Node a,b;    a=node[s];    vis[a.x][a.y]=1;    q.push(a);    do    {        a=q.front();        //cout<<a.x<<"  "<<a.y<<"  "<<a.t<<endl;        q.pop();        for(int k=0;k<4;k++)        {            b.x=a.x+dxy[k][0];            b.y=a.y+dxy[k][1];            b.t=a.t+1;            if(ok(b.x,b.y) && vis[b.x][b.y]==0 && p[b.x][b.y]!='#')            {                if(p[b.x][b.y]=='A' || p[b.x][b.y]=='S')                {                    for(int i=1;i<=K;i++)                    {                        if(b.x==node[i].x && b.y==node[i].y)                        {                            edge[s][i]=b.t;                            //cout<<"( "<<s<<","<<i<<")  "<<b.t<<endl;                        }                    }                }                {                    q.push(b);                    vis[b.x][b.y]=1;                }            }        }    }while(!q.empty());    return 0;}int ioan(){    int sum=0;    int minn[2550];    int vis[2550]={0};    memset(minn,0x7f,sizeof(minn));    minn[1]=0;    for(int i=1;i<=K;i++)    {        int k=0;        for(int j=1;j<=K;j++)        {            if(!vis[j] && minn[j]<minn[k])                k=j;        }        vis[k]=1;        sum+=minn[k];        for(int j=1;j<=K;j++)        {            if(!vis[j] && edge[k][j]<minn[j])                minn[j]=edge[k][j];        }    }    return sum;}int main(){    char s[51];    cin>>t;    while(t--)    {        //memset(vis,0,sizeof(vis));        memset(edge,0x7f,sizeof(edge));        memset(node,0,sizeof(node));        cin>>m>>n;        K=0;        //fflush(stdin);          //百度来的清空空格不对啊!!!!!不如直接gets()一次来的实在!!!        gets(s);        for(int i=1;i<=n;i++)        {            gets(s);            for(int j=1;j<=m;j++)            {                p[i][j]=s[j-1];                if(s[j-1]=='S' || s[j-1]=='A')                {                    K++;                    node[K].x=i;                    node[K].y=j;                    node[K].t=0;                }            }        }        for(int i=1;i<=K;i++)        {            //cout<<"node  i "<<i<<"  "<<node[i].x<<"  "<<node[i].y<<endl;            bfs(i);        }        int ans=ioan();        cout<<ans<<endl;    }    return 0;}#endif // 




原创粉丝点击