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



題意:

給出一個迷宮,‘#’是牆壁,‘ ’(空格)可走,‘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;
}



發佈了119 篇原創文章 · 獲贊 10 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章