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 ...
 
随机推荐
- 重写ajax方法实现特定情况下跳转登录页面
			
jQuery(function($){ // 备份jquery的ajax方法 var _ajax=$.ajax; // 重写ajax方法, $.ajax=function(opt){ var _suc ...
 - [linux]df 磁盘100%Used告警,du显示目录状态良好的故障排查
			
1.回顾: 某在线主机深夜连续接到告警系统的disk Used 超限告警. 登陆主机查看却遇到了困惑:在检查磁盘使用量 df –h 出来的磁盘使用量确实和告警信息一样,已经被100%占用,但是查看目录 ...
 - mybatis异常:Could not find result map Java.util.Map 问题分析及解决   定位不到具体的位置的错误
			
mybatis异常:Could not find result map Java.util.Map 问题分析及解决 报这个错误呢,很难受的就是你定位不到具体的地方,找不到位置修改,你只知道有错误,但是 ...
 - 使用TortoiseGit+码云管理项目代码
			
1.下载安装msysgit. 2.下载安装tortoisegit. 3.创建ssh密钥. 开始–所有程序–TortoiseGit–PuTTYgen 生成方法:点击“Generate”后,鼠标在key下 ...
 - 《DSP using MATLAB》Problem 2.9
			
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
 - JavaScript异步编程__“回调地狱”的一些解决方案
			
异步编程在JavaScript中非常重要.过多的异步编程也带了回调嵌套的问题,本文会提供一些解决“回调地狱”的方法. setTimeout(function () { console.log('延时触 ...
 - 【网络通信】TCP三次握手和四次挥手的示意图
			
三次握手 TCP连接是通过三次握手来连接的. 第一次握手 当客户端向服务器发起连接请求时,客户端会发送同步序列标号SYN到服务器,在这里我们设SYN为m,等待服务器确认,这时客户端的状态为SYN_SE ...
 - java编程排序之自定义类型的集合,按业务需求排序
			
自定义引用类型放入集合中,按实际业务需求进行排序的两种思路 第一种思路: (1)自定义实体类实现java.lang.Comparable接口,重写public int compareTo(Object ...
 - purescript 基本试用
			
安装环境 安装预编译文件 https://github.com/purescript/purescript/releases 配置环境变量: export PATH=$PATH:/Users/dalo ...
 - TestCase和TestSuite详解
			
一.TestCast和TestSuite概念介绍 TestCase:字面意思,测试用例.为一个或多个方法提供测试方法,一般是一个test. TestSuite:测试集合,即一组测试.一个test su ...