poj 3026 Borg Maze(bfs+prim)

Borg Maze

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10810   Accepted: 3574

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

题目大意:输入x,y,表示y行x列。字符为空格时,表示可以移动,为‘#’时,表示墙壁,不可穿越,当为‘A’,‘S’时,表示节点,‘S’为起点。        求所有点连通时,权值最小为多少

  1 /*注意:G++递交AC,C++递交编译错误  Compile Error*/
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <queue>
  7 using namespace std;
  8 const int MAX=150;
  9 const int INF=0xfffffff;
 10 char g[MAX][MAX];  //存放地图
 11 int egde[MAX][MAX]; //字母间的最短距离
 12 int vis[MAX][MAX],num[MAX][MAX];//vis广搜用的标记数组,num给字母编号
 13 int dis[MAX][MAX];
 14 int t,x,y,cnt,nc;
 15 int dirx[]= {0,0,1,-1}; //四个方向的坐标
 16 int diry[]= {1,-1,0,0};
 17 struct Pos
 18 {
 19     int x;
 20     int y;
 21 };
 22 void fun()  //给字母编号
 23 {
 24     nc=0;  // 字母个数
 25     memset(num,-1,sizeof(num));
 26     for(int i=0; i<y; i++)
 27     {
 28         for(int j=0; j<x; j++)
 29         {
 30             if(g[i][j]==‘A‘||g[i][j]==‘S‘)
 31                 num[i][j]=nc++;
 32             if(g[i][j]==‘S‘)  //起点的编号
 33                 cnt=nc-1;
 34         }
 35     }
 36 }
 37 void bfs(int cx,int cy)  // 广搜确定最短距离
 38 {
 39     queue<Pos>Q;
 40     memset(vis,0,sizeof(vis));
 41     vis[cx][cy]=1;
 42     dis[cx][cy]=0;
 43     Q.push((Pos)
 44     {
 45         cx,cy
 46     });
 47     while(!Q.empty())
 48     {
 49         Pos e=Q.front();
 50         Q.pop();
 51         int x=e.x,y=e.y;
 52         if(num[x][y]!=-1)  //为-1时,即(x,y)为字母
 53             egde[num[cx][cy]][num[x][y]]=dis[x][y];
 54         for(int d=0; d<4; d++)
 55         {
 56             int sx=x+dirx[d];
 57             int sy=y+diry[d];
 58             if(sx<0&&sy<0&&sx>=y&&sy>=x)
 59                 continue;
 60             if(vis[sx][sy]||g[sx][sy]==‘#‘)
 61                 continue;
 62             vis[sx][sy]=1;
 63             dis[sx][sy]=dis[x][y]+1;
 64             Q.push((Pos)
 65             {
 66                 sx,sy
 67             });
 68         }
 69     }
 70 }
 71 int prim(int start) //套prim模板
 72 {
 73     int i,j,k,Min,ans=0;
 74     int d[MAX];
 75     bool visit[MAX];
 76     memset(visit,false,sizeof(visit));
 77     for(i=0; i<nc; i++)
 78         d[i]=egde[start][i];
 79     visit[start]=1;
 80     d[start]=0;
 81     for(i=0; i<nc-1; i++)
 82     {
 83         Min=INF,k=-1;
 84         for(j=0; j<nc; j++)
 85         {
 86             if(!visit[j]&&d[j]<Min)
 87             {
 88                 Min=d[j];
 89                 k=j;
 90             }
 91         }
 92         ans+=Min;
 93         visit[k]=1;
 94         for(j=0; j<nc; j++)
 95         {
 96             if(!visit[j]&&d[j]>egde[k][j])
 97                 d[j]=egde[k][j];
 98         }
 99     }
100     return ans;
101 }
102 int main()
103 {
104     cin>>t;
105     char s[500];
106     while(t--)
107     {
108         scanf("%d%d",&x,&y);
109         gets(s);  //注意:或许有无数空格
110         for(int i=0; i<y; i++)
111             gets(g[i]);
112         fun();
113         for(int i=0; i<y; i++)
114         {
115             for(int j=0; j<x; j++)
116             {
117                 if(g[i][j]==‘A‘||g[i][j]==‘S‘)
118                     bfs(i,j);
119             }
120         }
121         printf("%d\n",prim(cnt));
122     }
123     return 0;
124 }

时间: 2024-10-13 12:43:57

poj 3026 Borg Maze(bfs+prim)的相关文章

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

链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走),空格代表空位(可走),S代表搜索起点(可走) A代表外星人站(可走),现在要从S出发,将S和所有的A之间都连通,求路线总距离最小值 分析:可以先用bfs将所有的A,S两两之间的最短距离,题目的目的是将S与所有的A连通,使得总距离最小, 所有任选一点开始按最小生成树的算法做就行,并非非要从S点开始 注:题目输入x,y后可能有很多空格,可以用gets将多余的空格取走,开数组是尽量开大点,之前虽然开的比题目数据     稍大,但一

POJ - 3026 Borg Maze BFS加最小生成树

Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算读懂了题目,也比较难想到这用到了最小生成树的知识,因为可以分身,所以每个点可以向其他点都连上边.可以用bfs预处理出所有的S点,A点的连线,再跑一遍最小生成树,即可得出答案.这里有几点注意,一开始我bfs没有记录step,而是直接找到一点后算曼哈顿距离,这是不对的,因为可能是绕了一个圈到了这个点.还

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(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]

POJ 3026 Borg Maze(bfs + prime)

解题思路: 先用BFS预处理出每个字母节点到其它节点的最短路径,然后套用prime算法. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <queue> #include <stack> #include <vector> #include <

POJ 3026 Borg Maze &amp; UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树)

http://poj.org/problem?id=3026 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1248 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8498   Accepted: 2862 Description The Bor

POJ 3026 Borg Maze

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7998   Accepted: 2675 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 descr

poj 3026 Borg Maze 最小生成树+bfs prim算法

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 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 descr

POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)

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