Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6971   Accepted: 2345

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
 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int INF = 0x3f3f3f3f;
int dis[][],vis[][];
int m,n,cnt;
char map[][];
struct node
{
int x,y;
int id;
}cor[];
struct sear
{
int x,y;
int step;
};
queue <sear> que; int bfs(int x, int y,int ex, int ey)
{
memset(vis,,sizeof(vis));
while(!que.empty())
que.pop();
que.push((struct sear){x,y,});
vis[x][y] = ; while(!que.empty())
{
struct sear u = que.front();
que.pop();
if(u.x == ex && u.y == ey)
return u.step;
if(map[u.x-][u.y] != '#' && !vis[u.x-][u.y])
{
vis[u.x-][u.y] = ;
que.push((struct sear){u.x-,u.y,u.step+});
}
if(map[u.x+][u.y] != '#' && !vis[u.x+][u.y])
{
vis[u.x+][u.y] = ;
que.push((struct sear){u.x+,u.y,u.step+});
}
if(map[u.x][u.y-] != '#' && !vis[u.x][u.y-])
{
vis[u.x][u.y-] = ;
que.push((struct sear){u.x,u.y-,u.step+});
}
if(map[u.x][u.y+] != '#' && !vis[u.x][u.y+])
{
vis[u.x][u.y+] = ;
que.push((struct sear){u.x,u.y+,u.step+});
}
}
}
int prim_dis[];
int prim_vis[];
int prim(int id)
{
int i,j;
int ans = ;
memset(prim_vis,,sizeof(prim_vis));
prim_vis[id] = ;
for(i = ; i < cnt; i++)
prim_dis[i] = dis[id][i];
for(i = ; i < cnt; i++)
{
int min = INF,pos;
for(j = ; j < cnt; j++)
{
if(prim_dis[j] < min && !prim_vis[j])
{
min = prim_dis[j];
pos = j;
}
}
prim_vis[pos] = ;
ans += min;
for(j = ; j < cnt; j++)
{
if(!prim_vis[j] && prim_dis[j] > dis[pos][j])
prim_dis[j] = dis[pos][j];
}
}
return ans;
} int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
cnt = ;
scanf("%d %d",&m,&n);
char space[];
gets(space);
for(i = ; i < n; i++)
{
gets(map[i]);
for(j = ; j < m; j++)
{
if(map[i][j] == 'A' || map[i][j] == 'S')
cor[cnt++] = ((struct node){i,j,cnt});
}
}
for(i = ; i < cnt; i++)
{
for(j = ; j < cnt; j++)
{
if(i == j) dis[i][j] = ;
else dis[i][j] = INF;
}
}
for(i = ; i < cnt; i++)
{
for(j = i+; j < cnt; j++)
{
dis[i][j] = dis[j][i] = bfs(cor[i].x,cor[i].y,cor[j].x,cor[j].y);
}
}
printf("%d\n",prim());
}
return ;
}

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

  1. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  2. POJ3026——Borg Maze(BFS+最小生成树)

    Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...

  3. POJ - 3026 Borg Maze BFS加最小生成树

    Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...

  4. POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  5. POJ 3026 Borg Maze(Prim+bfs求各点间距离)

    题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’  ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...

  6. POJ3026 Borg Maze(bfs求边+最小生成树)

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

  7. POJ 3026 Borg Maze bfs+Kruskal

    题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...

  8. poj 3026 Borg Maze bfs建图+最小生成树

    题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...

  9. poj 3026 Borg Maze (bfs + 最小生成树)

    链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...

随机推荐

  1. XMPPFrameWork IOS 开发(一)xmpp简介

    原始地址:XMPPFrameWork IOS 开发(一) XMPP : The Extensible Messaging and Presence Protocol 中文全称: 可扩展通讯和表示协议 ...

  2. [PWA] 2. Service worker life cycle

    Once serive worker is registered, the first time we go to the app, we cannot see the logs from servc ...

  3. [Farcol] Introduce

    Use the Falcor Router to create a Virtual JSON resource. In this tutorial we will use Falcor’s expre ...

  4. poj2239 Selecting Courses --- 二分图最大匹配

    匈牙利算法模板题 有n门课程,每门课程可能有不同一时候间,不同一时候间的课程等价. 问不冲突的情况下最多能选多少门课. 建立二分图,一边顶点表示不同课程,还有一边表示课程的时间(hash一下). #i ...

  5. C++类中静态变量

    以下是对类中static变量的一点解说    ===============================================    静态数据成员的用法和注意事项例如以下:       ...

  6. java 连接数据库mysql的方法

    1.把那个文件配置好环境变量. 2.创建数据库,插入数据 注意的地方: (1)环境变量 classpath(可大写,也可以小写,可放在个人变量,也可以试系统变量) 里面的值 F:\mysql-conn ...

  7. [转] 消息系统该Push/Pull模式分析

    信息推拉技术简介 “智能信息推拉(IIPP)技术”是在网上信息获取技术中加入了智能成份,从而有助于用户在海量信息中高效.及时地获取最新信息,提高了信 息系统主动信息服务的能力.如果引入基于IIPP的主 ...

  8. josn 转php

    $data = josn_decode(data,[true]); 加true转化为php数组:不加为对象,使用:$data->'字段'.

  9. nodejs抓取网页内容

    function loadPage(url) { var http = require('http'); var pm = new Promise(function (resolve, reject) ...

  10. js面向对象--类式继承

    //待研究//类式继承 //js中模拟类式继承的3个函数 //简单的辅助函数,让你可以将新函数绑定到对象的 prototype 上 Function.prototype.method = functi ...