Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9220   Accepted: 3087

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

Source

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. using namespace std;
  5. int col , row , cnt;
  6. char maze[][] ;
  7. int l[][] ;
  8. int vis[][] ;
  9. int map[][] ;
  10. const int inf = 0x3f3f3f3f ;
  11. int move[][] = { , , , , - , , , -} ;
  12.  
  13. void bfs (int sx , int sy)
  14. {
  15. queue <pair <int , int> > q ;
  16. while (!q.empty ())
  17. q.pop () ;
  18. memset (vis , - , sizeof(vis)) ;
  19. vis[sx][sy] = ;
  20. q.push (make_pair(sx , sy)) ;
  21. while (!q.empty ()) {
  22. pair <int , int> k = q.front () ;
  23. q.pop () ;
  24. if (l[k.first][k.second] != -)
  25. map [l[sx][sy]] [l[k.first][k.second]] = vis [k.first][k.second] ;
  26. for (int i = ; i < ; i++) {
  27. int tx = k.first + move[i][] ;
  28. int ty = k.second + move[i][] ;
  29. if (maze[tx][ty] == '#' || vis[tx][ty] != -)
  30. continue ;
  31. vis[tx][ty] = vis[k.first][k.second] + ;
  32. q.push (make_pair(tx , ty)) ;
  33. }
  34. }
  35. }
  36.  
  37. void prim ()
  38. {
  39. int p[] , d[] ;
  40. for (int i = ; i < cnt ; i++) {
  41. d[i] = map[][i] ;
  42. p[i] = ;
  43. }
  44. d[] = ;
  45. int ans = ;
  46. for (int i = ; i < cnt - ; i++) {
  47. int minc = inf , k ;
  48. for (int j = ; j < cnt ; j++) {
  49. if (d[j] && d[j] < minc) {
  50. minc = d[j] ;
  51. k = j ;
  52. }
  53. }
  54. d[k] = ;
  55. for (int j = ; j < cnt ; j++) {
  56. if (d[j] && d[j] > map[k][j]) {
  57. d[j] = map[k][j] ;
  58. p[j] = k ;
  59. }
  60. }
  61. ans += minc ;
  62. }
  63. printf ("%d\n" , ans) ;
  64. }
  65.  
  66. int main ()
  67. {
  68. // freopen ("a.txt" , "r" , stdin) ;
  69. int T ;
  70. scanf ("%d" , &T) ;
  71. while (T--) {
  72. scanf ("%d%d" , &col , &row) ;
  73. gets(maze[]) ;
  74. int tol = ;
  75. memset (l , - , sizeof(l)) ;
  76. for (int i = ; i < row ; i++) {
  77. gets (maze[i]) ;
  78. for (int j = ; j < col ; j++) {
  79. if (maze[i][j] == 'A' || maze[i][j] == 'S') {
  80. l[i][j] = tol++ ;
  81. }
  82. }
  83. }
  84. for (int i = ; i < row ; i++) {
  85. for (int j = ; j < col ; j++) {
  86. if (l[i][j] != -) {
  87. bfs (i , j);
  88. }
  89. }
  90. }
  91. cnt = tol ;
  92. prim () ;
  93. }
  94. return ;
  95. }

这道题有巨坑,收空格一定要用gets , 我用getchar RE了一个下午

Borg Maze(MST & bfs)的更多相关文章

  1. Borg Maze(BFS+MST)

    Borg Maze http://poj.org/problem?id=3026 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  2. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

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

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

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

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

  5. POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16625   Accepted: 5383 Descri ...

  6. POJ3026 Borg Maze(Prim)(BFS)

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

  7. POJ 3026 Borg Maze【BFS+最小生成树】

    链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

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

    题目链接:http://poj.org/problem?id=3026. Description The Borg is an immensely powerful race of enhanced ...

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

    有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...

随机推荐

  1. IL指令大全

    IL是.NET框架中中间语言(Intermediate Language)的缩写.使用.NET框架提供的编译器可以直接将源程序编译为.exe或.dll文件,但此时编译出来的程序代码并不是CPU能直接执 ...

  2. java并发:线程同步机制之Lock

    一.初识Lock Lock是一个接口,提供了无条件的.可轮询的.定时的.可中断的锁获取操作,所有加锁和解锁的方法都是显式的,其包路径是:java.util.concurrent.locks.Lock, ...

  3. SQL Server 收缩日志

    一. SQL Server 2008 收缩日志 (1) 使用SQL管理器收缩日志 第一步执行如下命令 ALTER DATABASE platform SET RECOVERY SIMPLE GO 第二 ...

  4. js判断页面点击事件

    <input type="submit" name="sb1" id="sb1" onclick="queryclick() ...

  5. 《TCP/IP详解卷1:协议》第19章 TCP的交互数据流-读书笔记

    章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...

  6. Mobile Web

    Silun来给大家介绍几个常见的移动浏览器标签~ 当当当~ <meta name="apple-mobile-web-app-capable" content="y ...

  7. 传智168期JavaEE就业班 day01-html

    * HTML * HTML: HyperText Markup Language 超文本标记语言. * HTML是最基础的网页语言. * HTML的代码都是由标签所组成. * HTML的基本格式 &l ...

  8. 在CentOS上安装Git

    文章引用 :http://www.ccvita.com/370.html CentOS的yum源中没有git,只能自己编译安装,现在记录下编译安装的内容,留给自己备忘. 确保已安装了依赖的包 yum ...

  9. Mvc多级Views目录 asp.net mvc4 路由重写及 修改view 的寻找视图的规则

    一般我们在mvc开发过程中,都会碰到这样的问题.页面总是写在Views文件夹下,而且还只能一个Controller的页面只能写在相应的以 Controller名命名的文件夹下.如果我们写到别处呢?那么 ...

  10. HTML5开发注意事项及BUG解决

    1.点透Q:元素A上定位另外一个元素B,点击元素B,如果元素A有事件或链接,会触发元素A上的事件或链接,即点透A:在元素B的touchend中增加ev.preventDefault();阻止默认事件即 ...