POJ 3026 Borg Maze

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6124 Accepted: 2055

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

Source

今天过了训练2的最后一个题,先说说这个题吧 这个题只要是理清了思路,用bfs和最短路程序%99的就对了,但是确实很容易犯错误,因为这与长期以来我们编写程序的方式有关,我们在处理输入含有空格的字符的时候习惯用get(c语言) ,cin.get(c++), 但是这样最后的回车会作为一个字符,我们在处理的时候还是调用get,cin.get 来过滤掉这个回车。这是我们常用的方法,但是这种方法是有弊端的,比如 输入一个数n 代表输入n个字符,我们会这样输入
int n;
char c;
cin>>n;
cin.get();
for(i=1;i<=n;i++)
{
    c=cin.get();
}

这是我们常用的方法吧,但是这样做有弊端,仔细想一想就会发现如果 再输入整数n之后没有接着按下回车,而是按空格。然后按回车,这就不对了,这题就是专门卡这一点,cin.get() 只能过滤掉一个字符。 所以在过滤的时候用gets(s1)《c语言》,getline(cin,s1)<C++> 这样就行了,以后再编写的程序的时候也这样写吧,毕竟我们不知道数据什么就会多出空格。
     这个训练2的计划做起来还是比较吃力的,因为有最大流,这个新知识点,最大流主要就是建图,还有题目的题意很难理解。  不过最后还是挺过来了,终于完成了。
    训练2做完了,也快考试了,可以安心的准备考试了。
#include <iostream>
#include <string.h>
using namespace std;
int a[100][100],b[120][120];
int c[120][4],res[120];
int status[120][120],queue[100000][2];
int vex[]={-1,1,0,0};
int vey[]={0,0,-1,1};
int INF=0x7fffffff;
int n,m;
string s1;
int main()
{
    void bfs(int x,int y);
    int i,j,s,t,k,min1,sum;
    char d;
    cin>>t;
    while(t--)
    {
        cin>>m>>n;
        getline(cin,s1);
        s=2;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                d=cin.get();
                if(d=='#')
                {
                    a[i][j]=0;
                }else if(d=='A')
                {
                    a[i][j]=s;
                    c[s][0]=i; c[s][1]=j; s++;
                }else if(d=='S')
                {
                    a[i][j]=1;
                    c[1][0]=i; c[1][1]=j;
                }else if(d==' ')
                {
                    a[i][j]=-1;
                }
            }
            cin.get();
        }
        for(i=1;i<=s-1;i++)
        {
            for(j=1;j<=s-1;j++)
            {
                b[i][j]=INF;
            }
        }
        for(i=1;i<=s-1;i++)
        {
            bfs(c[i][0],c[i][1]);
        }
        s-=1;
        for(i=1;i<=s;i++)
        {
            res[i]=b[1][i];
        }
        sum=0;
        for(i=1;i<=s-1;i++)
        {
            min1=INF;
            for(j=2;j<=s;j++)
            {
                if(min1>res[j]&&res[j]!=0)
                {
                    min1=res[j];
                    k=j;
                }
            }
            sum+=min1;
            res[k]=0;
            for(j=2;j<=s;j++)
            {
                if(res[j]>b[k][j])
                {
                    res[j]=b[k][j];
                }
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}
void bfs(int x,int y)
{
    int i,j,base,top,posx,posy;
    int xend,yend,sum[120][120];
    posx=x; posy=y;
    memset(status,0,sizeof(status));
    memset(sum,0,sizeof(sum));
    base=top=0;
    queue[top][0]=x; queue[top++][1]=y;
    status[x][y]=1;
    while(base<top)
    {
        x=queue[base][0]; y=queue[base++][1];
        for(i=0;i<=3;i++)
        {
            xend=x+vex[i]; yend=y+vey[i];
            if(xend>=1&&xend<=n&¥d>=1&¥d<=m&&a[xend][yend]!=0&&!status[xend][yend])
            {
                sum[xend][yend]=sum[x][y]+1;
                if(a[xend][yend]!=-1)
                {
                    b[a[posx][posy]][a[xend][yend]]=sum[xend][yend];
                }
                queue[top][0]=xend; queue[top++][1]=yend;
                status[xend][yend]=1;
            }
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
未来社区的建设背景和需求分析指出,随着智能经济、大数据、人工智能、物联网、区块链、云计算等技术的发展,社区服务正朝着数字化、智能化转型。社区服务渠道由分散向统一融合转变,服务内容由通用庞杂向个性化、服务导向转变。未来社区将构建数字化生态,实现数据在线、组织在线、服务在线、产品智能和决策智能,赋能企业创新,同时注重人才培养和科研平台建设。 规划设计方面,未来社区将基于居民需求,打造以服务为中心的社区管理模式。通过统一的服务平台和应用,实现服务内容的整合和优化,提供灵活多样的服务方式,如推送式、订阅式、热点式等。社区将构建数据与应用的良性循环,提高服务效率,同时注重生态优美、绿色低碳、社会和谐,以实现幸福民生和产业发展。 建设运营上,未来社区强调科学规划、以人为本,创新引领、重点突破,统筹推进、整体提升。通过实施院落+社团自治工程,转变政府职能,深化社区自治法制化、信息化,解决社区治理中的重点问题。目标是培养有活力的社会组织,提高社区居民参与度和满意度,实现社区治理服务的制度机制创新。 未来社区的数字化解决方案包括信息发布系统、服务系统和管理系统。信息发布系统涵盖公共服务类和社会化服务类信息,提供政策宣传、家政服务、健康医疗咨询等功能。服务系统功能需求包括办事指南、公共服务、社区工作参与互动等,旨在提高社区服务能力。管理系统功能需求则涉及院落管理、社团管理、社工队伍管理等,以实现社区治理的现代化。 最后,未来社区建设注重整合政府、社会组织、企业等多方资源,以提高社区服务的效率和质量。通过建立社区管理服务综合信息平台,提供社区公共服务、社区社会组织管理服务和社区便民服务,实现管理精简、高效、透明,服务快速、便捷。同时,通过培育和发展社区协会、社团等组织,激发社会化组织活力,为居民提供综合性的咨询和服务,促进社区的和谐发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值