POJ 3026 Borg Maze

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15508 Accepted: 5019

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


题意:

给出一个图,'#'表示墙,' '表示路,求将所有的S和A连接起来的最小代价。

解题思路:

因为题目给的图很小,只有50*50,而且A最多只有100个,所以,我们可以用BFS来把路全部搜出来,然后再用kruskal算法,算出最小生成树。然后就WA了。为什么WA?并不是解法错误。要不是看了discuss,不然怎么写都不能AC。原因是后台输入的“6 5             ”在5的后面有一行空格!题目中看不出来啊!还我1A。。

附上AC代码

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#include<queue>
#include<algorithm>
#define maxn 55

int n,m,sum,cnt;
struct trip
{
    int x,y,step;
};
struct road
{
    int from,to,val;
} arr[20000];           /*存路的起点、终点、权值*/
int p[105];             /*并查集数组*/
char pic[maxn][maxn];
int flag[maxn][maxn];
int ser[maxn][maxn];    /*ser数组用来存S和A的序号,方便并查集初始化*/
int mov[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};

int check(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m||pic[x][y]=='#') return 1;
    return flag[x][y];
}
void bfs(int r,int s)
{
    memset(flag,0,sizeof(flag));
    queue<trip> q;
    trip now,next;
    now.x=r;
    now.y=s;
    now.step=0;
    flag[r][s]=1;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(ser[now.x][now.y]&&ser[r][s]<=ser[now.x][now.y])
        {
            arr[sum].from=ser[r][s];
            arr[sum].to=ser[now.x][now.y];
            arr[sum++].val=now.step;
        }
        for(int i=0; i<4; i++)
        {
            next.x=now.x+mov[i][0];
            next.y=now.y+mov[i][1];
            if(check(next.x,next.y)) continue;
            flag[next.x][next.y]=1;
            next.step=now.step+1;
            q.push(next);
        }
    }
}
int cmp(road a,road b)
{
    return a.val<b.val;
}
int find(int x)
{
    return p[x]==x?x:find(p[x]);
}
int kruskal()
{
    for(int i=1; i<=cnt; i++)
        p[i]=i;
    int ans=0,asd=0;
    for(int i=0; i<sum; i++)
    {
        int x=find(arr[i].from);
        int y=find(arr[i].to);
        if(x!=y)
        {
            ans+=arr[i].val;
            //printf("%d %d %d\n",arr[i].from,arr[i].to,arr[i].val);
            asd++;
            p[x]=y;
        }
        if(asd==cnt-1) return ans;
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t;
    cin>>t;
    while(t--)
    {
        char a[51];
        scanf("%d%d",&m,&n);
        gets(a);                   /*这里的操作是最骚的,由于后面有一系列的空格,所以必须用gets吞掉后面的空格,别问我是怎么知道的。。*/
        for(int i=0; i<n; i++)
            gets(pic[i]);
//        for(int i=0; i<n; i++)
//            puts(pic[i]);
        sum=cnt=0;
        memset(ser,0,sizeof(ser));
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                if(pic[i][j]=='A'||pic[i][j]=='S') ser[i][j]=++cnt;      /*给每个点标上号*/
//        for(int i=0; i<n; i++)
//        {
//            for(int j=0; j<m; j++)
//                printf("%d",ser[i][j]);
//            printf("\n");
//        }
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                if(ser[i][j]) bfs(i,j);           /*找出所有的路*/
            }
//        for(int i=0;i<sum;i++)
//            printf("%d %d %d\n",arr[i].from,arr[i].to,arr[i].val);
        sort(arr,arr+sum,cmp);
        printf("%d\n",kruskal());
    }
    return 0;
}
题目还是很有意思的。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值