POJ 3026 Borg Maze「建议收藏」

POJ 3026 Borg Maze

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

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

2
6 5
##### 
#A#A##
# # A#
#S  ##
##### 
7 7
#####  
#AAA###
#    A#
# S ###
#     #
#AAA###
#####  

Sample Output

8
11

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

AC代码例如以下:

<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.net/115412.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • c语言枚举类型enum例子_枚举是什么意思

    c语言枚举类型enum例子_枚举是什么意思在实际的编程应用中,有的变量只有几种可能的取值,譬如说一个星期的七种可能,性别的两种可能等等。C语言为这种类型的变量的定义提供了enum关键字。要使用枚举类型的变量,首先需要先定义一个枚举类型名,然后再声明该变量是枚举类型的例如:enumweekday{\\该语句定义了一个`枚举类型`MONDAY,TUSEDAY,WEDNESDAY,…

    2025年8月19日
    4
  • SPI协议简单介绍

    SPI协议简单介绍导言SPI是串行扩展总线。串行总线技术可以使系统的硬件设计大大简化、系统的体积减小、可靠性提高。同时系统的更改和扩充极为容易。常用的串行扩展总线有:I2C(InterICBus)总线、单总线(1-WIREBUS)、SPI(SerialPeripheralInterface)总线及Microwire/PLUS等。一、SPI协议SPI总线是微控制器四线的外部总线。SPI没有明文标准,是一种事实总线,对通信操作的实现由芯片厂商和驱动开发者通过datasheet和applicat..

    2022年10月15日
    3
  • hihoCoder – 1082 – 然而沼跃鱼早就看穿了一切 (字符串处理!!)

    hihoCoder – 1082 – 然而沼跃鱼早就看穿了一切 (字符串处理!!)

    2022年2月5日
    54
  • 下载网页视频并自动合成视频[通俗易懂]

    下载网页视频并自动合成视频[通俗易懂]1.首先使用Chrome打开网页,单击F12打开开发者工具开始视频播放,在F12出来的界面中单击Network在Network中有文件列表,检查当中是否存在m3u8结尾的文件2.如果有m3u8结尾的文件,把它的源地址复制下来源地址复制下来可能分两段(两个http),一段是跳转地址,一段是目标地址,将目标地址保留下来即可。正确的m3u8文件地址大概的样子在下面的命令示例中…

    2022年5月5日
    64
  • SRS软件需求规格说明书_SOR是什么文件

    SRS软件需求规格说明书_SOR是什么文件【摘要】随着信息时代科技的飞速发展,经济全球化已广为人知,英语作为全球最主要的语言之一,受到越来越多的人的喜爱,不仅为了增长知识,也为了能适应社会发展的需求。但是,学英语最重要的事首先是积累词汇,没有

    2022年8月2日
    5
  • Snapde和常用的CSV文件编辑器对比

    Snapde和常用的CSV文件编辑器对比Snapde,一个专门为编辑超大型数据量CSV文件而设计的单机版电子表格软件;它运行的速度非常快,反应非常灵敏。CSV是一种用逗号分隔列、回车分割行的文本文件,市面上常用的CSV编辑软件有:Snapde、Ron’sEditor、CSVEditorPro、DMcsvEditor、CSVPad、CSVed、CSVFileView、KillinkCSVEditor、CSVBuddy、Me…

    2022年7月21日
    27

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号