前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 3026 Borg Maze「建议收藏」

POJ 3026 Borg Maze「建议收藏」

作者头像
全栈程序员站长
发布2022-07-10 14:26:16
1520
发布2022-07-10 14:26:16
举报

大家好,又见面了,我是全栈君。

Borg Maze

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 7998

Accepted: 2675

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

代码语言:javascript
复制
2
6 5
##### 
#A#A##
# # A#
#S  ##
##### 
7 7
#####  
#AAA###
#    A#
# S ###
#     #
#AAA###
#####  

Sample Output

代码语言:javascript
复制
8
11

最小生成树问题,主要是要求出点点距离,即字母间边权。这里用BFS求出了边权,用prim算法求出了最小生成树路径。

AC代码例如以下:

代码语言:javascript
复制
<pre name="code" class="cpp">#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#define inf 100000
using namespace std;

char map[60][60];
int vis[60][60],v[60][60];
int n,m,tt,ans;
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};

struct H
{
    int h,z;
}a[10005];

int w[251][251],lc[251],vv[60][60];//W是边权记录,LCprim算法运用,VV记录第几个字母的坐标
struct HH
{
    int h,z,st;
};

int bfs(int h,int z)
{
    int i;
    v[h][z]=1;
    queue <HH> q;
    HH a,b,c;
    a.h=h;
    a.z=z;
    a.st=0;
    q.push(a);
    while(!q.empty())
    {
        b=q.front();
        q.pop();
        if(vv[b.h][b.z])//统计边权
            {
                w[vv[h][z]][vv[b.h][b.z]]=b.st;
                w[vv[b.h][b.z]][vv[h][z]]=b.st;
                //cout<<vv[h][z]<<" "<<vv[b.h][b.z]<<" "<<b.st<<endl;
            }

        for(i=0;i<4;i++)
        {
            c.h=b.h+dx[i];
            c.z=b.z+dy[i];
            if(c.h>=0&&c.h<n&&c.z>=0&&c.z<m&&map[c.h][c.z]!='#'&&!v[c.h][c.z])
            {
                v[c.h][c.z]=1;
                c.st=b.st+1;
                q.push(c);
            }
        }
    }
}

void prim()
{
    int i,j;
    int m,id;
    for(i=1;i<tt;i++)
    {
        lc[i]=w[1][i];
    }

    lc[1]=0;
    for(j=1;j<tt-1;j++)
    {
        m=inf;
        for(i=1;i<tt;i++)
        if(lc[i]!=0&&lc[i]<m)
        {m=lc[i];id=i;}
        ans+=m;
        lc[id]=0;
        for(i=1;i<tt;i++)
        {
            if(lc[i]!=0&&lc[i]>w[id][i])
                lc[i]=w[id][i];
        }
    }

}

int main()
{
    int i,j,l;
    int t;
    char c[10];
    scanf("%d",&t);
    gets(c);
    while(t--)
    {
        tt=1;
        memset(vis,0,sizeof vis );
        cin>>m>>n;
        gets(c);
        for(i=0;i<n;i++)
            gets(map[i]);
        //for(i=0;i<n;i++)
            //cout<<map[i]<<endl;
        int bj=0;
        memset(vv,0,sizeof vv);
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            if(map[i][j]=='A'||map[i][j]=='S')
            {
                vv[i][j]=tt++;//记录字母序号及个数
            }
        }
        for(i=1;i<tt;i++)
            for(j=1;j<=i;j++)
                if(i==j)
                w[i][j]=0;
                else {
                    w[i][j]=inf;
                    w[j][i]=inf;
                }
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            {
                if((map[i][j]=='A'||map[i][j]=='S')&&!vis[i][j])
                {
                    vis[i][j]=1;
                    memset(v,0,sizeof v);
                    bfs(i,j);//求出这个字母到各个字母产生的边权
                }

            }

        ans=0;
        prim();//prim算法的运用
        cout<<ans<<endl;
    }
    return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115412.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022年2月4,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档