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 ...
随机推荐
- conn not captured
线程 和 事件中 变量 not captured 把变量定义为 static 或者添加为全局变量(放在main之前)
- 设计规范VS设计创造力,谁更胜一筹?
设计规范和设计创造力哪个更重要?这是一个颇具争议性的话题.如果是3年前问我这个问题我会毫不犹豫的选择设计创造力,毫无疑问,一个好的设计创造力真的是可以让人像打了鸡血一样疯狂. 原来在上大学的时候,我就 ...
- c++11多线程学习笔记之三 condition_variable使用
从windows角度来说,condition_variable类似event. 阻塞等待出发,不过condition_variable可以批量出发. 代码如下: // 1111111.cpp : 定义 ...
- jquery 元素筛选 13.6.20
<ul> <li>list item 1</li> <li>list item 2</li> <li class="thir ...
- spring-boot @Async 的使用、自定义Executor的配置方法
1. TaskExecutor Spring异步线程池的接口类,其实质是java.util.concurrent.Executor Spring 已经实现的异常线程池: 1. SimpleAsyncT ...
- 客户被绑,蒙眼,惊问:“想干什么?” 对方不语,鞭笞之,客户求饶:“别打,要钱?” 又一鞭,“十万够不?” 又一鞭,“一百万?” 又一鞭。客户崩溃:“你们TMD到底要啥?” “要什么?...
1. 客户被绑,蒙眼,惊问:“想干什么?” 对方不语,鞭笞之,客户求饶:“别打,要钱?” 又一鞭,“十万够不?” 又一鞭,“一百万?” 又一鞭.客户崩溃:“你们TMD ...
- 2018.10.19 NOIP训练 yk赚钱记(01分数规划)
传送门 其实是一个裸的最优比率生成树. 注意精度的控制就行了. 代码
- 一个 图片 滚动 飞入的css特效
@keyframes bounceInLeft { from, 60%, 75%, 90%, to {animation-timing-function: cubic-bezier(0.215, 0. ...
- MATLAB实现最优低通滤波器的函数
MATLAB实现最优低通滤波器的函数 % Fs --Data rate % Fpass --pass band % Fstop --Cutoff frequencies % Apass ...
- Linux上查看造成IO高负载的进程
方法1:使用iotop工具这是一个python脚本工具,使用方法如:iotop -o方法2:使用工具dmesg使用dmesg之前,需要先开启内核的IO监控:echo 1 >/proc/sys/v ...