poj3026
Borg Maze
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12952 | Accepted: 4227 |
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 题目大意:从S点出发,把图中所有A字符连通的最短路径 思路:因为连通所有字符,想到用Prim算法,构造最小生成树,但是我们需要各个点的距离关系
所以再用bfs求各个点的之间的距离。注意的是不要一个一个的求,否则很可能会超时,把一个点
到其他所有点的距离一次求完,也就是每一次都遍历整个图 代码如下:
#include <iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxs = ;
char a[maxs][maxs];
struct Point
{
int col;
int row;
int step;
}node[maxs];
int col,row,nums;//nums需要被连通的所有点的个数
int edge[maxs][maxs];
int dir[][]={{-,},{,},{,-},{,}};//上下左右
bool judge(Point point)
{
if(point.col>&&point.col<=col&&point.row>&&point.row<=row&&a[point.row][point.col]!='#')
return true;
return false;
} void bfs(int i)
{
bool vis2[maxs][maxs];
int dist[maxs][maxs];//用来打表
memset(vis2,false,sizeof(vis2));
queue<Point> q;
node[i].step=;
q.push(node[i]);
vis2[node[i].row][node[i].col]=true;
Point cur,next;
while(!q.empty())
{
cur = q.front();
q.pop();
for(int k=;k<;k++)
{
next.row=cur.row+dir[k][];
next.col = cur.col+dir[k][];
if(!vis2[next.row][next.col]&&judge(next))
{
next.step=cur.step+;
vis2[next.row][next.col]=true;
q.push(next);
if(a[next.row][next.col]=='A')
dist[next.row][next.col]=next.step;
}
}
}
for(int j=;j<=nums;j++)
{
int d = dist[node[j].row][node[j].col];
edge[i][j]=d;
edge[j][i]=d;
}
}
int prim()
{
bool vis[maxs];
memset(vis,false,sizeof(vis));
vis[]=true;
int dist[maxs],ans=;
for(int i=;i<=nums;i++)
dist[i]=edge[][i];
for(int i=;i<=nums;i++)
{
int mins = INF,k=;
for(int j=;j<=nums;j++)
if(!vis[j]&&dist[j]<mins)
{
mins = dist[j];
k=j;
}
if(mins!=INF)
ans+=mins;
vis[k]=true;
for(int j=;j<=nums;j++)
if(!vis[j]&&dist[j]>edge[k][j])
dist[j]=edge[k][j];
}
return ans;
}
int main()
{
freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
nums=;
memset(node,,sizeof(node));
memset(a,,sizeof(a));
scanf("%d%d",&col,&row);
char s[];
for(int i=;i<=row;i++)
{
gets(s);
for(int j=;j<=col;j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='S')
{
node[].row=i;node[].col=j;
}
else if(a[i][j]=='A')
{
node[++nums].row=i;node[nums].col=j;
}
}
}
for(int i=;i<=nums;i++)
{
edge[i][i]=;
bfs(i);
}
printf("%d\n",prim());
}
return ;
}
poj3026的更多相关文章
- POJ3026 最小生成树
问题: POJ3026 分析: 采用BFS算出两两之间的距离,再用PRIM算法计算最小生成树. AC代码: //Memory: 220K Time: 32MS #include <iostrea ...
- POJ-3026 Borg Maze---BFS预处理+最小生成树
题目链接: https://vjudge.net/problem/POJ-3026 题目大意: 在一个y行 x列的迷宫中,有可行走的通路空格' ',不可行走的墙'#',还有两种英文字母A和S,现在从S ...
- POJ-3026(图上的最小生成树+prim算法+gets函数使用)
Borg Maze POJ-3026 一开始看到这题是没有思路的,看了题解才知道和最小生成树有关系. 题目的意思是每次走到一个A或者S就可以分为多个部分继续进行搜索.这里就可以看出是从该点分出去的不同 ...
- poj3026(bfs+prim)
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...
- POJ3026——Borg Maze(BFS+最小生成树)
Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...
- POJ3026 Borg Maze(最小生成树)
题目链接. 题目大意: 任意两点(点表示字母)可以连线,求使所有点连通,且权值和最小. 分析: 第一感觉使3维的BFS.但写着写着,发现不对. 应当用最小生成树解法.把每个字母(即A,或S)看成一个结 ...
- POJ3026(BFS + prim)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10554 Accepted: 3501 Descri ...
- 快速切题 poj3026
感受到出题人深深的~恶意 这提醒人们以后...数字后面要用gets~不要getchar 此外..不要相信那个100? Borg Maze Time Limit: 1000MS Memory Lim ...
- POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14165 Accepted: 4619 Descri ...
随机推荐
- CentOS7下BIND配置主从服务器和缓存服务器
系统环境:CentOS Linux release 7.4.1708 (Core) 3.10.0-693.el7.x86_64 软件版本:bind-chroot-9.9.4-51.el7_4.1.x ...
- 对FPKM/RPKM以及TPM的理解
对FPKM/RPKM以及TPM的理解 2018年07月03日 16:05:53 sixu_9days 阅读数:559 标签: FPKM/RPKMTPMRNA-Seq 更多 个人分类: RNA-Seq ...
- BZOJ 2726 [SDOI2012] 任务安排 - 斜率优化dp
题解 转移方程与我的上一篇题解一样 : $S\times sumC_j + F_j = sumT_i \times sumC_j + F_i - S \times sumC_N$. 分离成:$S\t ...
- ubuntu关闭防火墙
https://jingyan.baidu.com/article/73c3ce283ee2c1e50343d9f6.html
- 参看dll参数类型
http://blog.csdn.net/chinabinlang/article/details/7698459 验证
- 安卓编译 translate error Lint: How to ignore “<key> is not translated in <language>” errors?
Add following at the header of your strings.xml file <resources xmlns:tools="http://schemas. ...
- STAX项目结束总结
STAX:Support Taxonomy Management Console. 使用了MVC+WCF+jQuery+Azman.msc(权限控制)+kendoUI+SQL SERVER 2012
- 2018.08.29 NOIP模拟 movie(状压dp/随机化贪心)
[描述] 小石头喜欢看电影,选择有 N 部电影可供选择,每一部电影会在一天的不同时段播 放.他希望连续看 L 分钟的电影.因为电影院是他家开的,所以他可以在一部电影播放过程中任何时间进入或退出,当然他 ...
- time & datetime 模块
在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime,calendar(很少用,不讲),下面分别来介绍. 在开始之前,首先要说明几点: 一 ...
- Mongodb 存储日志信息
线上运行的服务会产生大量的运行及访问日志,日志里会包含一些错误.警告.及用户行为等信息,通常服务会以文本的形式记录日志信息,这样可读性强,方便于日常定位问题,但当产生大量的日志之后,要想从大量日志里挖 ...