Borg Maze--Prim+BFS

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12531 Accepted: 4105

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

题目链接:http://poj.org/problem?id=3026

神坑题!!!!!!!!!!!!!!!!!

强行卡题!!!!!!!!!!!!!!!!!!!

对于这个题我只想说,后台数据太坑了,我还是太年轻,上来我就超时了,然后优化了算法,然后就奇迹般的wa,一开始我以为是我姿势不对,以为我裸敲Prim和BFS出错了,然后就上了板子,然后wa,wa,wa....弄了一晚上,从6.30到1点,然后我就服气了,投降,请教了子祥大大,然后得到的结果是,你代码没错,就是输入长和宽后后台数据可能会有空格,我!@#¥……%&×()…#¥%#


此题可以理解为是从S到所有A的最小生成树,先用BFS求出每两个点的距离(把S当做A),然后就是板子,裸地板子。。


代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
char map1[200][200];
int map2[200][200];
int map3[200][200];
int lowc[200];
int a[200];
int b[200];
int top;
int n,m;
struct node
{
    int x;
    int y;
    int time;
};
int bfs(int i)//最短路
{
    int vis[200][200];
    memset(vis,0,sizeof(vis));
    node t,f;
    t.x=a[i];
    t.y=b[i];
    t.time=0;
    queue<node> q;
    q.push(t);
    vis[t.x][t.y]=1;
    while(!q.empty())
    {
        f=q.front();
        q.pop();
        if(map1[f.x][f.y]!='#'&&map1[f.x][f.y]!=' ')//优化BFS,不要停止循环,直接求出i点到所有点的距离
        {
            int h=map2[f.x][f.y];
            if(f.time<map3[i][h])
                map3[i][h]=map3[h][i]=f.time;
        }
        if(map1[f.x+1][f.y]!='#'&&f.x+1<n&&!vis[f.x+1][f.y])//四个方向
        {
            t.x=f.x+1;
            t.y=f.y;
            t.time=f.time+1;
            q.push(t);
            vis[t.x][t.y]=1;
        }
        if(map1[f.x-1][f.y]!='#'&&f.x-1>=0&&!vis[f.x-1][f.y])
        {
            t.x=f.x-1;
            t.y=f.y;
            t.time=f.time+1;
            q.push(t);
            vis[t.x][t.y]=1;
        }
        if(map1[f.x][f.y+1]!='#'&&f.y+1<m&&!vis[f.x][f.y+1])
        {
            t.x=f.x;
            t.y=f.y+1;
            t.time=f.time+1;
            q.push(t);
            vis[t.x][t.y]=1;
        }
        if(map1[f.x][f.y-1]!='#'&&f.y-1>=0&&!vis[f.x][f.y-1])
        {
            t.x=f.x;
            t.y=f.y-1;
            t.time=f.time+1;
            q.push(t);
            vis[t.x][t.y]=1;
        }
    }
    return 0;
}
int prim()//最小生成树
{
    int i,j;
    int vis[200];
    int res=0;
    memset(vis,0,sizeof(vis));
    for(i=0;i<top;i++)
    {
        lowc[i]=map3[0][i];
    }
    vis[0]=1;
    int minc,p;
    for(i=1;i<top;i++)
    {
        minc=inf;
        p=-1;
        for(j=0;j<top;j++)
        {
            if(lowc[j]<minc&&!vis[j])
            {
                minc=lowc[j];
                p=j;
            }
        }
        if(minc==inf)
            return -1;
        res+=minc;
        vis[p]=1;
        for(j=0;j<top;j++)
        {
            if(lowc[j]>map3[p][j]&&!vis[j])
            {
                lowc[j]=map3[p][j];
            }
        }
    }
    return res;
}
int main()
{
    int t;
    scanf("%d",&t);
    int i,j;
    while(t--)
    {
        memset(map3,inf,sizeof(map3));
        top=0;
        scanf("%d%d\n",&m,&n);//坑坑坑!!scanf("%d%d%*c",&m,&n);是过不去的!!!!!!!!
        for(i=0;i<n;i++)
        {
            gets(map1[i]);
            for(j=0;j<m;j++)
            {
                if(map1[i][j]=='A')
                {
                    map2[i][j]=top;
                    a[top]=i;
                    b[top++]=j;
                }
                if(map1[i][j]=='S')
                {
                    map2[i][j]=top;
                    a[top]=i;
                    b[top++]=j;
                }
            }
        }
        for(i=0;i<top;i++)
        {
            bfs(i);//以所有的顶点为起点,求一张完全图
        }
        int h=prim();
        cout<<h<<endl;
    }
    return 0;
}

POJ就是有些神题啊


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值