POJ 3026 Borg Maze(BFS+Prim)

题目网址:http://poj.org/problem?id=3026

题目:

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14805   Accepted: 4797

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

思路:

无非就是求最小生成树,不同点是放进了坐标里,不能求直接的距离。要先用BFS求出每两个点之间的距离,再用prim。这道题有点坑的是,数组要开的够大,数据的x,y超过50了。

代码:

 1 #include <cstdio>
 2 #include <queue>
 3 #include <cstring>
 4 using namespace std;
 5 const int N=300;
 6 const int inf=1111111;
 7 int n,m,cnt;
 8 char maze[N][N];
 9 int vis[N][N],exist[N];
10 int mp[N][N];
11 int dist[N],rd[N][N];
12 struct node{
13     int x,y;
14 }id[300],dir[4]={{-1,0},{1,0},{0,1},{0,-1}};
15 void bfs(int v){
16     node now=id[v],next;
17     queue<node>q;
18     q.push(now);
19     vis[now.x][now.y]=1;
20     while (!q.empty()) {
21         now=q.front();q.pop();
22         if (maze[now.x][now.y]=='A' || maze[now.x][now.y]=='S') {
23             int x=mp[now.x][now.y];
24             rd[v][x]=vis[now.x][now.y]-1;
25         }
26         for (int d=0; d<4; d++) {
27             next.x=now.x+dir[d].x;
28             next.y=now.y+dir[d].y;
29             if(next.x<0 || next.x>=n || next.y<0 || next.y>=m || vis[next.x][next.y] || maze[next.x][next.y]=='#')  continue;
30             vis[next.x][next.y]=vis[now.x][now.y]+1;
31             q.push(next);
32         }
33     }
34 }
35 int prim(){
36     int res=0;
37     for(int i=2;i<=cnt;i++) dist[i]=inf;
38     dist[1]=0;
39     memset(exist, 0, sizeof(exist));
40     for(int i=1;i<=cnt;i++){
41         int Min=inf,u;
42         for (int j=1; j<=cnt; j++){
43             if(Min>dist[j] && !exist[j]){
44                 Min=dist[j];
45                 u=j;
46             }
47         }
48         exist[u]=1;
49         res+=Min;
50         for (int j=1; j<=cnt; j++) {
51             if(dist[j]>rd[u][j]){
52                 dist[j]=rd[u][j];
53             }
54         }
55     }
56     return res;
57 }
58 int main(){
59     int t;
60     scanf("%d",&t);
61     while (t--) {
62         cnt=0;
63         scanf("%d%d ",&m,&n);
64         for (int i=0; i<n; i++) {
65             gets(maze[i]);
66             for (int j=0; j<m; j++) {
67                 if(maze[i][j]=='A' || maze[i][j]=='S'){
68                     mp[i][j]=++cnt;//将每个字母进行编号,方便后面的操作
69                     id[cnt].x=i;id[cnt].y=j;//以编号为索引,坐标为内容
70                 }
71             }
72         }
73         for (int i=1; i<=cnt; i++) {
74             for (int j=1; j<=cnt; j++) {
75                 rd[i][j]=inf;//初始化每个编号之间的距离为“无穷大”
76             }
77         }
78         for(int i=1;i<=cnt;i++){
79             memset(vis, 0, sizeof(vis));
80             bfs(i);//求出每两个编号之间的距离
81         }
82         printf("%d\n",prim());
83     }
84     return 0;
85 }

 

posted @ 2017-08-03 09:49  ventricle  阅读(141)  评论(0编辑  收藏  举报