poj-3026 Borg Maze

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的最小总距离 <重复的路径不计算>

思路:<来源于 Jun博客>

将 所有A和S 看做 节点, 则可以构成一个 包含N个节点的无向完全连通图, 权值 为 字母到其他字母的 距离;BFS求出字母到字母的距离;

则 问题可看做 求 改图的最小生成树;利用Prim求解即可;

代码::

/**** 我认为这道题难想也是难理解的就是建距离图那一块 在这个代码中 自己设置点 即mp[][]=dot 在计算距离的时候 是计算点i到dot的距离 */ #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; #define INF 99999999 char str[300][300]; int mp[300][300],dot,dx[]= {-1,0,0,1},dy[]= {0,-1,1,0},n,m;///cnt用于BFS中的结束队列  int dis[300][300],vist[300][300]; struct node {     int x,y,step; } s[500]; int panduan(int a,int b) {     if( a<1 || a>n || b<1 || b>m || vist[a][b]==1 || mp[a][b]==-1 )         return 0;     return 1; } int bfs()///BFS查找与建立距离图 <注意看建距离图怎么建的,> {     node next,temp;     int x,y,i,j;     queue<node>q;     memset(dis,0,sizeof(dis));///计算结构体里面字符之间的距离     for(i=1; i<=dot; i++)///计算每个点到其他点的距离     {         while(!q.empty())             q.pop();         memset(vist,0,sizeof(vist));///bfs里面的标记数组         s[i].step = 0;         vist[s[i].x][s[i].y]=1;///表示以s[i].x为横坐标,s[i].y为纵坐标的点已经被搜索到         q.push(s[i]);         while(!q.empty())         {             temp=q.front();             q.pop();             x=temp.x;             y=temp.y;             if(mp[x][y]!=0 && mp[x][y]!=-1)///表示这个点数是A 或者 S             {                // cnt++;                 dis[i][mp[x][y]]=dis[mp[x][y]][i]=temp.step;                 /*if( cnt==dot)                     break;*/             }             for(j=0; j<4; j++)///四个方向             {                 int xx=temp.x+dx[j];                 int yy=temp.y+dy[j];                 if(panduan(xx,yy))                 {                     next.x=xx;                     next.y=yy;                     next.step=temp.step+1;                     vist[xx][yy]=1;                     q.push(next);                 }             }         }     } } void prim() {     int low[300],k,i,j,min1;     int vis[300],sum;     memset(vis,0,sizeof(vis));     for(i=1; i<=dot; i++)     {         low[i]= INF;///注意初始化     }     sum=0;     low[1]=0;///注意初始化    // vis[1]=1;     for(i=1; i<=dot; i++)     {         min1=INF;         for(j=1; j<=dot; j++)         {             if(low[j]<min1 && vis[j]==0)             {                 min1=low[j];                 k=j;             }         }         sum+=min1;         vis[k]=1;         for(j=1; j<=dot; j++)         {             if(vis[j]==0&&low[j]>dis[k][j])             {                 low[j]=dis[k][j];             }         }     }     printf("%d\n",sum); } int main() {     int t,i,j;     char c[60];     scanf("%d",&t);     while(t--)     {         dot=0;         scanf("%d%d",&n,&m);         gets(c);         for(i=1; i<=n; i++)         {             gets(str[i]);             for(j=1; j<=m; j++)             {                 if(str[i][j]=='A'||str[i][j]=='S')///把含有A S字符的存进结构体                 {                     dot++;                     mp[i][j]=dot;     ///方便bfs里面建距离图 mp[i][j]为距离图的横坐标                     s[dot].x=i;                     s[dot].y=j;                 }                 else if(str[i][j]==' ')                 {                     mp[i][j]=0;                 }                 else if(str[i][j]=='#')                 {                     mp[i][j]=-1;                 }             }         }         bfs();         prim();     }     return 0; }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值