POJ-3026_Borg Maze
Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
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
题意:给你一个地图,地图中A代表外星人,S代表出发点,从S点出发,每遇到一个A点便可分裂成多个。求把所有A都吃完需要多少步。
题解:这是一个DFS+最短路的问题,用DFS遍历求出每个点到其他点的最短距离,然后用prim算法。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;
const int INF = 1e9+7;
char s[55][55];
int f[55][55],m,n,num,mp[105][105];
struct node
{
int x,y;
int step;
}p[105];
int Next[4][2] = {-1,0,1,0,0,-1,0,1};
int judge(int x,int y)
{
if(x>=0&&y>=0&&x<n&&y<m)
return 1;
return 0;
}
void bfs(int x)
{
node t1,t2;
int ff[55][55],dx,dy,i,num1,num2;
memset(ff,0,sizeof(ff));
queue<node> q;
t1 = p[x];
num1 = f[t1.x][t1.y];
ff[t1.x][t1.y] = 1;
t1.step = 0;
q.push(t1);
while(!q.empty())
{
t1 = q.front();
//printf("%d %d\n",t1.x,t1.y);
q.pop();
for(i=0;i<4;i++)
{
dx = t1.x + Next[i][0];
dy = t1.y + Next[i][1];
if(judge(dx,dy)&&s[dx][dy]!='#'&&!ff[dx][dy])
{
t2.x = dx;
t2.y = dy;
ff[dx][dy] = 1;
t2.step = t1.step + 1;
q.push(t2);
if(f[t2.x][t2.y]!=-1)
{
num2 = f[t2.x][t2.y];
//printf("%d\n",t2.step);
//printf("%d %d\n",num1,num2);
if(mp[num1][num2]>t2.step)
mp[num1][num2] = t2.step;
}
}
}
}
}
void prim()
{
int i,j,k,MIN,sum = 0;
int f[105],dis[105];
for(i=0;i<num;i++)
{
dis[i] = mp[0][i];
f[i] = 0;
}
f[0] = 1;
for(i=0;i<num;i++)
{
MIN = INF;
k = -1;
for(j=0;j<num;j++)
{
if(!f[j]&&dis[j]<MIN)
{
MIN = dis[j];
k = j;
}
}
//printf("%d\n",k);
if(MIN == INF)
break;
f[k] = 1;
sum += MIN;
for(j=0;j<num;j++)
{
if(!f[j]&&dis[j]>mp[k][j])
dis[j] = mp[k][j];
}
}
printf("%d\n",sum);
}
int main()
{
int t,i,j;
char a[105];
scanf("%d",&t);
while(t--)
{
memset(f,-1,sizeof(f));
num = 0;
for(i=0;i<=100;i++)
for(j=0;j<=100;j++)
mp[i][j] = (i==j)?0:INF;
scanf("%d%d",&m,&n);
gets(a);/*不知道为什么,getchar会WA*/
for(i=0;i<n;i++)
{
gets(s[i]);
for(j=0;j<m;j++)
{
if(s[i][j]=='A'||s[i][j]=='S')
{
p[num].x = i;
p[num].y = j;
f[i][j] = num++;
}
}
}
for(i=0;i<num;i++)
bfs(i);
// for(i=0;i<num;i++)
// {
// for(int j=0;j<num;j++)
// printf("%d ",mp[i][j]);
// printf("\n");
// }
prim();
}
return 0;
}