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 ...
随机推荐
- Redis发布订阅和事物笔记
Redis 发布订阅 Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. Redis 客户端可以订阅任意数量的频道. 下图展示了频道 cha ...
- 使用C#创建WCF服务控制台应用程序
本文属于原创,转载请注明出处,谢谢! 一.开发环境 操作系统:Windows 10 开发环境:VS2015 编程语言:C# IIS版本:10.0.0.0 二.添加WCF服务.Internet Info ...
- LeetCode Merge k Sorted Lists (链表)
题意 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- Kafka API: TopicMetadata
Jusfr 原创,转载请注明来自博客园 TopicMetadataRequest/TopicMetadataResponse 前文简单说过"Kafka是自描述的",是指其broke ...
- Gartner研究副总裁:人工智能的五点傲慢与偏见
对于人工智能能够为各企业机构完成哪些任务,IT与业务领导者们时常感到困惑,并深受多个人工智能错误观念的困扰.全球领先的信息技术研究和顾问公司Gartner认为,开发人工智能项目的IT与业务领导者必须分 ...
- kali linux 安装Nessus
Nessus 介绍: Nessus 是目前全世界最多人使用的系统漏洞扫描与分析软件.总共有超过75,000个机构使用Nessus 作为扫描该机构电脑系统的软件. 下载Nessus,我的是64为,我选择 ...
- PAT甲题题解-1095. Cars on Campus(30)-(map+树状数组,或者模拟)
题意:给出n个车辆进出校园的记录,以及k个时间点,让你回答每个时间点校园内的车辆数,最后输出在校园内停留的总时间最长的车牌号和停留时间,如果不止一个,车牌号按字典序输出. 几个注意点: 1.如果一个车 ...
- Android开发环境(发展演变)
初步接触android,要安装android开发工具时是使用eclipse,这是因为百度靠前的搜索项是eclipse来开 发android,而且那时还不知道android studio. 首先是下载配 ...
- cxgrid多选删除
设置OptionsData选项中的Editing设为True,按着Shift和Ctrl可实现多选 SelectionChanged事件 For i:= 0 To cxGrid1DBTableView1 ...
- [开源中国]Windows 10 全球市场份额正式超越 Windows 7
Windows 10 全球市场份额正式超越 Windows 7 全球知名科技数据调查公司 Netmarketshare 昨天发布了2018年12月份最新的桌面操作系统份额报告.对于微软来说,这是历史一 ...