Borg Maze--(prime+BFS)

Borg Maze

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 71   Accepted Submission(s) : 22
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
  
  
2 6 5 ##### #A#A## # # A# #S ## ##### 7 7 ##### #AAA### # A# # S ### # # #AAA### #####
 

Sample Output
  
  
8 11

题意:求从s出发走遍所有'A'点的最短路径,且在起点处或到达'A'点时,可以分成多条路

先记录下所有'A'和'S'点的坐标,求出他们两两之间的距离,然后转化为最小生成树问题,用prime解决

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
#define inf 1000000
struct node {
    int x,y,id,step;
}p[105];            //p是存放'A'点的数据的结构数组,id是编号,step是BFS是用来记录路径的
int len[105][105];//将记录好的'A'点两两之间距离转化为矩阵,就彻底变为最小生成树问题
char s[55][55];
int dis[105];  //prime算法中用于记录最小边权
bool vis[55][55],ok[105]; //记录使用情况
int x,y,num;     //x,y是所给矩阵大小,num是'A'点个数
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void bfs(int id) //传边的起点编号
{
    node t1,t2;
    memset(vis,false,sizeof(vis));
    queue<node>Q;
    t1.x=p[id].x; t1.y=p[id].y; t1.step=0;
    vis[p[id].x][p[id].y]=true;
    Q.push(t1);
    while(!Q.empty())
    {
        t1=Q.front();
        Q.pop();
        for(int i=0;i<num;i++)
        {
            if(t1.x==p[i].x&&t1.y==p[i].y) //如果走到了其余'A'点,记录下最短路径
            {
                len[id][p[i].id]=len[p[i].id][id]=t1.step;
            }
        }
        for(int i=0;i<4;i++) //继续向4个放向走
        {
            t2.x=t1.x+dx[i];
            t2.y=t1.y+dy[i];
            if(t2.x>=0&&t2.x<x&&t2.y>=0&&t2.y<y&&!vis[t2.x][t2.y]&&s[t2.x][t2.y]!='#')
            {
                t2.step=t1.step+1;
                vis[t2.x][t2.y]=true;
                Q.push(t2);
            }
        }
    }
}
void prime()
{
    memset(ok,false,sizeof(ok));
    for(int i=1;i<num;i++)   //从第一个'A'点生成树,因为从哪个点生成都可以,并不是非得从‘S'点
        dis[i]=len[0][i];
    dis[0]=0;
    ok[0]=true;
    int total=0;
    for(int i=1;i<num;i++)
    {
        int minn=inf; int k;
        for(int j=1;j<num;j++)
        {
            if(!ok[j]&&dis[j]<minn) {minn=dis[j]; k=j;}
        }
        if(minn==inf) break;
        total+=dis[k];
        ok[k]=true;
        for(int j=1;j<num;j++)
        {
            if(!ok[j]&&dis[j]>len[k][j]) dis[j]=len[k][j];
        }
    }
    printf("%d\n",total);
}
int main()
{
    int t,i,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&y,&x);
        gets(s[0]); //看了题解才知道,x,y后面可能有空格
        for(i=0;i<x;i++)
            gets(s[i]);
        num=0;
        memset(p,0,sizeof(p));
        for(i=0;i<x;i++)
            for(j=0;j<y;j++)
        {
            if(s[i][j]=='A'||s[i][j]=='S')
            {
                p[num].x=i;
                p[num].y=j;
                p[num].id=num;
                num++;
            }
        }
        for(i=0;i<num;i++)   //对于每个点求它与所有点的距离
        {
            bfs(i);
        }
        prime();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值