http://poj.org/problem?id=3026

Borg Maze

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

  1. 8
  2. 11
    最小生成树问题,但较不同点的是没有给原图,所以要用BFS找各个字母之间的距离,即构成原图
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<stdlib.h>
  5. #include<ctype.h>
  6. #include<queue>
  7. #define INF 0x3f3f3f3f
  8. #define max(a, b)(a > b ? a : b)
  9. #define min(a, b)(a < b ? a : b)
  10. #define N 110
  11.  
  12. using namespace std;
  13.  
  14. struct node
  15. {
  16. int x, y, step;
  17. };
  18.  
  19. int G[N][N], dist[N], p[N][N];//p记录字母的坐标
  20. bool vis[N], use[N][N];
  21. char maps[N][N];
  22. int m, n, num;
  23. int d[][] = {{, }, {, }, {-, }, {, -}};
  24.  
  25. void Init()
  26. {
  27. int i, j;
  28. memset(p, , sizeof(p));
  29. memset(vis, false, sizeof(vis));
  30. for(i = ; i <= num ; i++)
  31. {
  32. for(j = ; j <= num ; j++)
  33. {
  34. if(i == j)
  35. G[i][j] = ;
  36. else
  37. G[i][j] = G[j][i] = INF;
  38. }
  39. }
  40. }
  41.  
  42. int prim(int s)//最小生成树,求最小花费
  43. {
  44. int i, j, index, Min, ans = ;
  45. for(i = ; i <= num ; i++)
  46. dist[i] = G[s][i];
  47. vis[s] = true;
  48. for(i = ; i < num ; i++)
  49. {
  50. Min = INF;
  51. for(j = ; j <= num ; j++)
  52. {
  53. if(!vis[j] && dist[j] < Min)
  54. {
  55. Min = dist[j];
  56. index = j;
  57. }
  58. }
  59. ans += Min;
  60. vis[index] = true;
  61. for(j = ; j <= num ; j++)
  62. {
  63. if(!vis[j] && dist[j] > G[index][j])
  64. dist[j] = G[index][j];
  65. }
  66. }
  67. return ans;
  68. }
  69.  
  70. void BFS(int x, int y)//广搜构建原图
  71. {
  72. queue<node>Q;
  73. int i, a, b;
  74. node now, next;
  75. memset(use, false, sizeof(use));
  76. now.x = x;
  77. now.y = y;
  78. now.step = ;
  79. use[x][y] = true;
  80. Q.push(now);
  81. while(!Q.empty())
  82. {
  83. now = Q.front();
  84. Q.pop();
  85. if(p[now.x][now.y] > )//该点为字母
  86. G[p[x][y]][p[now.x][now.y]] = now.step;
  87. for(i = ; i < ; i++)
  88. {
  89. a = next.x = now.x + d[i][];
  90. b = next.y = now.y + d[i][];
  91.  
  92. if(a >= && a < m && b >= && b < n && !use[a][b] && maps[a][b] != '#')
  93. {
  94. next.step = now.step + ;
  95. use[a][b] = true;
  96. Q.push(next);
  97.  
  98. }
  99. }
  100. }
  101. }
  102.  
  103. int main()
  104. {
  105. int i, j, t;
  106. scanf("%d", &t);
  107. while(t--)
  108. {
  109. scanf("%d%d ", &n, &m);
  110. Init();
  111. num = ;
  112. for(i = ; i < m ; i++)
  113. {
  114. for(j = ; j < n ; j++)
  115. {
  116. scanf("%c", &maps[i][j]);
  117. }
  118. getchar();
  119. }
  120. for(i = ; i < m ; i++)
  121. {
  122. for(j = ; j < n ; j++)
  123. {
  124. if(maps[i][j] == 'A' || maps[i][j] == 'S')
  125. p[i][j] = ++num;//统计字母的个数,即要进入树的点的个数
  126. }
  127. }
  128. for(i = ; i < m ; i++)
  129. {
  130. for(j = ; j < n ; j++)
  131. if(p[i][j] > )
  132. BFS(i, j);
  133. }
  134. printf("%d\n", prim());
  135. }
  136. return ;
  137. }

poj 3026 Borg Maze (BFS + Prim)的更多相关文章

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

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

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

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

  3. POJ 3026 Borg Maze(Prim+bfs求各点间距离)

    题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’  ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...

  4. POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  5. POJ 3026 Borg Maze bfs+Kruskal

    题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...

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

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

  7. poj 3026 Borg Maze bfs建图+最小生成树

    题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...

  8. 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 Descrip ...

  9. POJ 3026 Borg Maze(bfs+最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6634   Accepted: 2240 Descrip ...

随机推荐

  1. JS中字符串拼装 单双引号的处理 字符转义

    js中可能会用到动态追加元素,可能数据也是从后台传过来的,当然有两种思路, 1.在后台拼装好直接返回; 2.在前台js里面拼装, 如果拼装大量的html时可能单双引号就容易出问题;那么如何解决呢?最近 ...

  2. new int[]和new int()的区别

    1. new int[] 是创建一个int型数组,数组大小是在[]中指定,例如:int * p = new int[10]; //p执行一个长度为10的int数组.2. new int()是创建一个i ...

  3. js实现ppt

    实现ppt的js框架有很多,这里推荐几个: impress.js      impress.js demo webSlide.js    webSlide.js demo reveal.js      ...

  4. tahoma字体对中文字的影响

    一提到tahoma字体大家都会想到,它是一个英文字体,对中文不会有影响. 但是今天就遇到一个问题,tahoma字体会影响中文字的显示,如: html代码: <div class="bo ...

  5. WPF 用 DataTemplate 合并DataGrid列表列头<类似报表设计>及行头列头样式 - 学习

    WPF中 DataGrid 列头合并,类似于报表设计.效果图如下↓ 1.新建一个WPF项目WpfApplication1,新建一个窗体DataGridTest,前台代码如下: <Window x ...

  6. JS省队集训记

    不知不觉省队集训已经结束,离noi也越来越近了呢 论考前实战训练的重要性,让我随便总结一下这几天的考试 Day 1 T1 唉,感觉跟xj测试很像啊?meet in middle,不过这种题不多测是什么 ...

  7. 50个python库

    50个很棒的Python模块,包含几乎所有的需要:比如Databases,GUIs,Images, Sound, OS interaction, Web,以及其他.推荐收藏. Graphical in ...

  8. AI 行为树

    by AKara 2010-12-09 @ http://blog.csdn.net/akara @ akarachen(at)gmail.com @weibo.com/akaras 谈到游戏AI,很 ...

  9. ti processor sdk linux am335x evm /bin/setup-targetfs-nfs.sh hacking

    #!/bin/sh # # ti processor sdk linux am335x evm /bin/setup-targetfs-nfs.sh hacking # 说明: # 本文主要对TI的s ...

  10. I.MX6 Linux udev porting

    /*********************************************************************** * I.MX6 Linux udev porting ...