您的位置:首页 > 其它

POJ-3026-Borg Maze(BFS+最小生成树)

2016-08-02 08:21 393 查看
Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12508 Accepted: 4100
DescriptionThe Borg is an immensely(极大地) powerful race of enhanced(提高) humanoids(类人动物) fromthe delta(三角洲) quadrant(象限) ofthe galaxy(银河). The Borg collective(集体的) isthe term used to describe the group consciousness(意识) of the Borg civilization(文明).Each Borg individual(个人的) is linked to the collective by a sophisticated(弄复杂) subspace(子空间) networkthat 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(最低的) costof scanning(扫描) a maze(迷宫) forthe assimilation(同化) ofaliens(外国人) hidingin the maze, by moving in north, west, east, and south steps. The tricky(狡猾的) thing is that the beginning of the search is conductedby a large group of over 100 individuals. Whenever an alien is assimilated(吸收), or at the beginning of the search, the group may splitin 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(包含) inthe 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 which x characters. For each character, a space `` '' stands for an open space, ahash mark ``#'' stands for an obstructing(妨碍) wall, the capital letter ``A'' stand for an alien, and the capital letter ``S'' standsfor 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(坐标) ofthe ``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
#include<string.h>#include<stdio.h>#include<queue>#include<algorithm>using namespace std;const int MAXN = 55;const int Max = 100010;const int INF = 0x3f3f3f;int n, m, t, top, num;char s[MAXN][MAXN];bool vis2[MAXN][MAXN];int tx[] = {0,1,0,-1};int ty[] = {-1,0,1,0};struct node{int x,y,step;}p,w, xt[MAXN*MAXN],Way[Max];bool cmp(node a, node b){if(a.step!=b.step)return a.step<b.step;if(a.x!=b.x)return a.x<b.x;return a.y<b.y;}int f(int x, int y){for(int i = 0;i<top;++i)if(xt[i].x==x&&xt[i].y==y)return i;return -1;}void BFS(int x, int y){memset(vis2,0,sizeof(vis2));queue<node>qq;p.x = x;p.y = y;p.step = 0;qq.push(p);vis2[p.x][p.y]=1;while(!qq.empty()){p = qq.front();qq.pop();for(int i = 0;i<4;++i){int xx = p.x+tx[i];int yy = p.y+ty[i];if(xx>=0&&xx<m&&y>=0&&y<n&&!vis2[xx][yy]&&s[xx][yy]!='#'){w.x = xx;w.y = yy;w.step = p.step+1;vis2[xx][yy] = 1;qq.push(w);if(s[xx][yy]=='A'||s[xx][yy]=='S'){int k1 = f(x,y);int k2 = f(xx,yy);Way[num].x = k1;Way[num].y = k2;Way[num].step = w.step;++num;}}}}}void creat(){for(int i = 0;i<m;++i)gets(s[i]);top = 0, num = 0;for(int i = 0;i<m;++i)for(int j = 0;j<n;++j)if(s[i][j]=='A'||s[i][j]=='S')xt[top].x = i,xt[top].y = j,++top;for(int i = 0;i<top;++i)BFS(xt[i].x, xt[i].y);}int pre[Max];void Initializa(){for(int i = 0;i<num;++i)pre[i] = i;}int Find(int x){while(pre[x]!=x)x = pre[x];return x;}void Join(int a, int b){int root_a = Find(a);int root_b = Find(b);if(root_a!=root_b){if(root_a>root_b)pre[root_a] = root_b;else pre[root_b] = root_a;}}void Kruskal(){Initializa();sort(Way,Way+num,cmp);int sum = 0;for(int i = 0;i<num;++i){int u = Way[i].x;int v = Way[i].y;if(Find(u)!=Find(v)){sum+=Way[i].step;Join(u,v);}}printf("%d\n",sum);}int main(){scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);gets(s[0]); // m后面应该还有空格, getchar()是错了,玄学问题creat();//建图Kruskal();//最小生成树}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: