Borg Maze

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.
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

  1. 2
  2. 6 5
  3. #####
  4. #A#A##
  5. # # A#
  6. #S ##
  7. #####
  8. 7 7
  9. #####
  10. #AAA###
  11. # A#
  12. # S ###
  13. # #
  14. #AAA###
  15. #####

Sample Output
8
11

题目大意:

    一个迷宫,' '代表可以到达,'#'代表不可以到达。'S'初始点。'A'是要到达的点。

    输出从S开始路过所有的A点所需要的路程(重复路过只算一次)。

解题思路:

    因为重复的路过某个点,路程只算一次,就相当于计算一个包含所有的'A'和'S'的最小树。

    使用BFS来计算边的权值(上下左右四种情况,简单的BFS,注意初始点的dis为0),来构成完全无向图,再用Kruskal算法算出权值和即可。(注意题目数据范围,数组开小了会WA)

    PS : POJ坑爹的后台,每行后面可能有坑爹的多余空格,注意处理掉即可。

Code:

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<string>
  5. #define MAXN 1000
  6. using namespace std;
  7. struct point
  8. {
  9. int x,y;
  10. } P[];
  11. struct point q[];
  12. int N,M,father[];
  13. bool vis[][],v[][];
  14. int dis[];
  15. struct edge
  16. {
  17. int begin,end;
  18. int dis;
  19. } T[*];
  20. void init()
  21. {
  22. memset(vis,,sizeof(vis));
  23. for (int i=; i<=; i++)
  24. father[i]=i;
  25. }
  26. int bfs(int a,int b)
  27. {
  28. int front=,rear=;
  29. q[front]=P[a];
  30. for (int i=; i<=M; i++)
  31. for (int j=; j<=N; j++)
  32. v[i][j]=vis[i][j];
  33. dis[front]=;
  34. while (front<rear)
  35. {
  36. if (q[front].x==P[b].x&&q[front].y==P[b].y) break;
  37. int x=q[front].x,y=q[front].y;
  38. int a[]= {x-,x+,x,x},b[]= {y,y,y+,y-};
  39. for (int i=; i<=; i++)
  40. if (a[i]>=&&a[i]<=M&&b[i]>=&&b[i]<=N&&!v[a[i]][b[i]])
  41. {
  42. q[rear].x=a[i],q[rear].y=b[i];
  43. dis[rear]=dis[front]+;
  44. v[a[i]][b[i]]=;
  45. rear++;
  46. }
  47. front++;
  48. }
  49. return dis[front];
  50. }
  51. int find(int x)
  52. {
  53. if (father[x]!=x)
  54. father[x]=find(father[x]);
  55. return father[x];
  56. }
  57. void join(int x,int y)
  58. {
  59. int fx=find(x),fy=find(y);
  60. father[fx]=fy;
  61. }
  62. bool cmp(struct edge a,struct edge b)
  63. {
  64. return a.dis<b.dis;
  65. }
  66. int main()
  67. {
  68. int C;
  69. cin>>C;
  70. while (C--)
  71. {
  72. init();
  73. cin>>N>>M;
  74. int k=;
  75. char ch;
  76. while ()
  77. {
  78. ch=getchar();
  79. if (ch=='\n') break;
  80. }
  81. for (int i=; i<=M; i++)
  82. {
  83. for (int j=; j<=N; j++)
  84. {
  85. char tmp;
  86. scanf("%c",&tmp);
  87. if (tmp=='#') vis[i][j]=;
  88. else vis[i][j]=;
  89. if (tmp=='A'||tmp=='S')
  90. {
  91. P[k].x=i,P[k].y=j;
  92. k++;
  93. }
  94. }
  95. while ()
  96. {
  97. ch=getchar();
  98. if (ch=='\n') break;
  99. }
  100. }
  101. int t=;
  102. for (int i=; i<k; i++)
  103. for (int j=; j<i; j++)
  104. {
  105. T[t].begin=i;
  106. T[t].end=j;
  107. T[t].dis=bfs(i,j);
  108. t++;
  109. }
  110. int cnt=,sum;
  111. sort(T+,T+t,cmp);
  112. for (int i=; i<t; i++)
  113. {
  114. if (find(T[i].begin)!=find(T[i].end))
  115. {
  116. cnt+=T[i].dis;
  117. join(T[i].begin,T[i].end);
  118. sum++;
  119. if (sum==k-) break;
  120. }
  121. }
  122. printf("%d\n",cnt);
  123. }
  124. return ;
  125. }

POJ3026——Borg Maze(BFS+最小生成树)的更多相关文章

  1. POJ3026 Borg Maze(bfs求边+最小生成树)

    Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...

  2. poj 3026 Borg Maze (bfs + 最小生成树)

    链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...

  3. POJ - 3026 Borg Maze bfs+最小生成树。

    http://poj.org/problem?id=3026 题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地.求从S出发,经过所有A所需要的最短路.你有一个特殊能力,当走到 ...

  4. POJ - 3026 Borg Maze BFS加最小生成树

    Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...

  5. POJ3026 Borg Maze(Prim)(BFS)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12729   Accepted: 4153 Descri ...

  6. POJ 3026 Borg Maze (最小生成树)

    Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...

  7. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  8. POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14165   Accepted: 4619 Descri ...

  9. 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20846 POJ 3026是同样的题,但是内存要求比较严格,并是没有 ...

随机推荐

  1. Source Insight 技巧总结

    以下文章转载自网络:http://blog.csdn.net/junjie319/article/details/6910992 http://www.cnblogs.com/bluestorm/ar ...

  2. vc 编译执行bat

    转载:

  3. apache重写字段详细说明

    用Apache虚拟主机的朋友很多,apache提供的.htaccess模块可以为每个虚拟主机设定rewrite规则,这对网站SEO优化相当有用,同时也改善了用户体验.国内的虚拟机一般不提供.htacc ...

  4. SQL正常工作日上班安排

    alter proc [work] as declare @i int begin id into #restdate from dt_work where work_date in (select ...

  5. 操作xml文档的常用方式

    1.操作XML文档的两种常用方式: 1)使用XmlReader类和XmlWriter类操作 XmlReader是基于数据流的,占用极少的内存,是只读方式的,所以速度极快.只能采用遍历的模式查找数据节点 ...

  6. Setfocus - IE 需要使用setTimeout

    setTimeout(function () { $('#controlid').focus(); }, 100); document.getElementById('filterPopupInput ...

  7. APACHE支持.htaccess以及 No input file specified解决方案

    在你的Apache安装文件夹conf里找到httpd.conf文件 搜索LoadModule rewrite_module modules/mod_rewrite.so 如果前面有注释符号#,请去掉. ...

  8. JS远程获取网页源代码的例子

    js代码获取网页源代码. 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < ...

  9. WPF中让TextBlock每一个字符显示不同的颜色

    XAML代码: <TextBlock x:Name="tb"> <Run Foreground="Red">R</Run> ...

  10. 安装Nuget上常用的包的命令

    起因: Nuget图形化操作界面各种卡顿,或者有时干脆就连不上了.所以用命令还是很必须的. 常用命令: 安装 Entity Framework : PM> Install-Package Ent ...