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 ...
随机推荐
- mysql alter修改字段的长度 类型sql语句
在mysql中alter命令可以修改字段类型,长度,名称或一些其它的参数,下面我来给大家介绍alter函数修改字段长度与类型的两个命令,希望文章来给各位带来帮助. mysql 修改字段长度 a ...
- ubuntu16 install nginx
1,更新系统 sudo apt-get update 2,安装nginx sudo apt-get install nginx 3,验证是否安装成功 curl 127.0.0.1 常用配置文件和命令 ...
- Jenkins配置slave遇到“无法启动该应用程序”的问题
飞测说:最近在负责持续集成相关的工作,我们用的是jenkins+svn+maven+sonar, 今天在用slave这块出现了一个问题,排查了好久才解决,踩过的坑,现在和大家一起看看,希望对大家有帮助 ...
- vue music 歌单组件
在data里面定义 discList: [] methods: { _getRecommend() { getRecommend().then((res) => { if(res.code == ...
- ambassador 学习九 多ambassador部署说明
目前官方稳文档没有写,但是demo 里面有,所以就整理出来,其实目前demo里面的 多实例部署用了多个服务的service(使用nodeport 暴露地址,具体使用就是制定ambassador 实例的 ...
- How To Enable EPEL Repository in RHEL/CentOS 7/6/5?
What is EPEL EPEL (Extra Packages for Enterprise Linux) is open source and free community based repo ...
- intellij idea 清除版本控制
一.概述 intellij idea 再加入版本控制后,在工作空间中的项目文件都会纳入管理范围,这样idea左侧 "project视图" 中的项目及文件也会出现红色(可能其它颜色) ...
- c# 爬虫(一) HELLO WORLD
最近在摸索爬虫相关的东西,写点随笔,以便忘记. 目的与用途 现实的项目中,我们需要太多的第三方接口了.而往往这些第三方接口由于条件限制,一时拿不到. 譬如: 1. 淘宝网今天有什么特价商品. 2. 百 ...
- ExtJs 扩展类CheckColumn的使用(事件触发)
[javascript] view plain copy print? 使用 Extjs 在进行数据库编程经常会遇到 checkbox 的问题(奇怪网上却没有此类文章不知道其他人是怎么解决的,在此贴上 ...
- 视频支持拖动进度条播放的实现(基于nginx)
http协议下的flv/mp4流式播放支持的三个要点: 1 服务器端要支持flv/mp4流式播放,现在nginx或者lighttpd都是支持这样的应用的,还支持mp4的流式播放(默认编译版本一般都是打 ...