POJ - 3062 Borg Maze

时间:2023-04-24 16:23:50

题目链接:http://poj.org/problem?id=3026

Svenskt Masterskap我程序员/ Norgesmesterskapet 2001

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

#####
#A#A##
# # A#
#S ##
##### #####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
分析:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<math.h>
#include<queue>
#include<map>
using namespace std; #define INF 0x3f3f3f3f
#define N 1200 int n,m,maps[N][N],b[N][N],dist[N],vis[N][N],viss[N];
char s[N][N];
int dir[][]={ {,},{,-},{,},{-,} }; struct node
{
int x,y,step;
}a[N]; void Init()
{
int i,j;
for(i=;i<m*n;i++)
dist[i]=INF;
for(j=;j<n*m;j++)
maps[i][j]=(i==j)?:INF;
} void bfs(int ss,int x,int y)
{
int i; memset(vis,,sizeof(vis));
queue<node>Q;
node B,Next,Now;
B.x=x;
B.y=y;
B.step=;
Q.push(B);
vis[x][y]=; while(Q.size())
{
Now=Q.front();
Q.pop(); if(s[Now.x][Now.y]>='A'&&s[Now.x][Now.y]<='Z')
maps[ss][b[Now.x][Now.y]]=Now.step; for(i=;i<;i++)
{
Next.x=Now.x+dir[i][];
Next.y=Now.y+dir[i][];
if(Next.x<m&&Next.x>=&&Next.y<n&&Next.y>=&&vis[Next.x][Next.y]==&&s[Next.x][Next.y] != '#')
{
vis[Next.x][Next.y]=;
Next.step=Now.step+;
Q.push(Next);
}
}
}
} int Prim(int e)
{
int i,j;
viss[]=;
for(int i=;i<=e;i++)
dist[i]=maps[][i];
int sum=;
for(i=;i<=e;i++)
{
int Min=INF,index=-; for(j=;j<=e;j++)
if(!viss[j]&&Min>dist[j])
{
Min=dist[j];
index=j;
}
if(index==-)break;
sum+=Min;
viss[index]=; for(j=;j<=e;j++)
if(!viss[j]&&dist[j]>maps[index][j])
dist[j]=maps[index][j];
} return sum;
} int main()
{
int T,i,j; scanf("%d", &T); while(T--)
{
memset(a,,sizeof(a));
memset(viss,,sizeof(viss)); scanf("%d%d ", &n,&m);
Init(); int cnt=;
for(i=;i<m;i++)
{
gets(s[i]);
for(j=;j<n;j++)
{
if(s[i][j]<='Z'&&s[i][j]>='A')
b[i][j]=cnt++;///记录cnt号动点位置
}
} for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
if(s[i][j]<='Z'&&s[i][j]>='A')
bfs(b[i][j],i,j);///搜索,记录各个点之间步数
}
} int ans=Prim(cnt-);///最小生成树求最终值
printf("%d\n", ans);
}
}