Borg Maze
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12944 | Accepted: 4224 |
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
题意:给你一个n*m的迷宫(n列m行),可以上下左右的走,只能走空格或字母,求出将所有字母连通起来的最小耗费。
思路:用最小生成树的思想的话,S点和A点就一样了,因此无论哪个点做起点都是一样的,(通常选取第一个点),因此起点不是S也没有关系。所以所有的A和S都可以一视同仁,看成一模一样的顶点就可以了。先用Bfs先跑出各个顶点到其他点的距离, 然后再用Kruskal求最小生成树。这题需要注意的一点,discuss里面有说到,测试数据每行后面有很多空格,空格位于"6 5 ",得丢弃,不能用getchar(),要用gets()吃掉,数组也要开大一点。
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; const int maxn = 100000; const int N = 1005; int map[N][N]; int father[N]; struct Edge { int u,v; int quanzhi; } edge[maxn]; struct ee { int x,y; int step; }; int move[4][2]= {{1,0},{-1,0},{0,1},{0,-1}}; int n,m; int vis[N][N]; int k; int judge(int x,int y) { if(x>=0&&x<n&&y>=0&&y<m) return 1; return 0; } //并查集压缩路劲 int find(int x) { if(x!=father[x]) father[x]=find(father[x]); return father[x]; } int merge(int v,int u){ int t1,t2; t1=find(v); t2=find(u); if(t1!=t2){ father[t2]=t1; return 1; } return 0; } bool cmp(Edge a,Edge b) { return a.quanzhi<b.quanzhi; } void bfs(int x,int y) { queue<ee>q; while(!q.empty()) q.pop(); ee cur,next; cur.x=x; //生成树顶点 cur.y=y; cur.step=0; q.push(cur); memset(vis,0,sizeof(vis)); vis[x][y]=1; //不断找A(S) while(!q.empty()) { cur=q.front(); q.pop(); for(int i=0; i<4; i++) { int xx=cur.x+move[i][0]; int yy=cur.y+move[i][1]; if(!judge(xx,yy)||map[xx][yy]<0||vis[xx][yy]) continue; //next存下一步 next.x=xx; next.y=yy; next.step=cur.step+1; vis[xx][yy]=1; if(map[xx][yy]>=1) { edge[k].u=map[x][y]; //第k条边的第一个点 edge[k].v=map[xx][yy];//第k条边的第二个点 edge[k].quanzhi=next.step; k++; } q.push(next); } } } int main() { int t,i,j; scanf("%d",&t); while(t--) { scanf("%d%d",&m,&n); char s[1000]; gets(s); int num=0; k=0; char c; for(i=0; i<n; i++) { for(j=0; j<m; j++) { scanf("%c",&c); if(c=='#') map[i][j]=-1; else if(c==' ') map[i][j]=0; else map[i][j]=++num; //出现的S或A依次递增赋值 } getchar(); } for(i=0; i<n; i++) for(j=0; j<m; j++) if(map[i][j]>0)//不管从那个点(S或A)开始搜寻都没问题 bfs(i,j); for(i=0; i<=num; i++) father[i]=i; //Kruskal算法核心 sort(edge,edge+k,cmp); int ans=0; for(i=0;i<k;i++){ if(merge(edge[i].u,edge[i].v)) { ans+=edge[i].quanzhi; } } printf("%d ",ans); } return 0; }