poj3026

Borg Maze

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12952 Accepted: 4227

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

题目大意:从S点出发,把图中所有A字符连通的最短路径

思路:因为连通所有字符,想到用Prim算法,构造最小生成树,但是我们需要各个点的距离关系
所以再用bfs求各个点的之间的距离。注意的是不要一个一个的求,否则很可能会超时,把一个点
到其他所有点的距离一次求完,也就是每一次都遍历整个图

代码如下:
  1 #include <iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<queue>
  5 using namespace std;
  6 const int INF = 0x3f3f3f3f;
  7 const int maxs = 305;
  8 char a[maxs][maxs];
  9 struct Point
 10 {
 11     int col;
 12     int row;
 13     int step;
 14 }node[maxs];
 15 int col,row,nums;//nums需要被连通的所有点的个数
 16 int edge[maxs][maxs];
 17 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上下左右
 18 bool judge(Point point)
 19 {
 20     if(point.col>0&&point.col<=col&&point.row>0&&point.row<=row&&a[point.row][point.col]!='#')
 21         return true;
 22     return false;
 23 }
 24 
 25 void bfs(int i)
 26 {
 27     bool vis2[maxs][maxs];
 28     int dist[maxs][maxs];//用来打表
 29     memset(vis2,false,sizeof(vis2));
 30     queue<Point> q;
 31     node[i].step=0;
 32     q.push(node[i]);
 33     vis2[node[i].row][node[i].col]=true;
 34     Point cur,next;
 35     while(!q.empty())
 36     {
 37         cur = q.front();
 38         q.pop();
 39         for(int k=0;k<4;k++)
 40         {
 41             next.row=cur.row+dir[k][0];
 42             next.col = cur.col+dir[k][1];
 43             if(!vis2[next.row][next.col]&&judge(next))
 44             {
 45                 next.step=cur.step+1;
 46                 vis2[next.row][next.col]=true;
 47                 q.push(next);
 48                 if(a[next.row][next.col]=='A')
 49                     dist[next.row][next.col]=next.step;
 50             }
 51         }
 52     }
 53     for(int j=2;j<=nums;j++)
 54     {
 55         int d = dist[node[j].row][node[j].col];
 56         edge[i][j]=d;
 57         edge[j][i]=d;
 58     }
 59 }
 60 int prim()
 61 {
 62     bool vis[maxs];
 63     memset(vis,false,sizeof(vis));
 64     vis[1]=true;
 65     int dist[maxs],ans=0;
 66     for(int i=1;i<=nums;i++)
 67         dist[i]=edge[1][i];
 68     for(int i=2;i<=nums;i++)
 69     {
 70         int mins = INF,k=1;
 71         for(int j=1;j<=nums;j++)
 72             if(!vis[j]&&dist[j]<mins)
 73             {
 74                 mins = dist[j];
 75                 k=j;
 76             }
 77         if(mins!=INF)
 78             ans+=mins;
 79         vis[k]=true;
 80         for(int j=1;j<=nums;j++)
 81             if(!vis[j]&&dist[j]>edge[k][j])
 82                 dist[j]=edge[k][j];
 83     }
 84     return ans;
 85 }
 86 int main()
 87 {
 88     freopen("in.txt","r",stdin);
 89     int T;
 90     scanf("%d",&T);
 91     while(T--)
 92     {
 93         nums=1;
 94         memset(node,0,sizeof(node));
 95         memset(a,0,sizeof(a));
 96         scanf("%d%d",&col,&row);
 97         char s[2];
 98         for(int i=1;i<=row;i++)
 99         {
100             gets(s);
101             for(int j=1;j<=col;j++)
102             {
103                 scanf("%c",&a[i][j]);
104                 if(a[i][j]=='S')
105                 {
106                     node[1].row=i;node[1].col=j;
107                 }
108                 else if(a[i][j]=='A')
109                 {
110                     node[++nums].row=i;node[nums].col=j;
111                 }
112             }
113         }
114         for(int i=1;i<=nums;i++)
115         {
116             edge[i][i]=0;
117             bfs(i);
118         }
119         printf("%d\n",prim());
120     }
121     return 0;
122 }

 

 

转载于:https://www.cnblogs.com/wt20/p/5748846.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值