POJ 3026-Borg Maze(BFS+prim最小生成树)

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14048 Accepted: 4580

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,在S处开始搜索时或者到达某个A时可以分裂成小分队分别搜索,将所有小队的路径加起来即可。
所以可以看成是计算连接所有S/A之间的最短路径。

解题思路:

先将所有S/A字符转换成数字存图便与搜索记录,对每个S/A字符BFS它们之间的最短路,然后用prim的模板求最小生成树。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 0xfffffff
#define MAXN 201
struct Node
{
    int x,y;
};
int col,row,cnt;
int dir[4][2]= {{0,-1},{0,1},{-1,0},{1,0}};//搜索方向
char tma[MAXN][MAXN];
int ma[MAXN][MAXN];
int cost[MAXN][MAXN];
int step[MAXN][MAXN];
int mincost[MAXN];
bool used[MAXN],vis[MAXN][MAXN];
void bfs(int sx,int sy)
{
    queue<Node> q;
    memset(step,0,sizeof(step));
    memset(vis,false,sizeof(vis));//每次搜索都要初始化,WA在这里哭死
    vis[sx][sy]=true;
    Node s;
    s.x=sx,s.y=sy;
    q.push(s);
    while(!q.empty())
    {
        Node temp=q.front();
        q.pop();
        if(ma[temp.x][temp.y]!=-1)//记录最短路
            cost[ma[sx][sy]][ma[temp.x][temp.y]]=cost[ma[temp.x][temp.y]][ma[sx][sy]]=step[temp.x][temp.y];
        for(int i=0; i<4; i++)
        {
            int tx=temp.x+dir[i][0];
            int ty=temp.y+dir[i][1];
            if(tma[tx][ty]=='#'||vis[tx][ty])continue;
            step[tx][ty]=step[temp.x][temp.y]+1;
            s.x=tx,s.y=ty;
            vis[tx][ty]=true;
            q.push(s);
        }
    }
}
void prim()//A/S字符之间的最小生成树
{
    fill(mincost,mincost+cnt,INF);
    fill(used,used+cnt,false);
    mincost[0]=0;
    int res=0;
    while(true)
    {
        int v=-1;
        for(int u=0; u<cnt; ++u)
        {
            //从不属于已加入生成树的顶点中选取从已加入生成树的点到该顶点的权值最小的点
            if(!used[u]&&(v==-1||mincost[u]<mincost[v]))
                v=u;
        }
        if(v==-1) break;
        used[v]=true;
        res+=mincost[v];
        for(int u=0; u<cnt; ++u)
            mincost[u]=min(mincost[u],cost[v][u]);
    }
    cout<<res<<endl;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("G:/cbx/read.txt","r",stdin);
//freopen("G:/cbx/out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>col>>row;
        cnt=0;
        for(int i=0; i<=row; ++i)
            for(int j=0; j<=col; ++j)
                cost[i][j]=INF;
        memset(ma,-1,sizeof(ma));
        char temp[51];
        cin.getline(temp,51);//上一行输入的吃掉回车符
        for(int i=0; i<row; i++)
        {
            cin.getline(tma[i],MAXN);
            for(int j=0; j<col; j++)
                if(tma[i][j]=='A'||tma[i][j]=='S')
                    ma[i][j]=cnt++;//记录A/S字符,0~cnt标号
        }
        for(int i=0; i<row; i++)
            for(int j=0; j<col; j++)
                if(ma[i][j]!=-1)
                    bfs(i,j);//对每个A/S字符搜索其到其它字符的最短路
        prim();
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值