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. idea中写servlet时报错--关于405错误

    将super方法注释掉 原因:super是调用了此类继承父类doget和dopost方法的, 如果此类中没有这个方法,就会报错The specified HTTP method is not allo ...

  2. 前端笔记之React(五)Redux深入浅出

    一.Redux整体感知 Redux是JavaScript状态管理容器,提供了可被预测状态的状态管理容器.来自于Flux思想,Facebook基于Flux思想,在2015年推出Redux库. 中文网站: ...

  3. Pyinstaller 打包工具的使用!!!

    打包成一个文件夹: pyinstaller xxx.py 打包成单个文件: pyinstaller -F xxx.py 打包成不显示终端的单个文件: pyinstaller -F -w xxx.py ...

  4. Find out "Who" and "Where"

    Yesterday a friend of mine Kirby came to me with a smartphone and she wanted me to do her a favor. S ...

  5. 自定义SWT控件五之自定义穿梭框

    5.自定义穿梭框 package com.view.control.shuttlebox; import java.util.ArrayList; import java.util.HashMap; ...

  6. 监控LVS

    监控LVS #!/usr/bin/python-2.6.6 #data 2017-10-17 #auth liuchao import commands,os,time #-------------- ...

  7. Vue2.0仿饿了么webapp单页面应用

    Vue2.0仿饿了么webapp单页面应用 声明: 代码源于 黄轶老师在慕课网上的教学视频,我自己用vue2.0重写了该项目,喜欢的同学可以去支持老师的课程:http://coding.imooc.c ...

  8. 什么?小程序实时语音识别你还在痛苦的对接科大讯飞?百度Ai识别?

    前言 微信小程序,说不上大火,但是需求还是不少的.各大企业都想插一足 于是前端同学就有事情做了. 需求 我需要录音 我边说话边识别,我要同声传译,我要文字转语音,还要萝莉音 我:??? 正文 一开始, ...

  9. 数据结构之队列java版

    //java由于泛型的擦除,用起来十分不方便 abstract class BaseQueue<T>{ abstract boolean enQueue(T x); abstract T ...

  10. 【C++】string::substr函数

    形式:s.substr(p, n) 返回一个string,包含字符串s中从p开始的n个字符的拷贝(p的默认值是0,n的默认值是s.size() - p,即不加参数会默认拷贝整个s) int main( ...