Borg Maze - poj 3026(BFS + Kruskal 算法)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9821   Accepted: 3283

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
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<queue>
  4 #include<algorithm>
  5 #include <iostream>
  6 using namespace std;
  7 int map[102][102];
  8 int vis[102][102];
  9 int parent[102];
 10 int row, col;
 11 int edge_num = 0;
 12 int point_num=0;
 13 struct edge {
 14     int u, v, w;
 15 } e[102 * 102];
 16 struct points {
 17     int u, v, d;
 18 } point;
 19 bool cmp(const edge e1,const edge e2){
 20     return e1.w<e2.w;
 21 }
 22 int Kruskal(){
 23     int mindis=0;
 24     for(int i=1;i<=point_num;i++){
 25         parent[i]=i;
 26     }
 27     sort(e,e+edge_num,cmp);
 28     int pnum=0;
 29     for(int i=0;i<edge_num&&pnum<point_num;i++){
 30         int k,g;
 31         int u=e[i].u;
 32         int v=e[i].v;
 33         for(k=parent[u];parent[k]!=k;k=parent[parent[k]]);
 34         for(g=parent[v];parent[g]!=g;g=parent[parent[g]]);
 35
 36         if(k!=g){
 37             parent[g]=k;
 38             mindis+=e[i].w;
 39             pnum++;
 40         }
 41         }
 42     return mindis;
 43 }
 44 void BFS(int x, int y) {
 45     queue<points> p;
 46     points s;
 47     s.u = x;
 48     s.v = y;
 49     s.d = 0;
 50     p.push(s);
 51     memset(vis, 0, 102 * 102 * sizeof(int));
 52     vis[x][y] = 1;
 53
 54     while (!p.empty()) {
 55         points tmp = p.front();
 56         p.pop();
 57         for (int i = -1; i < 2; i += 2) {
 58             points next;
 59             next.u = tmp.u + i;
 60             next.v = tmp.v;
 61             next.d = tmp.d + 1;
 62             if (next.u >= 0 && next.u < row) {
 63                 if (!vis[next.u][next.v]) {
 64                     int pos = map[next.u][next.v];
 65                     vis[next.u][next.v] = 1;
 66                     if (pos >= 0)
 67                         p.push(next);
 68                     if (pos >= 1) {
 69                         e[edge_num].u = map[x][y];
 70                         e[edge_num].v = pos;
 71                         e[edge_num].w = next.d;
 72                         edge_num++;
 73                     }
 74                 }
 75             }
 76         }
 77         for (int i = -1; i < 2; i += 2) {
 78             points next;
 79             next.u = tmp.u;
 80             next.v = tmp.v + i;
 81             next.d = tmp.d + 1;
 82             if (next.v >= 0 && next.v < col) {
 83                 if (!vis[next.u][next.v]) {
 84                     int pos = map[next.u][next.v];
 85                     vis[next.u][next.v] = 1;
 86                     if (pos >= 0)
 87                         p.push(next);
 88                     if (pos >= 1) {
 89                         e[edge_num].u = map[x][y];
 90                         e[edge_num].v = pos;
 91                         e[edge_num].w = next.d;
 92                         edge_num++;
 93                     }
 94                 }
 95             }
 96         }
 97
 98     }
 99 }
100 int main() {
101     int n;
102     scanf("%d", &n);
103     char tmp[102];
104     for (int i = 0; i < n; i++) {
105         point_num=0;
106         edge_num=0;
107         scanf("%d %d", &col, &row);
108         gets(tmp); //坑【一串空格】
109         for (int j = 0; j < row; j++) {
110             for (int k = 0; k < col; k++) {
111                 char c;
112                 scanf("%c", &c);
113                 if (c == ‘ ‘)
114                     map[j][k] = 0;
115                 else if (c == ‘#‘)
116                     map[j][k] = -1;
117                 else if (c == ‘\n‘)
118                     k--;
119                 else
120                     map[j][k] = ++point_num;
121             }
122
123         }
124         for (int j = 0; j < row; j++) {
125             for (int k = 0; k < col; k++) {
126                 if (map[j][k] > 0) {
127                     BFS(j, k);
128                 }
129             }
130         }
131         int mid=Kruskal();
132         printf("%d\n",mid);
133     }
134     return 0;
135 }
时间: 2024-08-30 04:10:55

Borg Maze - poj 3026(BFS + Kruskal 算法)的相关文章

J - Borg Maze - poj 3026(BFS+prim)

在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, ********************************************************************************** #include<algorithm>#include<stdio.h>#include<string.h>#include<que

Borg Maze POJ - 3026

题目链接:https://vjudge.net/problem/POJ-3026 思路: 题目说建立一个通道网络,使得‘S’能到达其他所有'A',且所有通道长度相加最短,可以看出是一个最小生成树,就是建图比较麻烦. 用bfs建图,跑出每个‘S’或‘A’到其他‘S’或‘A’的距离,然后只需要套上最小生成树的板子就OK. 1 #include <iostream> 2 #include <cstring> 3 #include<vector> 4 #include<s

POJ - 3026(BFS+最小生成树.krustal)

题目: 题目链接: http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17462   Accepted: 5631 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The

poj 3026(BFS+最小生成树)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12032   Accepted: 3932 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 desc

POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14021   Accepted: 5484   Special Judge Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the c

POJ 3026 Borg Maze(Prim+bfs求各点间距离)

题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’  ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度. 解题思路:相当于所有的字母A和S都是结点,求连接这些结点的最小距离和,是最小生成树的题目.先用BFS求各点间的距离,然后再用Prim(Kruskal也可以)求出最小距离就可以了. 注意:输完行列m和n之后,后面有一堆空格,要用gets()去掉,题目

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

题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显的最小生成树,点与点的距离要用bfs或者dfs求一下.这题bfs要稍微优化一下.不能下暴力的bfs就是两点两点之间 bfs这样会有很多重复的查询会超时,所以要一次性的bfs找到一个点就bfs到底. #include <iostream> #include <algorithm> #in

POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)

( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<queue> using namespace std; const int INF=10e8; const int MAXN=55; int col,row,k,minn; char str[MAXN][MAXN]

J - Borg Maze

J - Borg Maze 思路:bfs+最小生成树. #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 110 using namespace std; int fa[MAXN]; struct nond{ int x,y,z; }v[MAXN*MAXN]; struct none{ int