POJ - 3026 Borg Maze(最小生成树)
https://vjudge.net/problem/POJ-3026
题意
在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度。
分析
注意审题!!一开始读成可在任意位置分头走,结果题意限制了只能在字母处分开。这样就好做多了,L就是最小生成树的权值。因为这和生成树的处理过程很像,每一次找最近的点加入到集合中,即走过的路不用再考虑。本题难度在建图,先找出那几个字母的位置,给他们标号,然后逐个字母点进行bfs,找出他们到其他字母点的最短距离。建好后就套一下最小生成树的模板。坑点:最小树上的顶点数最多为100而不是50,用gets接受一行字符串,使用getchar()接受多余字符会wa(不知道原因),但在scanf中加多个空格就可以。。。
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
template <class T>
inline bool scan_d(T &ret){
char c;int sgn;
if(c=getchar(),c==EOF) return ;
while(c!='-'&&(c<''||c>'')) c=getchar();
sgn=(c=='-')?-:;
ret=(c=='-')?:(c-'');
while(c=getchar(),c>=''&&c<='') ret = ret*+(c-'');
ret*=sgn;
return ;
}
//const int N = 1e6+10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = ;
int T;
void testcase(){
printf("Case %d:",++T);
}
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
const double PI = acos(-1.0); bool vis[MAXM];
int lowc[MAXM];
int cost[MAXM][MAXM],index[MAXM][MAXM];
char maze[MAXM][MAXM];
int x,y; struct node{
int x,y,step;
};
bool vist[MAXM][MAXM];
int dir[][]={{,},{,},{-,},{,-}}; bool check(int a,int b){
if(a<||a>=y) return false;
if(b<||b>=x) return false;
if(maze[a][b]=='#') return false;
return true;
}
void bfs(int s,int x,int y){
mset(vist,false);
vist[x][y]=true;
node now,tmp;
now.x=x;now.y=y;now.step=;
queue<node> que;
que.push(now);
while(!que.empty()){
now = que.front();
que.pop();
if(index[now.x][now.y]!=-){
cost[s][index[now.x][now.y]]=now.step;
// cost[index[now.x][now.y]][s]=now.step;
}
for(int i=;i<;i++){
tmp.x = now.x+dir[i][];
tmp.y = now.y+dir[i][];
tmp.step = now.step+;
if(!vist[tmp.x][tmp.y]&&check(tmp.x,tmp.y)){
vist[tmp.x][tmp.y]=true;
que.push(tmp);
}
}
}
} int Prim(int n){
int ans=;
mset(vis,false);
vis[]=true;
for(int i=;i<n;i++) lowc[i]=cost[][i];
for(int i=;i<n;i++){
int minc = inf;
int p = -;
for(int j=;j<n;j++){
if(!vis[j]&&minc>lowc[j]){
minc = lowc[j];
p = j;
}
}
if(minc == inf){
return -;
}
ans+=minc;
vis[p]=true;
for(int j=;j<n;j++){
if(!vis[j]&&lowc[j]>cost[p][j]){
lowc[j] = cost[p][j];
}
}
}
return ans;
} int main() {
#ifdef LOCAL
freopen("data.in","r",stdin);
#endif // LOCAL int n;
scanf("%d",&n);
while(n--){
scanf("%d%d ",&x,&y);
mset(index,-);
for(int i=;i<MAXM;i++) cost[i][i]=; int cnt=;
for(int i=;i<y;i++){
gets(maze[i]);
for(int j=;j<x;j++){
if(maze[i][j]=='A'||maze[i][j]=='S'){
index[i][j]=cnt++;
}
}
}
for(int i=;i<y;i++){
for(int j=;j<x;j++){
if(index[i][j]!=-){
bfs(index[i][j],i,j);
}
}
} int ans=Prim(cnt);
printf("%d\n",ans);
}
return ;
}
POJ - 3026 Borg Maze(最小生成树)的更多相关文章
- poj 3026 Borg Maze 最小生成树 + 广搜
点击打开链接 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7097 Accepted: 2389 ...
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- poj 3026 Borg Maze (最小生成树+bfs)
有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- POJ 3026 Borg Maze【BFS+最小生成树】
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ - 3026 Borg Maze BFS加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- POJ 3026 Borg Maze(bfs+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
- POJ 3026 Borg Maze (最小生成树)
Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...
- poj 3026 Borg Maze (bfs + 最小生成树)
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
- POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16625 Accepted: 5383 Descri ...
随机推荐
- 架构师修炼 III - 掌握设计原则
关于软件的设计原则有很多,对于设计原则的掌握.理解.实践及升华是架构师的一项极为之必要的修炼. 记得在12年前第一次阅读<敏捷开发>时,五大基本设计原则就深深地植入到我的脑海中一直影响至今 ...
- HTML 图像实例
61.插入图像本例演示如何在网页中显示图像.图像标签(<img>)和源属性(Src)在 HTML 中,图像由 <img> 标签定义. <img> 是空标签,意思是说 ...
- Qt QpushButton 实现长按下功能
做项目需要一个按钮具备长时间按下的功能,才发现Qt原始的按钮是没有这个功能,不过Qt的原生按钮是存在按下和释放信号的,有了这两个信号,再来实现按钮长时间被按下,这就简单了,看下动画演示. 录成GIF效 ...
- selenium+python自动化----xlrd,xlswriter
1.准备: 使用之前需要先按照:打开cmd,输入pip install xlrd(xlswriter),点击enter; 2.基本使用: xlrd: #打开els文件,参数是文件路径: table = ...
- telnet协议:简介与安装使用
Telnet简介 Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机上完成远程主机工作的能力.在终端使用者的电脑上使用teln ...
- 微软职位内部推荐-Sr. SE - Office incubation
微软近期Open的职位: Senior Software Engineer-Office Incubation Office China team is looking for experienced ...
- linux 操作 mysql 指定端口登录 以及启动 停止
linux 操作 mysql 指定端口登录 mysql -uroot -p -h10.154.0.43 -P3341 1.查看mysql版本方法一:status;方法二:select version( ...
- Web Workers文档
Web Worker为Web内容在后台线程中运行脚本提供了一种简单的方法.线程可以执行任务而不干扰用户界面.此外,他们可以使用XMLHttpRequest执行 I/O (尽管responseXML和 ...
- 2-Eleventh Scrum Meeting20151211
第二阶段任务分工整理会议 1.会议任务: (1)明晰第二阶段的开发内容,统计未完成的功能留需完善开发. (2)安排任务分工,每个人的工作安排. (3)PM职位担任. (4)博客内容负责. 2.会议记录 ...
- Linux内核读书笔记第二周
什么是系统调用 简单来说,系统调用就是用户程序和硬件设备之间的桥梁.用户程序在需要的时候,通过系统调用来使用硬件设备. 系统调用的存在,有以下重要的意义: 1)用户程序通过系统调用来使用硬件,而不用关 ...