POJ 3026 Borg Maze & UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树)

http://poj.org/problem?id=3026

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1248


Borg Maze

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8498   Accepted: 2862

Description

The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual is linked to the collective by a sophisticated
subspace network that insures each member is given constant supervision and guidance.

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

On the first line of input there is one integer, N <= 50, giving the number of test cases in the input. Each test case starts with a line containg two integers x, y such that 1 <= x,y <= 50. After this, y lines follow, each which x characters. For each character,
a space `` ‘‘ stands for an open space, a hash mark ``#‘‘ stands for an obstructing wall, the capital letter ``A‘‘ stand for an alien, and the capital letter ``S‘‘ stands for the start of the search. The perimeter of the maze is always closed, i.e., there
is no way to get out from the coordinate of the ``S‘‘. At most 100 aliens are present in the maze, and everyone is reachable.

Output

For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.

Sample Input

2
6 5
#####
#A#A##
# # A#
#S  ##
#####
7 7
#####
#AAA###
#    A#
# S ###
#     #
#AAA###
#####

Sample Output

8
11

Source

Svenskt M?sterskap i Programmering/Norgesmesterskapet 2001

题意:

给出一个迷宫,‘#’是墙壁,‘ ’(空格)可走,‘S’是起点,‘A’是目标,一个群体从S点开始,每次可以走周围相邻的4个格子,走到某个目标的花费是从上一个目标(或起点)开始计算的步数,群体可且仅可在S或A出分成若干个(可以看成是无数个,即使在同一个格子中)群体。比如从S开始走5步到A1,在A1分成两个群体,其中一个到达A2走3步,另一个到达A3也走3步,那么总花费是5+3+3=11。求到达所有A的最小花费。

分析:

题意很难理解,其实就是个最小生成树,用BFS在平面内模拟prim算法即可,这里要用到优先队列,因为到达某个A后就相当于步数清0,每次选走过步数最少的扩展状态,注意格子是可以重复走的,但只有步数比之前到达这个格子的步数少才走。

POJ数据好恶心,每行后面还有一堆空格,UVA就没这种情况。

/*
 *
 *	Author	:	fcbruce
 *
 *	Date	:	2014-08-11 15:26:39
 *
 */
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define maxm
#define maxn 

using namespace std;

struct node
{
	int x,y,w;
	bool operator < (const node &n)const
	{
		return w>n.w;
	}
};

int n,m,total;
char G[101][101];
char buf[101];
int vis[101][101];
int sx,sy;

int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};

int bfs()
{
	int res=0;
	priority_queue<node >	q;
	q.push( (node){sx,sy,0});
	memset(vis,0x3f,sizeof vis);
	vis[sx][sy]=0;

	while (!q.empty() || total)
	{
		node s=q.top();q.pop();

		if (G[s.x][s.y]=='A')
		{
			res+=s.w;
			s.w=0;
			G[s.x][s.y]=' ';
			total--;
		}

		for (int i=0;i<4;i++)
		{
			int tx=s.x+dx[i];
			int ty=s.y+dy[i];
			int tw=s.w+1;
			if (G[tx][ty]!='#' && vis[tx][ty]>tw)
			{
				q.push( (node){tx,ty,tw});
				vis[tx][ty]=tw;
			}
		}
	}

	return res;
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("/home/fcbruce/文档/code/t","r",stdin);
	#endif // ONLINE_JUDGE

	int T_T;
	scanf( "%d",&T_T);

	while (T_T--)
	{
		scanf( "%d %d",&m,&n);gets(buf);
		for (int i=0;i<n;i++)
			gets(G[i]);

		total=0;

		for (int i=0;i<n;i++)
		{
			for (int j=0;j<m;j++)
			{
				if (G[i][j]=='S')
				{
					sx=i;sy=j;
				}
				if (G[i][j]=='A')
					total++;
			}
		}

		printf( "%d\n",bfs());
	}

	return 0;
}

POJ 3026 Borg Maze & UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树)

时间: 2024-10-13 21:29:55

POJ 3026 Borg Maze & UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树)的相关文章

POJ 3026 Borg Maze

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7998   Accepted: 2675 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to descr

poj 3026 Borg Maze (bfs + 最小生成树)

链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走),空格代表空位(可走),S代表搜索起点(可走) A代表外星人站(可走),现在要从S出发,将S和所有的A之间都连通,求路线总距离最小值 分析:可以先用bfs将所有的A,S两两之间的最短距离,题目的目的是将S与所有的A连通,使得总距离最小, 所有任选一点开始按最小生成树的算法做就行,并非非要从S点开始 注:题目输入x,y后可能有很多空格,可以用gets将多余的空格取走,开数组是尽量开大点,之前虽然开的比题目数据     稍大,但一

【POJ 3026】Borg Maze

[POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队可以无限分割 问搜索到所有alien所需要的总步数 即求一个无向图 包含所有的点并且总权值最小(最小生成树 BFS+最小生成树 Prim/Kruskal-懒死了 就这么贴吧--凑活看( ̄┰ ̄*) #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue&

POJ - 3026 Borg Maze BFS加最小生成树

Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算读懂了题目,也比较难想到这用到了最小生成树的知识,因为可以分身,所以每个点可以向其他点都连上边.可以用bfs预处理出所有的S点,A点的连线,再跑一遍最小生成树,即可得出答案.这里有几点注意,一开始我bfs没有记录step,而是直接找到一点后算曼哈顿距离,这是不对的,因为可能是绕了一个圈到了这个点.还

POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)

( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<queue> using namespace std; const int INF=10e8; const int MAXN=55; int col,row,k,minn; char str[MAXN][MAXN]

poj 3026

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8383   Accepted: 2813 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to descr

POJ3026——Borg Maze(BFS+最小生成树)

Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual is linked

uva 12206 - Stammering Aliens(哈希)

题目链接:uva 12206 - Stammering Aliens 题目大意:给出一个字符串,找出至少出现m次的最长子串. 解题思路:哈希算法,将每个后缀数组建立一个哈希值,每次二分长度判断,每次判断时将哈希值排序,计数即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef unsigned long long ll; const int ma

UVA 12206 - Stammering Aliens(后缀数组)

UVA 12206 - Stammering Aliens 题目链接 题意:给定一个序列,求出出现次数大于m,长度最长的子串的最大下标 思路:后缀数组,搞出height数组后,利用二分去查找即可 这题之前还写过hash的写法也能过,不过写后缀数组的时候,犯了一个傻逼错误,把none输出成node还一直找不到...这是刷题来第二次碰到这种逗比错误了,还是得注意.. 代码: #include <cstdio> #include <cstring> #include <algori