Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9971   Accepted: 3347

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 + 最短路的prim算法;
比较坑的是输入数字之后,必须用gets(),不能用getchar()
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#define WW freopen("a1.txt","w",stdout) using namespace std; const int INF = 0x3f3f3f3f; const int MAX = 110; struct Line
{
int x;
int y;
int num;
}; char Map[MAX][MAX]; int a[MAX][MAX]; int Dis[MAX][MAX]; int Dir[][2]= {{0,1},{0,-1},{1,0},{-1,0}}; int n,m,top; void BFS(int i,int j)//计算点之间的距离
{
bool vis[MAX][MAX];
queue<Line>q;
memset(vis,false,sizeof(vis));
Line ans,ant;
ans.x=i;
ans.y=j;
ans.num=0;
vis[ans.x][ans.y]=true;
q.push(ans);
while(!q.empty())
{
ant=q.front();
q.pop();
if(a[ant.x][ant.y]!=-1)
{
Dis[a[i][j]][a[ant.x][ant.y]]=ant.num;
}
for(int k=0; k<4; k++)
{
ans.x=ant.x+Dir[k][0];
ans.y=ant.y+Dir[k][1];
if(Map[ans.x][ans.y]=='#'||vis[ans.x][ans.y])
{
continue;
}
ans.num=ant.num+1;
vis[ans.x][ans.y]=true;
q.push(ans);
}
}
}
int dis[MAX];
bool vis[MAX];
int prim()
{
memset(vis,false,sizeof(vis));
int sum=0;
for(int i=1; i<top; i++)
{
dis[i]=Dis[0][i];
}
vis[0]=true;
for(int i=1; i<top; i++)
{
int ans=INF;
int flag=-1;
for(int j=0; j<top; j++)
{
if(!vis[j]&&dis[j]<ans)
{
ans=dis[j];
flag=j;
}
}
if(flag==-1)
return -1;
vis[flag]=true;
sum+=ans;
for(int j=0; j<top; j++)
{
if(!vis[j]&&dis[j]>Dis[flag][j])
{
dis[j]=Dis[flag][j];
}
}
}
return sum;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&m,&n);
gets(Map[0]);
top=0;
memset(a,-1,sizeof(a));
for(int i=0; i<n; i++)
{
gets(Map[i]);
for(int j=0; j<m; j++)
{
if(Map[i][j]=='S'||Map[i][j]=='A')
{
a[i][j]=top++;
}
}
}
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(a[i][j]!=-1)
{
BFS(i,j);
}
}
}
printf("%d\n",prim());
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. SRM 628 DIV2
  2. Ruby之基本数据类型(三)
  3. win7配置SMTP服务
  4. dscp hftp
  5. include使用中注意的问题
  6. StgCreateDocfileOnILockBytes复合文档
  7. ffmpeg-20160517-git-bin-v2
  8. 远程桌面Default.rdp 中各个参数的含义
  9. 方正S4101g笔记本电脑搜不到无线网络
  10. git gc
  11. Clean Code读书笔记
  12. 精通 Oracle+Python,第 9 部分:Jython 和 IronPython — 在 Python 中使用 JDBC 和 ODP.NET
  13. mysql日志文件相关的配置【1】
  14. jQuery validate 的valid()方法一直返回true
  15. java中可以出现的中文乱码的集中解决
  16. 在自学php的路上不知道怎么走!!
  17. 9.Java主要有那几种文件类型?各自的作用是什么?
  18. [JS]Math.random()
  19. AI 帮助涂鸦
  20. [Ynoi2018]未来日记

热门文章

  1. Servlet 3特性:异步Servlet
  2. vs2013 visual studio 插件安装
  3. PostgreSQL9.1 upgrade to PostgreSQL9.5rc1
  4. C++类构造析构调用顺序训练(复习专用)
  5. 1020: 部分A+B
  6. CSS_01_CSS和html结合的方式3、4
  7. Android Notification通知栏使用
  8. 夺命雷公狗ThinkPHP项目之----企业网站7之栏目的修改(主要用模型来验证字段)
  9. DFT设计绪论
  10. php基础知识和函数