J - Borg Maze
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 110
using namespace std;
int fa[MAXN];
struct nond{
int x,y,z;
}v[MAXN*MAXN];
struct none{
int x,y,z;
};
int dx[]={,-,,};
int dy[]={,,,-};
int t,n,m,tot,sum,ans,point;
int map[MAXN][MAXN],vis[MAXN][MAXN];
void bfs(int x,int y){
queue<none>que;none s;s.x=x;s.y=y;s.z=;
memset(vis,,sizeof(vis));vis[x][y]=;
que.push(s);int k=;
while(!que.empty()){
none now=que.front();
que.pop();
for(int i=;i<;i++){
int cx=now.x+dx[i];
int cy=now.y+dy[i];
int cz=now.z+;
if(cx>=&&cx<=n&&cy>=&&cy<=m&&map[cx][cy]>=&&!vis[cx][cy]){
if(map[cx][cy]>){ v[++tot].x=map[x][y];v[tot].y=map[cx][cy];v[tot].z=cz;k++; }
if(k==point) return ;
none tmm;tmm.x=cx;tmm.y=cy;tmm.z=cz;
vis[cx][cy]=;que.push(tmm);
}
}
}
}
int cmp(nond a,nond b){
return a.z<b.z;
}
int find(int x){
if(fa[x]==x) return x;
else return fa[x]=find(fa[x]);
}
int main(){
scanf("%d",&t);
char tmp[MAXN];
while(t--){
scanf("%d%d",&m,&n);
gets(tmp);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
char x;scanf("%c",&x);
if(x=='#') map[i][j]=-;
else if(x==' ') map[i][j]=;
else map[i][j]=++point;
}
char c;scanf("%c",&c);
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(map[i][j]>) bfs(i,j);
sort(v+,v++tot,cmp);
for(int i=;i<=point;i++) fa[i]=i;
for(int i=;i<=tot;i++){
int dx=find(v[i].x);
int dy=find(v[i].y);
if(dx==dy) continue;
fa[dy]=dx;sum++;
ans+=v[i].z;
if(sum==point-) break;
}
cout<<ans<<endl;
ans=;tot=;sum=;point=;
}
}
J - Borg Maze的更多相关文章
- 最小生成树+BFS J - Borg Maze
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...
- J - Borg Maze - poj 3026(BFS+prim)
在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, *************** ...
- J - Borg Maze +getchar 的使用注意(二维字符数组的输入)
题目链接: https://vjudge.net/contest/66965#problem/J 具体思路: 首先将每个点之间的最短距离求出(bfs),A 或者 S作为起点跑bfs,这样最短距离就求出 ...
- POJ 3026 Borg Maze【BFS+最小生成树】
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- Borg Maze(MST & bfs)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9220 Accepted: 3087 Descrip ...
- 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 最小生成树 + 广搜
点击打开链接 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7097 Accepted: 2389 ...
- 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+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
随机推荐
- UVAlive 6560 - The Urge to Merge(状压dp)
LA 6560 - The Urge to Merge option=com_onlinejudge&Itemid=8&page=show_problem&problem=45 ...
- session 的工作原理
session.cookie_domain session.cookie_path session.name session.save_path session.use_cokies session_ ...
- POJ2184 Cow Exhibition 背包
题目大意:已知c[i]...c[n]及f[i]...f[n],现要选出一些i,使得当sum{c[i]}和sum{f[i]}均非负时,sum(c[i]+f[i])的最大值. 以sum(c[i])(c[i ...
- Linux - 网络相关指令
系统时间与开关机 查看系统时间 date 查看硬件日期 hwclock 学习Linux不必全部指令都会,只要记住主要常用的几个就可以了.--MK 关机命令 shutdown init reboot p ...
- 在iOS开发中,我们会遇到十六进制和字符串之间相互转换,话不多说,直接上代码:
//将十六进制的字符串转换成NSString则可使用如下方式: + (NSString *)convertHexStrToString:(NSString *)str { if (!str || [s ...
- Java:笔记-1
ylbtech-Java:笔记-1 1.返回顶部 1. /** * 简介请求 * @return */ @RequestMapping("/JJ") public String j ...
- WPF中ListBox ListView数据翻页浏览笔记(强调:是数据翻页,非翻页动画)
ListBox和ListView在应用中,常常有需求关于每页显示固定数量的数据,然后通过Timer自动或者手动翻页操作,本文介绍到的就是该动作的实现. 一.重点 对于ListBox和ListView来 ...
- 【转】window 安装redis服务、卸载redis服务和启动redis服务
1.安装redis服务 redis-install.bat 1 echo install redis-server23 D:\redis\redis-server.exe --service-inst ...
- TPL详解、使用
使用时注意点 private async void button5_Click(object sender, EventArgs e) { /* string i1 = await F1Async() ...
- TensorFlow-LSTM序列预测
问题情境:已知某一天内到目前为止股票各个时刻的价格,预测接下来短时间内的价格变化. import tushare as ts import time from collections import n ...