POJ 3026 Borg Maze (bfs+最小生成树)
Borg Maze
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9177 | Accepted: 3067 |
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.
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
题意:在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度。
思路:由于使用prim算法求在最小生成树,因此无论哪个点做起点都是一样的,先bfs求 A 或 S 点最短路,最后跑个最小生成树就OK了。
代码:
View Code#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
const int N = 105;
char g[N][N];
int a[N][N],cost[N][N],t[N][N];
int move[][2]={{1,0},{-1,0},{0,1},{0,-1}};
void bfs(int sx, int sy)
{
queue<pair<int,int> >que;//pair别少了!!
while(!que.empty())que.pop();
memset(t,-1,sizeof(t));
t[sx][sy]=0;
que.push(make_pair(sx,sy));
while(!que.empty())
{
pair<int,int> now = que.front();
que.pop();
if(a[now.first][now.second]>=0)
cost[a[sx][sy]][a[now.first][now.second]]=t[now.first][now.second];
for(int i=0; i<4; i++)
{
int nx = now.first + move[i][0];
int ny = now.second + move[i][1];
if(g[nx][ny] == '#'||t[nx][ny]!=-1)continue;//遇到障碍或已经走过就退出
t[nx][ny]=t[now.first][now.second]+1;
que.push(make_pair(nx,ny));
}
}
}
const int INF=0x3f3f3f3f;
bool vis[N];
int lowc[N];
int Prim(int n)//点是0~n-1
{
int ans=0;
memset(vis,false,sizeof(vis));
vis[0]=true;
for(int i=1; i<n; i++)lowc[i]=cost[0][i];
for(int i=1; i<n; i++)
{
int minc=INF;
int p=-1;
for(int j=0; j<n; j++)
if(!vis[j]&&minc>lowc[j])
{
minc=lowc[j];
p=j;
}
if(minc==INF)return -1;//原图不连通
ans+=minc;
vis[p]=true;
for(int j=0; j<n; j++)
if(!vis[j]&&lowc[j]>cost[p][j])
lowc[j]=cost[p][j];
}
return ans;
}
int main()
{
// freopen("in.txt","r",stdin);
int t,n,m;
scanf("%d",&t);
while(t--)
{
int cnt = 0;
memset(a,-1,sizeof(a));
scanf("%d%d",&m,&n);
gets(g[0]);//原题后面有多余的空格..gets掉...原先我只getchar个换行符..一直wrong
for(int i=0; i<n; i++)
{
gets(g[i]);
for(int j=0; j<m; j++)
{
if(g[i][j] == 'A'||g[i][j] == 'S')
a[i][j] = cnt++;
}
}
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(a[i][j]>=0)
bfs(i,j);
printf("%d\n",Prim(cnt));
}
return 0;
}



浙公网安备 33010602011771号