Borg Maze

题意:

  题目我一开始一直读不懂。有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点。

思路:

  感觉就算读懂了题目,也比较难想到这用到了最小生成树的知识,因为可以分身,所以每个点可以向其他点都连上边。可以用bfs预处理出所有的S点,A点的连线,再跑一遍最小生成树,即可得出答案。这里有几点注意,一开始我bfs没有记录step,而是直接找到一点后算曼哈顿距离,这是不对的,因为可能是绕了一个圈到了这个点。还有读完数字再读字符串的话,以后尽量用getline(cin,str),防止题目数据后面多几个空格。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/ const int maxn = ;
int fa[maxn*maxn],tot = ,cnt = ,t,n,m;
bool vis[maxn][maxn];
int dis[maxn][maxn];
struct node
{
int x,y;
}p[maxn];
struct eg{
int u,v,dis;
}e[maxn*maxn];
bool cmp(eg a,eg b){
return a.dis < b.dis;
}
int find(int x){
if(fa[x] == x)return x;
return fa[x] = find(fa[x]);
}
string str[maxn]; int nt[][] = {
{,},{,},{-,},{,-}
};
void bfs(int x,int y){
for(int i=; i<m; i++){
for(int j=; j<n; j++){
dis[i][j] = inf;
vis[i][j] =false;
}
}
queue<pii>q;
q.push(pii(x,y));
vis[x][y] = true;
dis[x][y] = ;
while(!q.empty()){
pii tmp = q.front();q.pop();
int tx = tmp.fi,ty = tmp.se;
for(int i=; i<; i++){
int nx = tx + nt[i][];
int ny = ty + nt[i][];
if(nx <||nx >= m || ny < || ny >=n)continue;
if(str[nx][ny] == '#')continue;
if(vis[nx][ny])continue;
vis[nx][ny] = true;
dis[nx][ny] = min(dis[nx][ny], dis[tx][ty]+);
if(str[nx][ny] == 'A' || str[nx][ny] == 'S'){
e[++tot].u = x * n + y;
e[tot].v = nx * n + ny;
e[tot].dis = dis[nx][ny];
// if(x == 1 && y==1){
// cout<<e[tot].u<<" "<<e[tot].v<<" "<<e[tot].dis<<endl;
// }
}
q.push(pii(nx,ny));
}
}
}
int main(){
OKC;
cin>>t;
while(t--){
cin>>n>>m;
getline(cin,str[]);
tot = ;
for(int i=; i<m; i++){
getline(cin,str[i]);
}
for(int i=; i<m; i++){
for(int j=; j<n; j++){
if(str[i][j] == 'A' || str[i][j] == 'S')bfs(i,j);
}
}
for(int i=; i<=n*m*; i++)fa[i] = i;
sort(e+,e++tot,cmp); int ans = ;
for(int i=; i<=tot; i++){
int fx = find(e[i].u);
int fy = find(e[i].v);
if(fx != fy){
ans += e[i].dis;
fa[fx] = fy;
// debug(e[i].dis);
}
}
cout<<ans<<endl;
}
return ;
} /*
#####
#A#A##
# # A#
#S ##
##### #####
#AAA###
# A#
# S ###
# #
#AAA###
#####
*/

POJ 3026

POJ - 3026 Borg Maze BFS加最小生成树的更多相关文章

  1. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  2. poj 3026 Borg Maze (bfs + 最小生成树)

    链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...

  3. POJ - 3026 Borg Maze bfs+最小生成树。

    http://poj.org/problem?id=3026 题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地.求从S出发,经过所有A所需要的最短路.你有一个特殊能力,当走到 ...

  4. poj 3026 Borg Maze bfs建图+最小生成树

    题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...

  5. POJ 3026 Borg Maze bfs+Kruskal

    题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...

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

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

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

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

  8. 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0

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

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

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

随机推荐

  1. jQuery通过id和name获取值的区别

    $(#'id').函数 $("input[name='name']")

  2. GDB 基本用法

    1.编译文件时需要加上 -g 选项,并非是将源码嵌入可执行文件,只是加入源代码的信息.eg:gcc -g main.c -o main 2.直接按回车键会重复上一条命令 3.基本指令 help,可以查 ...

  3. 【iOS】PrefixHeader.pch

    还不太理解,暂且记下.

  4. 【Python-Django】Jinja2模板引擎配置教程详解!!!!

    Jinjia2的官方文档:http://jinja.pocoo.org/docs/2.10/ 1. 安装Jinja2扩展包 $ pip install Jinja2 2. 配置Jinja2模板引擎 T ...

  5. CSS开启硬件加速来提高网站性能

    原文永久链接 CSS animations, transforms 以及 transitions 不会自动开启GPU加速,而是由浏览器的缓慢的软件渲染引擎来执行. 那我们怎样才可以切换到GPU模式呢, ...

  6. 使用 PowerShell 远程管理

    要求 PowerShell 版本要求至少是2.0版本以上,目前PowerShell 2.0 支持最低的操作系统版本为Windows XP.本次操作使用的是 PowerShell 5.1 请使用管理员身 ...

  7. CentOS7.6源码编译安装PHP 7.3.8

    安装步骤 PHP官网下载链接:https://www.php.net/downloads.php 1. 使用wget命令下载源码安装包 wget https://www.php.net/distrib ...

  8. 【Java例题】1.4圆类

    4.定义一个圆类,包括半径.构造方法.计算周长方法, 计算面积方法和显示半径方法. 然后编写一个主类,在其主方法中通过定义一个圆对象来 显示圆的半径.周长和面积. package study; imp ...

  9. 什么是Singleton?

    Singleton:在Java中即指单例设计模式,它是软件开发中最常用的设计模式之一. 单:指唯一 例:指实例 单例设计模式,即某个类在整个系统中只能有一个实例对象可被获取和使用的代码模式. 要点: ...

  10. apicloud 开发环境搭建

     之前做过appcan 手机应用的开发,工作需要切换的apicloud , 开发环境的的搭建是开发的第一步,let's go 1新建应用 step1  注册账号 注册apicloud 账号:https ...