Borg Maze
POJ - 3026
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
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.
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
就是直接最小生成树的模板了嘿嘿~~~~就是题目我自己也看不懂嘻嘻哈哈哈
代码:
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
char maze[55][55];
int book[55][55];
int now = 0;
struct node
{
int x,y;
int s;
node(){s = 0;}
};
struct Node
{
int x,y;
int val;
}s[30000];
int n,m;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int pre[3000],vis[55],gg;
map<pair<int,int>,int>mp;
void BFS(int x,int y)
{
queue<node>qu;
node a;a.x= x;a.y = y;a.s = 0;
qu.push(a);
memset(book,0,sizeof(book));
book[x][y] =1;
pair<int ,int >pp;
while(qu.empty()==0)
{
node tmp = qu.front();
qu.pop();
for(int i = 0;i<4;i++)
{
node endd;
endd.x = tmp.x+dir[i][0];
endd.y = tmp.y+dir[i][1];
endd.s = tmp.s+ 1;
if(endd.x<1||endd.y<1||endd.x>m||endd.y>n)continue;
if(book[endd.x][endd.y]==1||maze[endd.x][endd.y]=='#')continue;
book[endd.x][endd.y] = 1;
qu.push(endd);
if(maze[endd.x][endd.y] == 'S'||maze[endd.x][endd.y]=='A')
{
pp.first = x;pp.second = y;
s[++now].x =mp[pp];
pp.first = endd.x;pp.second = endd.y;
s[now].y = mp[pp];
s[now].val = endd.s;
}
}
}
return ;
}
int find(int x)
{
if(x==pre[x])return x;
else return pre[x] = find(pre[x]);
}
bool merge(int x,int y)
{
int fx= find(x);
int fy = find(y);
if(fx!=fy){
pre[fx] = fy;
return true;
}
return false;
}
bool cmp(Node a,Node b){
return a.val < b.val;
}
int krasucal()
{
sort(s+1,s+1+now,cmp);
for(int i =1;i<=gg;i++)
pre[i] = i;
int ans = 0;
for(int i =1;i<=now;i++)
{
if(merge(s[i].x,s[i].y))
ans +=s[i].val;
}
return ans;
}
int main()
{
char str[150];
int t;scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
gets(str);//把多余的回车换行都读掉就可以了
mp.clear();
pair<int,int>pp;
gg = 0;
for(int i = 1; i<=m;i++)
{
gets(maze[i]+1);//读入操作完成
for(int j = 1;j<=n;j++)
{
if(maze[i][j]=='A'||maze[i][j]=='S')
{
pp = make_pair(i,j);
mp[pp] = ++gg;
}
}
}
now = 0 ;
for(int i =1;i<=m;i++)
{
for(int j =1;j<=n;j++)
{
if(maze[i][j]=='A'||maze[i][j]=='S')
BFS(i,j);
}
}
printf("%d\n",krasucal());
}
return 0;
}