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

Source

代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int map[300][300],dis[300],vis[300];
char str[300][300];
int point[300][300];
int tvis[300][300],tdis[300][300];
struct node{
int x,y;
};
int m,n,ans;
int tnext[4][2]={1,0,0,1,-1,0,0,-1};
void bfs(int tx,int ty){
     queue<node>q;
     node next,res;
     memset(tvis,0,sizeof(tvis));
     memset(tdis,0,sizeof(tdis));
     tvis[tx][ty]=1;
     res.x=tx;
     res.y=ty;
     q.push(res);
     while(!q.empty()){
         res=q.front();
         q.pop();
         if(point[res.x][res.y]){
           map[point[tx][ty]][point[res.x][res.y]]=tdis[res.x][res.y];
         }
         int xx,yy;
         for(int k=0;k<4;k++){
             next.x=xx=res.x+tnext[k][0];
             next.y=yy=res.y+tnext[k][1];
             if(xx>=1&&xx<=m&&yy>=1&&yy<=n&&!tvis[xx][yy]&&str[xx][yy]!='#'){
                 tvis[xx][yy]=1;
                 tdis[xx][yy]=tdis[res.x][res.y]+1;
                 q.push(next);
             }
         }

}
}

int prim(int u){
    int sum=0;
    for(int i=1;i<=ans;i++){
        dis[i]=map[u][i];
    }
    vis[u]=1;
    for(int ti=2;ti<=ans;ti++){
    int tmin=2000000000;
    int k;
       for(int i=1;i<=ans;i++){
          if(dis[i]<tmin&&!vis[i]){
              tmin=dis[i];
              k=i;
          }
       }
       sum+=tmin;
       vis[k]=1;
       for(int j=1;j<=ans;j++){
          if(dis[j]>map[k][j]&&!vis[j])
          dis[j]=map[k][j];
       }
    }
    return sum;
}

int main(){
   int t;
   scanf("%d",&t);
   while(t--){
       memset(point,0,sizeof(point));
       memset(map,0,sizeof(map));
       memset(dis,0,sizeof(dis));
       memset(vis,0,sizeof(vis));
       memset(str,0,sizeof(str));
      scanf("%d%d",&n,&m);
      gets(str[0]);
       ans=0;
      for(int i=1;i<=m;i++){
          gets(str[i]+1);
         for(int j=1;j<=n;j++){
             if(str[i][j]=='S'||str[i][j]=='A')
             point[i][j]=++ans;
         }

}

for(int i=1;i<=m;i++){
         for(int j=1;j<=n;j++){
            if(point[i][j])
            bfs(i,j);
         }
      }
      printf("%d\n",prim(1));
   }
   return 0;
}

poj3026(bfs+prim)的更多相关文章

  1. poj3026(bfs+prim)最小生成树

    The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...

  2. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  3. POJ 3026(BFS+prim)

    http://poj.org/problem?id=3026 题意:任意两个字母可以连线,求把所有字母串联起来和最小. 很明显这就是一个最小生成树,不过这个题有毒.他的输入有问题.在输入m和N后面,可 ...

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

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

  5. POJ3026(BFS + prim)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10554   Accepted: 3501 Descri ...

  6. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  7. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

  8. [CSP-S模拟测试]:Star Way To Heaven(最小生成树Prim)

    题目描述 小$w$伤心的走上了$Star\ way\ to\ heaven$. 到天堂的道路是一个笛卡尔坐标系上一个$n\times m$的长方形通道(顶点在$(0,0)$和$(n,m)$),小$w$ ...

  9. HDU 1026 Ignatius and the Princess I(BFS+优先队列)

    Ignatius and the Princess I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

随机推荐

  1. cryptdb中wrapper.lua的分析

    因为cryptDB是在mysql-proxy的基础上来实现了,可以看成是为mysql-proxy添加了新的.为mysql-proxy已经为开发人员提供了相应的接口.如果开发人员只需要通过lua脚本语言 ...

  2. JavaScript表单处理(上)

    为了分担服务器处理表单的压力,JavaScript提供了一些解决方案,从而大大打破了处处依赖服务器的局面.  发文不易,转载请亲注明出处,谢谢! 一.表单介绍 在HTML中,表单是由<form& ...

  3. IOS 遍历未知对象的属性和方法

    /* 注意:要先导入ObjectC运行时头文件,以便调用runtime中的方法*/ #import <objc/runtime.h> @implementation NSObject (P ...

  4. 每天一个linux命令(19):Linux 目录结构

    对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...

  5. iOS关于rar解压第三方库Unrar4iOS使用总结

    作者最近的公司项目要做实现rar解压的功能,在网上找了很久貌似关于rar解压的资料很少,不过有很多人推荐一个名叫“Unrar4iOS”的第三方开源框架,于是下载并尝试使用发现该开源框架并在使用过程中发 ...

  6. opencv笔记2:图像ROI

    time:2015年 10月 03日 星期六 12:03:45 CST # opencv笔记2:图像ROI ROI ROI意思是Region Of Interests,感兴趣区域,是一个图中的一个子区 ...

  7. BZOJ-2038 小Z的袜子(hose) 莫队算法

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MB Submit: 5573 Solved: 2568 [Subm ...

  8. Redis Installation、Configuration、Program Based On Redis Learning

    目录 . Redis 简介 . Redis安装配置 . 编程使用Redis . 使用Lua脚本 1. Redis 简介 0x1: Redis是什么 Redis是一款Nosql类型的基于key-valu ...

  9. MySql与Java的时间类型

    MySql的时间类型有          Java中与之对应的时间类型date                                           java.sql.DateDatet ...

  10. 在网络7层协议中,如果想使用UDP协议达到TCP协议的效果,可以在哪层做文章?(QQ 为什么采用 UDP 协议,而不采用 TCP 协议实现?)

    为了解决这题,可以具体看看下面这个讨论. 解灵运工程师 185 人赞同 某次架构师大会上那个58同城做即时通信的人说:原因是因为当时没有epoll这种可以支持成千上万tcp并发连接的技术,所以他们使用 ...