Borg Maze
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 13260 | Accepted: 4330 |
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
题解:
题意是给你一个m*n的平面图,然后#代表墙,不能走;空格代表可以通过,然后S和A代表起点和要遍历的点,走过的路不再计算路程,然后求最小多少步遍历所有要遍历的点A。
首先bfs遍历点,然后标记所有有字母的点,然后对这些点进行建图。最后跑prim,因为,我们发现,其经过的边不再算入总答案,所以,易得最小生成树。
ps:这个题的bfs真恶心。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int inf=2501;
char map[60][60];
bool vis[60][60], used[150];
int d[150][150], dis[150], ss[55][55], cnt = 0;
int edge[150][150];
int xx[5] = {0, 0, 1, -1};
int yy[5] = {1, -1, 0, 0};
int n, m;
struct qer
{
int x, y;
};
queue <qer> q;
void bfs(int i,int j)
{
memset(d,0,sizeof(d));
memset(vis,false,sizeof(vis));
vis[i][j]=true;
q.push((qer){i, j});
while(!q.empty())
{
int x = q.front().x;
int y = q.front().y;
q.pop();
if(ss[x][y])
edge[ ss[i][j] ][ ss[x][y] ] = d[x][y]; //抽取字母到字母的边权
for(int k=0;k<4;k++)
{
int mx=x+xx[k];
int my=y+yy[k];
if(mx>=1 && mx<= m && my>=1 && my<=n)
if(!vis[mx][my] && map[mx][my]!='#')
{
q.push((qer){mx, my});
vis[mx][my]=true;
d[mx][my]=d[x][y]+1;
}
}
}
return;
}
int prim(void)
{
int s=1;
int mm=1;
used[s]=true;
int min_w;
int prim_w=0;
int point;
for(int i=1;i<=cnt;i++)
{
dis[i]=inf;
used[i]=false;
}
while(true)
{
if(mm==cnt)
break;
min_w=inf;
for(int i=2;i<=cnt;i++)
{
if(!used[i] && dis[i]>edge[s][i])
dis[i] = edge[s][i];
if(!used[i] && min_w>dis[i])
{
min_w=dis[i];
point=i;
}
}
s=point;
used[s]=true;
prim_w+=min_w;
mm++;
}
return prim_w;
}
int main()
{
int t;
cin>>t;
while(t--)
{
memset(ss, 0, sizeof(ss));
cnt = 0;
cin>>n>>m;
char temp[51];
gets(temp);
for(int i = 1;i <= m;i ++)
{
gets(map[i]);
for(int j = 1;j <= n;j ++)
if(map[i][j] == 'A' || map[i][j] == 'S')
ss[i][j] = ++cnt;
}
for(int i = 1;i <= m;i ++)
for(int j = 1;j <= n;j ++)
if(ss[i][j])
bfs(i, j);
cout<<prim()<<endl;
}
return 0;
}