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 ...
随机推荐
- html5实现本页面元素拖放和本地文件拖放
HTML5拖放 拖放本地数据 1.HTML拖放 拖放(Drag 和 Drop)是HTML5标准的组成部分 2.拖放开始: ondragStart:调用了一个函数,drag(event),它规定了被 ...
- Linux DMA Engine framework(2)_功能介绍及解接口分析
http://www.wowotech.net/linux_kenrel/dma_engine_api.html 补充 http://www.zhimengzhe.com/linux/259646.h ...
- c++类成员函数重载常量与非常量版本时避免代码重复的一种方法
c++有时候需要为类的某个成员函数重载常量与非常量的版本,定义常量版本是为了保证该函数可作用于常量类对象上,并防止函数改动对象内容.但有时两个版本的函数仅仅是在返回的类型不同,而在返回前做了大量相同的 ...
- Python数据类型-02.字符串
本文主要记录字符串的相关知识,包括字符串的定义特点,常用方法和 请知悉: 计算机中,一切皆为对象世界万物,皆为对象,一切对象皆可分类 1.什么是字符串? 类似"hello world&quo ...
- I.MX6 eMMC 中启动U-boot存放的首地址
/************************************************************************************ * I.MX6 eMMC 中 ...
- C# 图片生成缩略图
C# 图片生成缩略图方法: /// <summary> /// 生成缩略图 /// </summary> /// <param name="fileName&q ...
- .NET工具软件收集
======================== ILSPY 官网: http://ilspy.net/ .NET Reflector 官网:http:/ ...
- JDBC的操作步骤
JDBC的操作步骤 一.什么是JDBC JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问 ...
- debugfs文件系统
debugfs: https://www.cnblogs.com/helloworldtoyou/p/6281415.html /proc/bus/input/devices /sys/kernel/ ...
- flow 类型生成工具 flow-typed 简单使用
flow 是一个javascript 的静态检查工具,flow-typed 为我们提供了三方类似type 的生成 安装flow-typed 使用全局安装 yarn global add flow-ty ...