http://poj.org/problem?id=3026

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12086   Accepted: 3953

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算出每个A或者S到其他A或者S的距离,建出图后用Prim算法直接做就可以了。(有一个坑点就是题目的Input貌似有点错误,输入完两个整数后需要再输入一个串再输入图。我也是看了Discuss才知道,还有个奇奇怪怪的地方,有人知道什么回事请告诉下我,谢谢)。

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 210
#define INF 1000000000
struct node
{
int x,y;
};
//node alien[210]; //这句是我之前写留下来的,可是不知道为什么删了之后就WA了,如果有人知道请务必告诉我(现在发现原来交C++就会错,交G++就不会了,不知道什么情况呀)
char maze[][];
int mp[][],low[],used[],vis[][],d[][];
int n,m,dx[]={,-,,},dy[]={,,,-},cnt; bool check(int x,int y)
{
if(d[x][y]==-&&maze[x][y]!='#'&&<=x&&x<=m&&<=y&&y<=n) return true;
else return false;
} void bfs(int sx,int sy)
{
queue<node> que;
while(!que.empty()) que.pop();
memset(d,-,sizeof(d));
node a;
a.x=sx;a.y=sy;
d[a.x][a.y]=;
que.push(a);
while(!que.empty()){
node b;
a=que.front();que.pop();
if(d[a.x][a.y]!=-){
mp[vis[sx][sy]][vis[a.x][a.y]]=d[a.x][a.y];
}
for(int k=;k<;k++){
int nx=dx[k]+a.x,ny=dy[k]+a.y;
if(check(nx,ny)){
b.x=nx,b.y=ny,d[b.x][b.y]=d[a.x][a.y]+;
que.push(b);
}
}
}
} int Prim()
{
int pos=,res=;
for(int i=;i<cnt;i++) low[i]=mp[pos][i];
for(int i=;i<cnt;i++){
int mi=INF;
for(int j=;j<cnt;j++){
if(!used[j]&&low[j]<mi){
mi=low[j],pos=j;
}
}
used[pos]=;
res+=low[pos];
for(int j=;j<cnt;j++){
if(!used[j]&&low[j]>mp[pos][j]){
low[j]=mp[pos][j];
}
}
}
return res;
} int main()
{
int t;
cin>>t;
while(t--){
memset(used,,sizeof(used));
memset(vis,-,sizeof(vis));
for(int i=;i<;i++)
for(int j=;j<;j++) mp[i][j]=INF;
cin>>n>>m;
char s[];
gets(s); //后面有些奇怪的东西,加上就AC了
cnt=;
for(int i=;i<m;i++){
gets(maze[i]);
} for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(maze[i][j]=='S'||maze[i][j]=='A'){
vis[i][j]=cnt++;
}
}
}
for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(vis[i][j]!=-) bfs(i,j);
}
}
int ans=Prim();
cout<<ans<<endl;
}
return ;
}

2016-05-20

POJ 3026 : Borg Maze(BFS + Prim)的更多相关文章

  1. POJ 3026 Borg Maze(bfs+最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6634   Accepted: 2240 Descrip ...

  2. Borg Maze(BFS+MST)

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

  3. 【POJ 3026】Borg Maze

    id=3026">[POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队能够无限切割 问搜索到全部alien所须要的总步数 即求一个无向图 包括全部的点而且总权值 ...

  4. POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16625   Accepted: 5383 Descri ...

  5. 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 Descrip ...

  6. POJ 2796:Feel Good(单调栈)

    http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://ww ...

  7. POJ 3318:Matrix Multiplication(随机算法)

    http://poj.org/problem?id=3318 题意:问A和B两个矩阵相乘能否等于C. 思路:题目明确说出(n^3)的算法不能过,但是通过各种常数优化还是能过的. 这里的随机算法指的是随 ...

  8. POJ 1200:Crazy Search(哈希)

    Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32483   Accepted: 8947 Des ...

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

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

随机推荐

  1. javascript实例学习之二——类新浪微博的输入框

    该案例实现如下效果,具体可见新浪微博网站的微博发布框 实现 以下效果效果1:当光标移入文本框时,文本框上方的文字发生变化,显示剩余可以输入的字数,当光标移出文本框,并且文本框中没有任何输入时,恢复最初 ...

  2. iOS XML  解析(原生的)

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  3. python 归档tarfile,zipfile学习

    一.tarfile 用法: tarfile.open(cls, name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)  返回一个Tar ...

  4. nyist 593 Take it easy

    http://acm.nyist.net/JudgeOnline/problem.php?pid=593 Take it easy 时间限制:1000 ms  |  内存限制:65535 KB 难度: ...

  5. BroadCast Receive 生命周期

    BroadCastReceiver 简介 BroadCastReceiver 源码位于: framework/base/core/java/android.content.BroadcastRecei ...

  6. linux 修改端口限制

    1.显示当前临时端口的范围:一般情形下:linux临时端口号范围是(32768,61000)      sysctl  net.ipv4.ip_local_port_range  或     cat ...

  7. LED流水灯(二)

    记住看汇编的时候是红在上面 黑色在下面 startup.s 程序 ; MDK跑马灯实验; PRESERVE8               // 字节对齐关键词 ,汇编有8位对齐的要求,要添加 AREA ...

  8. Windows 服务入门指南

    有很多时候,我们需要创建Windows Service. 这篇文章可以算是一个入门指南吧,希望对初学者有帮助. 要创建Windows Service, 首先选择Windows服务项目,如下图: 这里我 ...

  9. -XX:+printGC

    -XX:+printGC 可以打印GC的简要信息[GC 4790K->374K(15872K), 0.0001606 secs][GC 4790K->374K(15872K), 0.000 ...

  10. oracle 的索引

    一.索引分类      按逻辑分: 单列索引(Single column):  单列索引是基于单列所创建的索引 复合(多列)索引(Concatenated ): 复合索引是基于两列或者多列所创建的索引 ...