Borg Maze
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12536 | Accepted: 4107 |
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 BFS和最小生成树的结合,输入数据里面有空格,所以要注意数据的接收姿势,我就是写出来之后找了半天BUG才发现在数据的接收上。#include<iostream> #include<cstdio> #include<cstring> #include<queue> #define inf 0x3f3f3f3f using namespace std; struct node { int x,y; }ren[1111]; struct Node { int xxx,yyy; int sp; }t,tt; char mp[55][55]; int a[110][110]; bool vis[110][110]; int dis[110]; int X,Y; int n,m; int xx[]={0,0,1,-1}; int yy[]={1,-1,0,0}; int sum; queue<Node>Q; int bfs(int i,int j) { memset(vis,false,sizeof(vis)); while(!Q.empty()) Q.pop(); t={ren[i].x,ren[i].y,0}; Q.push(t); vis[ren[i].x][ren[i].y]=true; while(!Q.empty()) { tt=Q.front(); Q.pop(); if(tt.xxx==ren[j].x&&tt.yyy==ren[j].y) { return tt.sp; } for(int i=0;i<4;i++) { int nx=tt.xxx+xx[i]; int ny=tt.yyy+yy[i]; if(nx>=1&&ny>=1&&nx<=n&&ny<=m&&mp[nx][ny]!='#'&&!vis[nx][ny]) { t={nx,ny,tt.sp+1}; Q.push(t); vis[nx][ny]=true; } } } return inf; } int main() { int t; cin>>t; while(t--) { cin>>m>>n; int ans=1; gets(mp[1]); for(int i=1;i<=n;i++) { char ss[666]; gets(ss); for(int j=0;j<m;j++) { if(ss[j]=='S') { X=i; Y=j+1; } if(ss[j]=='A') ren[ans++]={i,j+1}; } for(int j=m;j>0;j--) mp[i][j]=ss[j-1]; } ren[0]={X,Y}; for(int i=0;i<ans;i++) for(int j=0;j<=i;j++) { if(i!=j) a[i+1][j+1]=a[j+1][i+1]=bfs(i,j); else a[i+1][j+1]=0; } n=ans; sum=0; n=ans; for(int i=1;i<=n;i++) dis[i]=a[1][i]; for(int i=1;i<n;i++) { int mmin=inf,flag; for(int j=1;j<=n;j++) { if(dis[j]<mmin&&dis[j]) { mmin=dis[j]; flag=j; } } sum+=dis[flag]; for(int j=1;j<=n;j++) if(dis[j]>a[flag][j]) dis[j]=a[flag][j]; } cout<<sum<<endl; } return 0; }