Borg Maze(BFS+MST)
Borg Maze
http://poj.org/problem?id=3026
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 18230 | Accepted: 5870 |
Description
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
Output
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)的更多相关文章
- POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3026 Borg Maze(bfs+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
- POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16625 Accepted: 5383 Descri ...
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- POJ - 3026 Borg Maze(最小生成树)
https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...
- POJ 3026 Borg Maze (最小生成树)
Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...
- poj3206(bfs+最小生成树)
传送门:Borg Maze 题意:有一个迷宫,里面有一些外星人,外星人用字母A表示,#表示墙,不能走,空格可以走,从S点出发,在起点S和A处可以分叉走,问找到所有的外星人的最短路径是多少? 分析:分别 ...
- Meteor Shower POJ - 3669 (bfs+优先队列)
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26455 Accepted: 6856 De ...
随机推荐
- (转)亿级Web系统搭建——单机到分布式集群
当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的压力会越来越大,在这个过程中,我们会遇到很多的问题.为了解决这些性能压力带来问题,我们需要在Web系统架构层 ...
- Http协议——Header说明
下图是我用IE的开发人员工具截取的一个Http Request请求的Header. 下图是我用IE的开发人员工具截取的一个Http Response的Header. header常用指令 header ...
- 记一次git fatal: Unable to find remote helper for 'https'问题的解决
登陆到远程linux服务器上,使用git, clone的时候报“fatal: Unable to find remote helper for 'https'”错,没管,绕过,使用git clone ...
- 2017《Java技术》预备作业02
1.学习使用Git和码云托管代码 参考资料:如何使用Git和码云 安装Git 在码云注册账号,新建项目,名称为Java-CS01(02)XXX, 一班为CS01,二班为CS02,后三位或两位为姓名缩写 ...
- BZOJ4881: [Lydsy1705月赛]线段游戏(二分图)
4881: [Lydsy1705月赛]线段游戏 Time Limit: 3 Sec Memory Limit: 256 MBSubmit: 359 Solved: 205[Submit][Stat ...
- WPF 中的 NameScope
我们在 WPF 中使用绑定时可以使用 ElementName=Foo 这样的写法,并且还能够真的在运行时找到这个名称对应的对象,是因为 WPF 中提供了名称范围概念. 实现 INameScope 接口 ...
- java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy at sun.reflect.an ...
- .NET泛型解析(上)
[1]:泛型介绍 泛型是C#2.0中一个重要的新特性,泛型是CLR和编程语言提供的一种特殊机制,它支持另一种形式的代码重用.泛型通常用与集合以及作用于集合的方法一起使用,当然也可以单独使用. C#是一 ...
- 安装wamp 缺少msvcr100.dll
在一台新电脑上安装wampsever 这是百度上的解决方案,http://jingyan.baidu.com/article/0320e2c1eb49681b87507ba4.html 本人亲测 第一 ...
- MFC vs. SDK程序流程
大家都知道,windows API编程以及其消息处理,其过程都清晰可见,大体步骤如下: 1)声明消息窗口类 2)注册窗口类 3)createwindows 4)消息获得以及分派(windows pro ...