Borg Maze (bfs+最小生成树)

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

题目大概:

在迷宫里抓住所有外星人的最佳方式。

思路:

用bfs()求出所有点(包括S和所有A)之间的最短距离,再用prim最小生成树求出最短路的距离。

这个题的输入非常坑,错了几次都是在那里,首先在输入地图前,有一个换行符,在输入数据中间后面还有空格,我用getchar()试过,是wr,所以就换了一个方法。

代码:


#include <iostream>
#include <string>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
const int INF = 0x3f3f3f3f;
using namespace std;
int n,pp;
char map[55][55];
int vis[55][55],p[250][250],a[250][250],b[250],vi[250];
int x,y,x1,y1;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

struct poin
{
    int s,d,tt;
};

int bfs(int w,int e)
{
    poin t;
    t.s=w;t.d=e;t.tt=0;
    queue<poin>q;
    vis[t.s][t.d]=1;
    q.push(t);
    poin w1,w2;
    while(!q.empty())
{    w1=q.front();
     q.pop();
     if(map[w1.s][w1.d]=='S'||map[w1.s][w1.d]=='A'){
             a[p[w][e]][p[w1.s][w1.d]]=w1.tt;
             a[p[w1.s][w1.d]][p[w][e]]=w1.tt;}
     for(int i=0;i<4;i++)
     {
         w2.s=w1.s+dx[i];
         w2.d=w1.d+dy[i];

         if(w2.s>=0&&w2.s<y&&w2.d>=0&&w2.d<x&&map[w2.s][w2.d]!='#'&&!vis[w2.s][w2.d])
         {

             vis[w2.s][w2.d]=1;
             w2.tt=w1.tt+1;
             q.push(w2);

         }

     }


}

}

int prim(int qq)
{
    memset(vi,0,sizeof(vi));
    for(int i=1;i<pp;i++)
    b[i]=a[qq][i];
    int sum=0;
    vi[qq]=1;
    for(int i=2;i<pp;i++)
    {   int k=i,minn=INF;
        for(int j=1;j<pp;j++)
        {
            if(!vi[j]&&minn>b[j])
            {
                minn=b[j];
                k=j;
            }
        }
        sum+=minn;
        vi[k]=1;
        for(int j=1;j<pp;j++)
        {
            if(!vi[j]&&b[j]>a[k][j])
            {
                b[j]=a[k][j];
            }
        }

    }
    return sum;

}

int main()
{
     scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d\n",&x,&y);
        pp=1;

        for(int j=0;j<y;j++)
        {
            gets(map[j]);
            for(int k=0;k<x;k++)
            {
                if(map[j][k]=='S'||map[j][k]=='A'){
                p[j][k]=pp;
                pp++;
                }
            }
        }



        for(int i=0;i<=pp;i++)
        {
            for(int j=0;j<=pp;j++)
            {
                if(i==j){a[i][j]=0;}
                else a[i][j]=INF;
            }
        }

           for(int i=0;i<y;i++)
        {
            for(int j=0;j<x;j++)
            {
               if(map[i][j]=='S'||map[i][j]=='A')
               {memset(vis,0,sizeof(vis));
                   bfs(i,j);

               }
            }
        }

        cout<<prim(1)<<endl;
    }

    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值