POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 16625 | Accepted: 5383 |
Description
Your task is to help the Borg (yes, really) by developing a program which helps the Borg to estimate the minimal cost of scanning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is that the beginning of the search is conducted by a large group of over 100 individuals. Whenever an alien is assimilated, or at the beginning of the search, the group may split in two or more groups (but their consciousness is still collective.). The cost of searching a maze is definied as the total distance covered by all the groups involved in the search together. That is, if the original group walks five steps, then splits into two groups each walking three steps, the total distance is 11=5+3+3.
Input
Output
Sample Input
2
6 5
#####
#A#A##
# # A#
#S ##
#####
7 7
#####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
8
11
Source
题意:给你一个图里面有S和A,让你求把S和A全部连在一起的最少消耗,明着最小生成树,但是每一个S和A之间的距离用bfs求出就行。
主要是题目有个坑,在n,和m后可能会有空行;
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int INF=0x3f3f3f3f;
char mapp[][];
int a[][];
int n,m;
int dx[]={,-,,};
int dy[]={,,,-};
struct node{
int x,y;
};
int flag[][];
bool vis[];
int minn[];
int cost[][];///记录总的各点到各点的距离
int cnt[][];///记录当前点到bfs到其他各点的距离
void bfs(int sx,int sy){ queue<node>q;
while(!q.empty())q.pop();
memset(cnt,-,sizeof(cnt));
cnt[sx][sy]=;
node first;
first.x=sx,first.y=sy;
q.push(first);
while(!q.empty()){
node now=q.front();
q.pop();
if(a[now.x][now.y]!=-)///记录到全局为生成树做准备
cost[a[sx][sy]][a[now.x][now.y]]=cnt[now.x][now.y];
for(int i=;i<;i++){
node next;
next.x=now.x+dx[i];
next.y=now.y+dy[i];
if(mapp[next.x][next.y]=='#'||cnt[next.x][next.y]!=-||next.x<||next.y<||next.x>=n||next.y>=m)continue;
cnt[next.x][next.y]=cnt[now.x][now.y]+;
q.push(next);
}
}
}
int prim(int n){
int ans=;
memset(vis,false,sizeof(vis));
vis[]=true;
for(int i=;i<n;i++)minn[i]=cost[][i];
for(int i=;i<n;i++){
int minc=INF;
int p=-;
for(int j=;j<n;j++){
if(!vis[j]&&minc>minn[j]){
minc=minn[j];
p=j;
}
}
ans+=minc;
vis[p]=true;
for(int j=;j<n;j++)
if(!vis[j]&&minn[j]>cost[p][j]){
minn[j]=cost[p][j];
}
}return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
cin>>t;
while(t--){
cin>>m>>n;
gets(mapp[]);
memset(a,-,sizeof(a));
int num=;
for(int i=;i<n;i++){
gets(mapp[i]);
for(int j=;j<m;j++){
if(mapp[i][j]=='A'||mapp[i][j]=='S')
a[i][j]=num++;
}
}
/* for(int i=0;i<n;i++){
cout<<mapp[i]<<endl;
}*/
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(a[i][j]!=-){
bfs(i,j);
}
}
}
/* for(int i=0;i<num;i++){
for(int j=0;j<num;j++){
cout<<cost[i][j]<<" ";
}
cout<<endl;
}*/
cout<<prim(num)<<endl;
}
return ;
}
POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)的更多相关文章
- poj 3026 Borg Maze (bfs + 最小生成树)
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
- POJ - 3026 Borg Maze bfs+最小生成树。
http://poj.org/problem?id=3026 题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地.求从S出发,经过所有A所需要的最短路.你有一个特殊能力,当走到 ...
- 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加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- 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)
题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显 ...
- poj 3026 Borg Maze bfs建图+最小生成树
题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...
- POJ 3026 Borg Maze bfs+Kruskal
题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...
- POJ - 3026 Borg Maze(最小生成树)
https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...
随机推荐
- [CF620E]New Year Tree
题目大意:有一棵以$1$为根的有根树,有$n$个点,每个节点初始有颜色$c_i$.有两种操作: $1 v c:$将以$v$为根的子树中所有点颜色更改为$c$ $2 v:$ 查询以$v$为根的子树中的节 ...
- [洛谷P4001][BJOI2006]狼抓兔子
题目大意:给你一个n*m的网格图,有三种边,横的,纵的和斜的,要你求出它的最小割 题解:网络流 卡点:1.无向图,反向弧容量应和正向弧相同 C++ Code: #include<cstdio&g ...
- [AHOI2013]作业 & Gty的二逼妹子序列 莫队
---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...
- NEYC 2017 游记
day 1: result: sum_rank: 11 school_rank:1 水题在你高估的时候就已经不水了 sum:有个快速乘类似快速幂: int ans=0; ...
- POJ1733:Parity Game(离散化+带权并查集)
Parity Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12853 Accepted: 4957 题目链接 ...
- 如何取消PPT中的动画效果
幻灯片放映——>设置放映式——>勾选放映时不加动画 (office2007)
- Nginx替换过滤文本模块replace-filter-nginx-module
1.安装此模块需要先安装sregex运行库 apt-get update;apt-get install git make gcc -y #Centos改成yum git clone https:// ...
- vue文件使用stylus报错问题
先npm install stylus --save然后安装你少的page.json中依赖:npm install stylus-loader css-loader style-loader --sa ...
- 《vue.js实战》练习---数字输入框组件
html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 地震(quake)
地震 题目描述 一场地震毁了 Farmer John 的整个农场.他是个有恒心的人,决定重建农场.在重建了所有 n(1<=n<=400)块田野后,他意识到还得修路将它们连起来.完工后,任两 ...