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

Borg Maze

 POJ - 3026

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

题意:将A,S(A与S无差别)用最小的距离连接起来

思路:先用bfs求各个点之间的距离,然后用MST连接

坑点:1:数组要开的大些

            2:输入m,n后面可能会有一串空格,getchar()不好用了

            3:这是我的个人问题,代码里我注释的部分是我的错误思路,这样写会超时

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<utility>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#define maxn 205  //题目数据问题,数组要开的大一些
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define E 1e-8
#define mod 1000000007
#define P pair<int,int>
using namespace std;

int T,n,m,cnt,p,sum;
char maze[maxn][maxn];
char arr[maxn][maxn];
int dis[maxn][maxn];
int pos[maxn][maxn];
int vis[maxn][maxn];
int d[maxn],v[maxn];
int nd[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

struct node1
{
    int x, y;
}flag[1000];

struct node2
{
    int x,y,step;
}nows,nexts;

bool judge(int x,int y)
{
    if(x<1||x>m||y<1||y>n) return false;
    if(maze[x][y] == '#')  return false;
    if(vis[x][y]==1)       return false;
    return true;
}

void bfs(int x,int y)
{
    queue<node2> q;
    while(!q.empty()) q.pop();
    memset(vis,0,sizeof(vis));

    nows.x = x;
    nows.y = y;
    nows.step = 0;
    vis[x][y] = 1;
    q.push(nows);

    while(!q.empty()){
        nows = q.front();
        q.pop();
        if(maze[nows.x][nows.y] == 'A' || maze[nows.x][nows.y] == 'S'){
            dis[pos[x][y]][pos[nows.x][nows.y]] = nows.step;  //这种写法要学会
            dis[pos[nows.x][nows.y]][pos[x][y]] = nows.step;
        }
        for(int i=0;i<4;++i){
            nexts.x = nows.x+nd[i][0];
            nexts.y = nows.y+nd[i][1];
            if(judge(nexts.x,nexts.y)){
                vis[nexts.x][nexts.y] = 1;
                nexts.step = nows.step+1;
                q.push(nexts);
            }
        }
    }
}
/*void dist()  //这个思路是先将所有S和A统计下来,分别对每一个进行bfs,1对k-1,然后每个点都进行一遍
               //比如:求完点1到点2的距离,再求点1到点3...点4...但是这样需要反复入队,每个步骤之间
               //会重复很多,因此时间浪费很多
               //正确的做法是针对点1,看看距离他为1的点有谁,距离为2的有谁,用一个队列把所有的点的
               //距离都求出来,进而节省时间
{
    for(int i=1;i<=k;++i){
        for(int j=i;j<=k;++j){
            if(i == j) continue;
            dis[i][j] = bfs(i,j);
            dis[j][i] = dis[i][j];
     //       printf("dis[%d][%d] = %d\n",i,j,dis[i][j]);
        }
    }
}*/
void prim()
{
    for(int i=1;i<=cnt;++i){
        d[i] = dis[1][i];
        v[i] = 0;
    }
    v[1] = 1;
    for(int i=1;i<=cnt;++i){
        int mi = INF;
        for(int j=1;j<=cnt;++j){
            if(!v[j] && d[j]<mi){
                mi = d[j];
                p = j;
            }
        }
        v[p] = 1;
        for(int j=1;j<=cnt;++j){
            if(!v[j] &&dis[p][j]<d[j])
                d[j] = dis[p][j];
        }
    }
    sum = 0;
    for(int i=2;i<=cnt;++i){
        sum += d[i];
    }
}

int main()
{
    cin>>T;
    while(T--){
        memset(maze,0,sizeof(maze));
        memset(dis,0,sizeof(dis));
        memset(flag,0,sizeof(flag));
        scanf("%d %d\n",&m,&n);  //数据问题:m,n后面可能会出现一串空格
        cnt = 0;
        for(int i=1;i<=n;i++){
            gets(arr[i]);
        }
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j){
                maze[i][j] = arr[i][j-1];
                if(maze[i][j]=='S'||maze[i][j]=='A')
                    pos[i][j] = ++cnt;
            }
        }
        for(int i=1;i<=55;++i){
            for(int j=1;j<=55;++j){
                if(i == j) dis[i][j] = 0;
                else dis[i][j] =INF;
            }
        }
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j){
                if(maze[i][j] == 'S' || maze[i][j] == 'A'){
                   bfs(i,j);
                }
            }
        }
        prim();
        printf("%d\n",sum);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值