Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12729   Accepted: 4153

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
【题意】在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度。
【分析】一开始没懂题意,后来懂了,就是简单的BFS+Prim。但是WA了好几发,伤心至极,去看看Discuss。发现好多人都被坑了,输入法人x,y后面还有好多空格,必须提前gets掉。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
int n,m,t,kk,ii,cnt,edg[N][N],lowcost[N];
int d[][]= {,,,,-,,,-};
int vis[N][N];
int num[N][N];
char w[N][N];
struct man {
int x,y,step;
};
void bfs(man s) {
queue<man>q;
while(!q.empty())q.pop();
q.push(s);
vis[s.x][s.y]=;
while(!q.empty()) {
man t=q.front();
q.pop();
if(w[t.x][t.y]=='A'||w[t.x][t.y]=='S') {
int u=num[s.x][s.y];
int v=num[t.x][t.y];
edg[u][v]=edg[v][u]=t.step;
}
for(int l=; l<; l++) {
int xx=t.x+d[l][],yy=t.y+d[l][];
if(xx>=&&xx<n&&yy>=&&yy<m&&vis[xx][yy]==&&w[xx][yy]!='#') {
man k;
k.x=xx,k.y=yy;
k.step=t.step+;
q.push(k);
vis[xx][yy]=;
}
}
}
}
void Prim() {
for(int i=; i<cnt; i++) {
lowcost[i]=edg[ii][i];
}
lowcost[ii]=-;
int sum=;
for(int i=; i<cnt; i++) {
int minn=inf;
for(int j=; j<cnt; j++) {
if(lowcost[j]!=-&&lowcost[j]<minn) {
minn=lowcost[j];
kk=j;
}
}
sum+=minn;
lowcost[kk]=-;
for(int j=; j<cnt; j++) {
if(edg[j][kk]<lowcost[j]) {
lowcost[j]=edg[j][kk];
}
}
}
printf("%d\n",sum);
}
int main() {
scanf("%d",&t);
while(t--) {
memset(edg,,sizeof(edg));
memset(lowcost,,sizeof(lowcost));
scanf("%d%d",&m,&n);
char tmp[N];gets(tmp);//一定要有这个,这是这个题目最坑的地方
for(int i=; i<n; i++) {
gets(w[i]);
}
cnt=;
for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
if(w[i][j]=='A'||w[i][j]=='S') {
num[i][j]=cnt++;
if(w[i][j]=='S') {
ii=cnt-;
}
}
}
}
for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
if(w[i][j]=='A'||w[i][j]=='S') {
man s;
s.x=i;
s.y=j;
s.step=;
memset(vis,,sizeof(vis));
bfs(s);
}
}
}
Prim();
}
return ;
}

POJ3026 Borg Maze(Prim)(BFS)的更多相关文章

  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 prim算法 难度:0

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

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

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

  4. Borg Maze(MST & bfs)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9220   Accepted: 3087 Descrip ...

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

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

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

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

  7. POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14165   Accepted: 4619 Descri ...

  8. Borg Maze(BFS+MST)

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

  9. POJ 3026 Borg Maze【BFS+最小生成树】

    链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

随机推荐

  1. 【转】IBatis.Net项目数据库SqlServer迁移至Oracle

    转自:http://www.2cto.com/database/201312/265514.html 最近完成了一个(IBatis.Net+MVC)项目的数据库+代码迁移工作,可把我折腾得~~~ IB ...

  2. 【题解】Bzoj4316小C的独立集

    决定要开始学习圆方树 & 仙人掌相关姿势.加油~~ 其实感觉仙人掌本质上还是一棵树,长得也还挺优美的.很多的想法都可以往树的方面上靠,再针对仙人掌的特性做出改进.这题首先如果是在树上的话那么实 ...

  3. Visio中ShapeAdded和SelectionAdded

    SelectionAdded 和 ShapeAdded 事件的相似之处在于它们都在创建形状之后触发.它们的区别在于,当单个操作添加多个形状时它们的行为方式不同.假定一个 Paste 操作创建三个新建形 ...

  4. 【ZJ选讲·BZOJ 5073】

    小A的咒语 给出两个字符串A,B (len<=105) 现在可以把A串拆为任意段,然后取出不超过 x 段,按在A串中的前后顺序拼接起来 问是否可以拼出B串. [题解]       ①如果遇 ...

  5. Angular白名单&&Angular拦截器 全局通用

    //angular 白名单全局通用 app.config([ '$compileProvider', function ($compileProvider) { $compileProvider.aH ...

  6. Win10的WSL很好用呀

    WSL全名是Windows Subsystem for Linux,是win10版本号16xx之后推出的开发者功能,提供了如原生linux版的体验. 最近最新的win10春季版1803出来了,安装了看 ...

  7. 洛谷P1339 热浪

    P1339 热浪 529通过 1.3K提交 题目提供者yeszy 标签图论福建省历届夏令营 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 求助...为什么是未知错误… 求修正,貌似死循环 第 ...

  8. 使用state模块部署lamp架构

    install_httpd: pkg.installed: - name: httpd httpd_running: service.running: - name: httpd - enable: ...

  9. python实现多个文件的分发

    之前写的脚本只能分发一个配置,每次分发多个配置总要执行很多次,很不爽,于是就有了这个脚本 from multiprocessing import Process import paramiko imp ...

  10. C++中的垃圾回收和内存管理

    最开始的时候看到了许式伟的内存管理变革系列,看到性能测试结果的时候,觉得这个实现很不错,没有深入研究其实现.现在想把这个用到自己的一个项目中来,在linux下编译存在一些问题,所以打算深入研究一下. ...