您的位置:首页 > 其它

POJ 3026 Borg Maze 图论 prim算法(最小生成树)+BFS算法(广度优先搜索)

2013-09-05 23:24 435 查看
Borg Maze
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 7151Accepted: 2405
DescriptionThe 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 linkedto 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 isthat 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 costof 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.InputOn 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 whichx 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 mazeis 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.OutputFor 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了,但是,主页君搜索实在木有怎么看过,就立刻翻书学习BFS和DFS,这道题具体思路就是对于这张迷宫,从任意一个出发点或者外星人窝藏的点出发,用DFS搜索图表,值就等于上一个节点的值+1,然后得到图中的值,这个具体方法可以百度,是BFS算法最基础的一部分,然后绘制出图以后,将图带入prim算法求出最小生成树的最小权值,输出这个权值即可AC。题目有丝进阶,主要利用了多种算法。
下面是AC代码:
#include<cstdio>#include<iostream>#include<queue>#include<cstring>using namespace std;#define max_vertexes 105int point[55][55],G[105][105];int dist[55][55];int m,n;void bfs(int a,int b){int x,y,flag[55][55];queue<int> quex;queue<int> quey;quex.push(a);quey.push(b);memset(dist,0,sizeof(dist));memset(flag,0,sizeof(flag));flag[a][b]=1;while(quex.empty()==0){x=quex.front();y=quey.front();flag[x][y]=1;quex.pop();quey.pop();if(point[x][y]>0){G[point[a][b]][point[x][y]]=dist[x][y];G[point[x][y]][point[a][b]]=dist[x][y];}if(x-1>=0&&point[x-1][y]>=0&&flag[x-1][y]==0){dist[x-1][y]=dist[x][y]+1;flag[x-1][y]=1;quex.push(x-1);quey.push(y);}if(x+1<n&&point[x+1][y]>=0&&flag[x+1][y]==0){dist[x+1][y]=dist[x][y]+1;flag[x+1][y]=1;quex.push(x+1);quey.push(y);}if(y-1>=0&&point[x][y-1]>=0&&flag[x][y-1]==0){dist[x][y-1]=dist[x][y]+1;flag[x][y-1]=1;quex.push(x);quey.push(y-1);}if(y+1<m&&point[x][y+1]>=0&&flag[x][y+1]==0){dist[x][y+1]=dist[x][y]+1;flag[x][y+1]=1;quex.push(x);quey.push(y+1);}}return;}int prim(int vcount){int i,j,k,sum=0;int lowcost[max_vertexes],closeset[max_vertexes],used[max_vertexes];for (i=1;i<=vcount;i++){lowcost[i]=G[1][i];closeset[i]=1;used[i]=0;}used[1]=1;for (i=1;i<vcount;i++){j=1;while (used[j]) j++;for (k=1;k<=vcount;k++)if ((!used[k])&&(lowcost[k]<lowcost[j])) j=k;sum+=G[j][closeset[j]];used[j]=1;for (k=1;k<=vcount;k++)if (!used[k]&&(G[j][k]<lowcost[k])){ lowcost[k]=G[j][k];closeset[k]=j; }}return sum;}int main(){int t,i,j,num;char ch[55][55];cin>>t;while(t--){scanf("%d%d\n",&m,&n);for(i=0;i<n;i++)gets(ch[i]);memset(point,0,sizeof(point));memset(G,0,sizeof(G));num=1;for(i=0;i<n;i++)for(j=0;j<m;j++){if(ch[i][j]=='S'||ch[i][j]=='A'){point[i][j]=num;num++;}else if(ch[i][j]=='#')point[i][j]=-1;}for(i=0;i<n;i++)for(j=0;j<m;j++)if(point[i][j]>0)bfs(i,j);printf("%d\n",prim(num-1));}return 0;}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: