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 ...
随机推荐
- json.dumps错误:'utf8' codec can't decode byte解决方案-乾颐堂
一次在使用json.dumps()过程中,出现错误提示: ERROR:"UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in ...
- g++报错原因分析:expected class-name before ‘{’ token
今天写程序的时候, 遇到这样一个错误expected class-name before ‘{’ token 最后发现原来是我的头文件声明没有加. 继承时不要忘记加基类的头文件 错误: class F ...
- 构造函数constructor 与析构函数destructor(四)
拷贝构造函数:拷贝构造函数就是在用一个类对象来创建另外一个类对象时被调用的构造函数,如果我们没有显示的提供拷贝构造函数,编译器会隐式的提供一个默认拷贝构造函数. 拷贝构造函数的定义是X(const X ...
- 2018.08.04 洛谷P3380 【模板】二逼平衡树(树套树)
传送门 技不如人,写的权值线段树套线段树在bzoj上无论如何都卡不过空间. 这是一道树套树简单题,感觉没什么好说的. 直接权值线段树套平衡树就行了. 代码: #include<bits/stdc ...
- linux系统编程之进程(五):exec系列函数(execl,execlp,execle,execv,execvp)使用
本节目标: exec替换进程映像 exec关联函数组(execl.execlp.execle.execv.execvp) 一,exec替换进程映像 在进程的创建上Unix采用了一个独特的方法,它将进程 ...
- Linux下安装配置 Jdk1.6+Tomcat6+Apache2.2.x+jk_mod1.2 详解
本篇以Redhat AS5,内核为Linux 2.6.18-8.el5 为例,其中Redhat/Fedora系列基本一致,其他Linux或者版本均可以参考. STEP 1 软件下载:1. jdk1.6 ...
- public class 和class 的区别
Java在编写类的时候可以使用两种方式定义类: public class定义类: class定义类: 如果一个类声明的时候使用了public class进行了声明,则类名称必须与 ...
- Getting Started with Google Tango(Google Tango开始教程)
https://developers.google.com/tango/ Build apps that understand space and motion in high fidelity on ...
- SPFA穿越虫洞——负权回路得判断
poj3259 题目大意:穿越虫洞可以回到过去(时间--)所以能不能让时间倒流呢,就是判断有没有负权回路这次尝试用SPFA算法,也可以复习一下链式前向星 准备工作,队列q,spfa算法得有点就在于这个 ...
- Android-天气预报Demo-JSON数据解析
在上两篇博客,Android-解析JSON数据(JSON对象/JSON数组),Android-Gson解析JSON数据(JSON对象/JSON数组),是介绍了解析本地文件里面的JSON数据: Andr ...