poj3026(bfs+prim)

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;
}

时间: 2024-10-20 19:03:28

poj3026(bfs+prim)的相关文章

POJ 3026(BFS+prim)

http://poj.org/problem?id=3026 题意:任意两个字母可以连线,求把所有字母串联起来和最小. 很明显这就是一个最小生成树,不过这个题有毒.他的输入有问题.在输入m和N后面,可能有一大串的空格.就因为这个,我RE都有点懵了,要不是discuss里面有人说输入有问题,我都没注意到这个,原本只用了一个getchar吃掉最后的换行符.没想到之后还有空格.有点小坑. 思路:这个题目如果他给你一个图,那就是最裸的prim了.不过这个题的难点也就是在他给的图你不能用Prim,你只能通

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

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

POJ3026(BFS + prim)

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

hdu 3345 War Chess (bfs+优先队列)

War Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1732    Accepted Submission(s): 416 Problem Description War chess is hh's favorite game: In this game, there is an N * M battle map, an

POJ 3278 Catch That Cow(BFS 剪枝)

题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出来.0 0 先说说这道题目,大意是有个农夫要抓牛,已知牛的坐标,和农夫位置.而且农夫有三种移动方式,X + 1,X - 1,X * 2,问最少几步抓到牛. 开始认为很简单的,三方向的BFS就能顺利解决,然后在忘开标记的情况下直接广搜,果然TLE,在你计算出最少位置之前,牛早跑了. 然后反应过来开标记

POJ 2485-Highways(最小生成树prim)

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22433   Accepted: 10341 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Fl

SPOJ 206 BITMAP(BFS+剪枝)

SPOJ 206 BITMAP(BFS+剪枝) ACM 题目地址:SPOJ 206 BITMAP 题意: 给出一个矩阵,有黑白点,计算每个点离最近的白点的距离,p1=(i1,j1) and p2=(i2,j2),距离d(p1,p2)=|i1-i2|+|j1-j2|. 分析: 有剪枝的BFS,如果从黑色的开始进行BFS最近的白色,复杂度是O(n^4),复杂度无法接受. 于是想把黑色白色分开记录下来,然后两遍for,发现复杂度不变... 从黑色开始搜,如果要记忆化的话,貌似很麻烦,但是我们可以从白色

hdu:4771Stealing Harry Potter&#39;s Precious(bfs + 全排列)

题目:hdu:4771Stealing Harry Potter's Precious 题目大意:给出n* m的矩阵,代表n * m间room,然后每个房间又有脆弱和坚固之分,分别用'.'和'#'代替.'@'代表Dudely所在的起点. 题目说Dudely想要偷Harry的宝物,他知道宝物的位置,但是他的魔法只能穿过脆弱的room.愚蠢的他又想偷完harry所有的宝物,并不在乎如何人出去.问最短移动的步数偷完所有的宝物.没法偷完所有的宝物就输出-1. 解题思路:这里要求的是最短路径问题.可以将各

poj 1258 Agri-Net (最小生成树 prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39499   Accepted: 16017 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee