# Borg Maze  POJ - 3026（bfs+最小生成树）

## 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

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;
}

08-13 586

07-30 6433

08-21 319

02-20 21

05-24 18

07-25 714

10-18 992

07-31 1617

08-05 242

10-01 116

• 写评论