poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
最小生成树问题,但较不同点的是没有给原图,所以要用BFS找各个字母之间的距离,即构成原图
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<queue>
#define INF 0x3f3f3f3f
#define max(a, b)(a > b ? a : b)
#define min(a, b)(a < b ? a : b)
#define N 110 using namespace std; struct node
{
int x, y, step;
}; int G[N][N], dist[N], p[N][N];//p记录字母的坐标
bool vis[N], use[N][N];
char maps[N][N];
int m, n, num;
int d[][] = {{, }, {, }, {-, }, {, -}}; void Init()
{
int i, j;
memset(p, , sizeof(p));
memset(vis, false, sizeof(vis));
for(i = ; i <= num ; i++)
{
for(j = ; j <= num ; j++)
{
if(i == j)
G[i][j] = ;
else
G[i][j] = G[j][i] = INF;
}
}
} int prim(int s)//最小生成树,求最小花费
{
int i, j, index, Min, ans = ;
for(i = ; i <= num ; i++)
dist[i] = G[s][i];
vis[s] = true;
for(i = ; i < num ; i++)
{
Min = INF;
for(j = ; j <= num ; j++)
{
if(!vis[j] && dist[j] < Min)
{
Min = dist[j];
index = j;
}
}
ans += Min;
vis[index] = true;
for(j = ; j <= num ; j++)
{
if(!vis[j] && dist[j] > G[index][j])
dist[j] = G[index][j];
}
}
return ans;
} void BFS(int x, int y)//广搜构建原图
{
queue<node>Q;
int i, a, b;
node now, next;
memset(use, false, sizeof(use));
now.x = x;
now.y = y;
now.step = ;
use[x][y] = true;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
if(p[now.x][now.y] > )//该点为字母
G[p[x][y]][p[now.x][now.y]] = now.step;
for(i = ; i < ; i++)
{
a = next.x = now.x + d[i][];
b = next.y = now.y + d[i][]; if(a >= && a < m && b >= && b < n && !use[a][b] && maps[a][b] != '#')
{
next.step = now.step + ;
use[a][b] = true;
Q.push(next); }
}
}
} int main()
{
int i, j, t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d ", &n, &m);
Init();
num = ;
for(i = ; i < m ; i++)
{
for(j = ; j < n ; j++)
{
scanf("%c", &maps[i][j]);
}
getchar();
}
for(i = ; i < m ; i++)
{
for(j = ; j < n ; j++)
{
if(maps[i][j] == 'A' || maps[i][j] == 'S')
p[i][j] = ++num;//统计字母的个数,即要进入树的点的个数
}
}
for(i = ; i < m ; i++)
{
for(j = ; j < n ; j++)
if(p[i][j] > )
BFS(i, j);
}
printf("%d\n", prim());
}
return ;
}
poj 3026 Borg Maze (BFS + Prim)的更多相关文章
- POJ - 3026 Borg Maze BFS加最小生成树
		
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
 - poj 3026 Borg Maze (bfs + 最小生成树)
		
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
 - POJ 3026 Borg Maze(Prim+bfs求各点间距离)
		
题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...
 - POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)
		
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
 - POJ 3026 Borg Maze    bfs+Kruskal
		
题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...
 - POJ - 3026  Borg Maze  bfs+最小生成树。
		
http://poj.org/problem?id=3026 题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地.求从S出发,经过所有A所需要的最短路.你有一个特殊能力,当走到 ...
 - poj 3026 Borg Maze bfs建图+最小生成树
		
题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...
 - 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
		
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
 - POJ 3026 Borg Maze(bfs+最小生成树)
		
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
 
随机推荐
- 【POJ】3523 The Morning after Halloween
			
1. 题目描述$m \times n$的迷宫(最大为$16 \times 16$)包含最多3个点,这些点可以同时向相邻方向移动或保持停止,使用小写字母表示起始位置,使用大写字母表示中止位置.求最少经过 ...
 - MyEclipse开发WebService教程
			
. 创建一个 webService 工程. 2. 创建一个普通 Java 类 3. 创建 webService 服务端 HelloJaxwsDelegate.java 的源代码如下: 4. 导 ...
 - [Codeforces676B]Pyramid of Glasses(递推,DP)
			
题目链接:http://codeforces.com/problemset/problem/676/B 递推,dp(i, j)表示第i层第j个杯子,从第一层开始向下倒,和数塔一样的题.每个杯子1个时间 ...
 - input默认提示取消
			
input 输入框有提示功能,当你之前输入过一些内容,你下次打入相关字符的时候,默认会有之前输入的一些相关的字符的提示,这个提示一般来说还是很好的,但是,有时候,我们想自己输入,不想要提示. 如果不需 ...
 - 新浪实时股票数据接口http://hq.sinajs.cn/list=code
			
股票数据的获取目前有如下两种方法可以获取:1. http/javascript接口取数据2. web-service接口 1.http/javascript接口取数据1.1Sina股票数据接口以大秦铁 ...
 - jquery在线教程
			
http://www.runoob.com/jquery/jquery-slide.htmlhttp://www.w3school.com.cn/jquery/http://www.phpstudy. ...
 - poj 3101 Astronomy (java  分数的最小公倍数 gcd)
			
题目链接 要用大数,看了别人的博客,用java写的. 题意:求n个运动周期不完全相同的天体在一条直线上的周期. 分析:两个星球周期为a,b.则相差半周的长度为a*b/(2*abs(a-b)),对于n个 ...
 - UVa 11992 (线段树 区间修改) Fast Matrix Operations
			
比较综合的一道题目. 二维的线段树,支持区间的add和set操作,然后询问子矩阵的sum,min,max 写完这道题也是醉醉哒,代码仓库里还有一份代码就是在query的过程中也pushdown向下传递 ...
 - POJ 1088 滑雪【记忆化搜索】
			
题意:给出一个二维矩阵,要求从其中的一点出发,并且当前点的值总是比下一点的值大,求最长路径 记忆化搜索,首先将d数组初始化为0,该点能够到达的路径长度保存在d数组中,同时把因为路径是非负的,所以如果已 ...
 - nginx - conf.d vs sites-available
			
自己理解: conf.d - 扩展配置文件,用户配置文件 sites-available - 配置 虚拟主机(nginx支持多个虚拟主机,sites-enabled(存放 软链接,指向sites-av ...