POJ3026 Borg Maze(Prim)(BFS)

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12729   Accepted: 4153

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
【题意】在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度。
【分析】一开始没懂题意,后来懂了,就是简单的BFS+Prim。但是WA了好几发,伤心至极,去看看Discuss。发现好多人都被坑了,输入法人x,y后面还有好多空格,必须提前gets掉。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=1005;
const int M=15005;
int n,m,t,kk,ii,cnt,edg[N][N],lowcost[N];
int d[4][2]= {0,1,1,0,-1,0,0,-1};
int vis[N][N];
int num[N][N];
char w[N][N];
struct man {
    int x,y,step;
};
void bfs(man s) {
    queue<man>q;
    while(!q.empty())q.pop();
    q.push(s);
    vis[s.x][s.y]=1;
    while(!q.empty()) {
        man t=q.front();
        q.pop();
        if(w[t.x][t.y]=='A'||w[t.x][t.y]=='S') {
            int u=num[s.x][s.y];
            int v=num[t.x][t.y];
            edg[u][v]=edg[v][u]=t.step;
        }
        for(int l=0; l<4; l++) {
            int xx=t.x+d[l][0],yy=t.y+d[l][1];
            if(xx>=0&&xx<n&&yy>=0&&yy<m&&vis[xx][yy]==0&&w[xx][yy]!='#') {
                man k;
                k.x=xx,k.y=yy;
                k.step=t.step+1;
                q.push(k);
                vis[xx][yy]=1;
            }
        }
    }
}
void Prim() {
    for(int i=0; i<cnt; i++) {
        lowcost[i]=edg[ii][i];
    }
    lowcost[ii]=-1;
    int sum=0;
    for(int i=1; i<cnt; i++) {
        int minn=inf;
        for(int j=0; j<cnt; j++) {
            if(lowcost[j]!=-1&&lowcost[j]<minn) {
                minn=lowcost[j];
                kk=j;
            }
        }
        sum+=minn;
        lowcost[kk]=-1;
        for(int j=0; j<cnt; j++) {
            if(edg[j][kk]<lowcost[j]) {
                lowcost[j]=edg[j][kk];
            }
        }
    }
    printf("%d\n",sum);
}
int main() {
    scanf("%d",&t);
    while(t--) {
        memset(edg,0,sizeof(edg));
        memset(lowcost,0,sizeof(lowcost));
        scanf("%d%d",&m,&n);
        char tmp[N];gets(tmp);//一定要有这个,这是这个题目最坑的地方
        for(int i=0; i<n; i++) {
            gets(w[i]);
        }
        cnt=0;
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if(w[i][j]=='A'||w[i][j]=='S') {
                    num[i][j]=cnt++;
                    if(w[i][j]=='S') {
                        ii=cnt-1;
                    }
                }
            }
        }
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if(w[i][j]=='A'||w[i][j]=='S') {
                    man s;
                    s.x=i;
                    s.y=j;
                    s.step=0;
                    memset(vis,0,sizeof(vis));
                    bfs(s);
                }
            }
        }
        Prim();
    }
    return 0;
}
View Code

 

posted @ 2016-08-03 13:01  贱人方  阅读(284)  评论(0编辑  收藏  举报