Borg Maze(最小生成树_bfs+prim)

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8835 Accepted: 2956

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

题意:给出 一个m*n的迷宫,空格代表路径,#代表墙, 现在求 从S到达所有A的最短路径和, 重复的路径不计算;

思路:先用bfs求出权值,在用最小生成树计算。

ps:坑了我一下午的题,看了评论区才知道WA一下午的原因是测试数据多了莫名其妙的空格,好想鄙视出题的,我感觉到了来自这个社会深深的鄙视,我还是太年轻!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;

char map[1010][1010];
int map1[1010][1010];//记录字母之间的权值
int site[1010][1010];//记录字母的位置(即编号)
int vis[1010][1010];//bfs中看该条路径是否被访问过
int dis[1010];//记录边的权值
int flag[1010];//记录是否被访问
int ans;//记录字母的个数
int n,m;
int jx[]= {1,0,-1,0};
int jy[]= {0,1,0,-1};
struct node
{
    int x,y,cnt;
};
void bfs(int x,int y)
{
    int i;
    struct node t,f;
    memset(vis,0,sizeof(vis));
    queue<node>q;
    t.x=x;
    t.y=y;
    t.cnt=0;
    vis[t.x][t.y]=1;
    q.push(t);
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        if(map[t.x][t.y]=='S'||map[t.x][t.y]=='A')
        {
            map1[site[x][y]][site[t.x][t.y]]=t.cnt;
        }
        for(i=0; i<4; i++)
        {
            f.x=t.x+jx[i];
            f.y=t.y+jy[i];
            if(f.x>=1&&f.x<m&&f.y>=1&&f.y<n&&map[f.x][f.y]!='#'&&!vis[f.x][f.y])
            {
                f.cnt=t.cnt+1;
                vis[f.x][f.y]=1;
                q.push(f);
            }
        }
    }
}
int prim()
{
    int i,j,k,max,sum=0;
    memset(flag,0,sizeof(flag));
    for(i=1; i<=ans; i++)
        dis[i]=map1[1][i];
    flag[1]=1;
    for(i=1; i<ans; i++)
    {
        max=inf;
        for(j=1; j<=ans; j++)
        {
            if(!flag[j]&&max>dis[j])
            {
                max=dis[j];
                k=j;
            }
        }
        if(max==inf)
        {
            break;
        }
        flag[k]=1;
        sum+=max;
        for(j=1; j<=ans; j++)
        {
            if(dis[j]>map1[k][j]&&!flag[j])
                dis[j]=map1[k][j];
        }
    }
    printf("%d\n",sum);
}
int main()
{
    int T,i,j;
    char str[110];
    scanf("%d",&T);
    while(T--)
    {
        ans=0;
        scanf("%d %d",&n,&m);
        memset(map,0,sizeof(map));
        memset(map1,0,sizeof(map1));
        gets(str);//这个就是那个坑人的地方,因为该数据n,m后面有空格,坑啊;
        int t1=0;//给当前字母标号
        for(i=1; i<=m; i++)
        {
            getchar();//每一行后面都有个\n所以用getchar()消去;
            for(j=1; j<=n; j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j]=='S'||map[i][j]=='A')
                {
                    ans++;//记录字母的总格数,即是点的个数
                    t1++;//从0开始对每个字母标号
                    site[i][j]=t1;//当前字母的标号。
                }
            }
        }
        for(i=1; i<=m; i++)
        {
            for(j=1; j<=n; j++)
            {
                if(i==j)
                    map1[i][j]=0;
                else
                    map1[i][j]=inf;
            }
        }
        for(i=1; i<=m; i++)
        {
            for(j=1; j<=n; j++)
            {
                if(map[i][j]=='S'||map[i][j]=='A')
                {
                    bfs(i,j);
                }
            }
        }

        /*for(i=1;i<=t1;i++)
        {
            for(j=1;j<=t1;j++)
            {
                printf("%d ",map1[i][j]);
            }
            printf("\n");
        }*/
        prim();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rocky0429

一块也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值