Borg Maze

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

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18230   Accepted: 5870

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

Source

很坑,数据n,m后面有很长的空格,要用gets读掉

 #include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std; struct sair{
int x,y,step;
}p[];
int fa[];
int n,m; int Find(int x){
int r=x,y;
while(x!=fa[x]){
x=fa[x];
}
while(r!=x){
y=fa[r];
fa[r]=x;
r=y;
}
return x;
} int join(int x,int y){
int xx=Find(x);
int yy=Find(y);
if(xx!=yy){
fa[xx]=yy;
return true;
}
return false;
} struct DIST{
int x,y,dis;
}dis[]; bool cmp(DIST a,DIST b){
return a.dis<b.dis;
} int dir[][]={,,,,,-,-,};
int co; char mp[][];
int book[][]; void BFS(int x,int y){
queue<sair>Q;
memset(book,,sizeof(book));
sair s,e;
s.x=x,s.y=y,s.step=;;
Q.push(s);
while(!Q.empty()){
s=Q.front();
Q.pop();
for(int i=;i<;i++){
e.x=s.x+dir[i][];
e.y=s.y+dir[i][];
if(e.x>=&&e.x<n&&e.y>=&&e.y<m&&!book[e.x][e.y]&&mp[e.x][e.y]!='#'){
e.step=s.step+;
Q.push(e);
book[e.x][e.y]=;
if(mp[e.x][e.y]=='A'||mp[e.x][e.y]=='S'){
dis[co].x=x*m+y+,dis[co].y=e.x*m+e.y+,dis[co++].dis=e.step;
}
}
}
}
} int main(){
int T;
scanf("%d",&T);
while(T--){
co=;
char dd[];
scanf("%d %d",&m,&n);
gets(dd);
for(int i=;i<=m*n;i++) fa[i]=i;
for(int i=;i<n;i++) gets(mp[i]);
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(mp[i][j]=='S'||mp[i][j]=='A'){
BFS(i,j);
}
}
}
sort(dis,dis+co,cmp);
int ans=;
for(int i=;i<co;i++){
if(join(dis[i].x,dis[i].y)){
ans+=dis[i].dis;
}
}
printf("%d\n",ans);
}
}
/*
1
50 7
##################################################
# AAAAAAA#AAAAAA AAA S #
# AAAAAAAAAA AAAAAAAAAAAAAA#####AAAAA### #
# A##A#A#A#A###A#A#A#A#A#A#A#A#A#A#A##A#A# #
# A ###########
# AAAAAAAAAAAA########## #
##################################################
*/

Borg Maze(BFS+MST)的更多相关文章

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

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

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

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

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

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

  4. POJ 3126 Prime Path(BFS算法)

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

  5. Cleaning Robot (bfs+dfs)

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

  6. POJ - 3026 Borg Maze(最小生成树)

    https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...

  7. POJ 3026 Borg Maze (最小生成树)

    Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...

  8. poj3206(bfs+最小生成树)

    传送门:Borg Maze 题意:有一个迷宫,里面有一些外星人,外星人用字母A表示,#表示墙,不能走,空格可以走,从S点出发,在起点S和A处可以分叉走,问找到所有的外星人的最短路径是多少? 分析:分别 ...

  9. Meteor Shower POJ - 3669 (bfs+优先队列)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26455   Accepted: 6856 De ...

随机推荐

  1. Kafka术语解释

    前一篇文章介绍了如何使用kafka收发消息,但是对于kafka的核心概念并没有详细介绍,这里将会对包括kafka基本架构以及消费者.生产者API涉及的术语进行说明.了解这些术语有助于更深入理解kafk ...

  2. PyQt5 中调用MySql接口失败 ( QSqlDatabase 组件) 在Linux环境下如何修改

    最近在跑下面这么一个代码,怎么跑都无法连通服务器,如下: # -*- coding: utf-8 -*- ''' [简介] PyQt5中 处理database 例子 ''' import sys fr ...

  3. 多目标跟踪baseline methods

    参考文献: MOTChallenge 2015: Towards a Benchmark for Multi-Target TrackingLaura Leal-Taix ´e, Anton Mila ...

  4. Python源码分析之dis

    一.简单例子 def add(a, b): return a + b add_nums.py import foo a = [1, 'python'] a = 'a string' def func( ...

  5. Struts2自定义标签3模仿原有的s:if s:elseif s:else自定义自己的if elsif else

    第一步:webroot/web-inf下简历str.tld文件 <?xml version="1.0" encoding="UTF-8"?> < ...

  6. Linux 脚本编写

    第一个shell脚本编写 #!/bin/bash # 上面中的 #! 是一种约定标记, 它可以告诉系统这个脚本需要什么样的解释器来执行; #定义变量: APP_BASE_PATH="/opt ...

  7. c#:Json字符串转成xml对象

    没看到.net framework中有这样的功能, 懒得到处找了, 索性花点时间自己写一个 /* * Created by SharpDevelop. * Date: 2013/6/24 * User ...

  8. sublime Text 3安装sublimecodeIntel插件

    下载:https://github.com/SublimeCodeIntel/SublimeCodeIntel   解压到:  data/pacakges目录 安装 Package Control插件 ...

  9. thinkphp3.2.3+smarty解决success调用模板错误心得

    最近学习thinkphp上瘾,出现success找不到模板问题,查阅各大神解决方案,分享一下针对新手如何解决该问题,如有不对的地方请大神指正 1.首先修改自己的config文件,添加如下配置代码:// ...

  10. erlang单独配置文件

    一种是erl启动的时候加参数 doudizhu.config [ {doudizhu,[ {listen_port, }, {node_caller_prefix,"ruby"}, ...