博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Borg Maze(MST & bfs)
阅读量:4560 次
发布时间:2019-06-08

本文共 4276 字,大约阅读时间需要 14 分钟。

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

26 5##### #A#A### # A##S  ####### 7 7#####  #AAA####    A## S ####     ##AAA########

Sample Output

811

Source

1 #include
2 #include
3 #include
4 using namespace std; 5 int col , row , cnt; 6 char maze[200][200] ; 7 int l[200][200] ; 8 int vis[200][200] ; 9 int map[200][200] ;10 const int inf = 0x3f3f3f3f ;11 int move[][2] = {
1 , 0 , 0 , 1 , -1 , 0 , 0 , -1} ;12 13 void bfs (int sx , int sy)14 {15 queue
> q ;16 while (!q.empty ())17 q.pop () ;18 memset (vis , -1 , sizeof(vis)) ;19 vis[sx][sy] = 0 ;20 q.push (make_pair(sx , sy)) ;21 while (!q.empty ()) {22 pair
k = q.front () ;23 q.pop () ;24 if (l[k.first][k.second] != -1)25 map [l[sx][sy]] [l[k.first][k.second]] = vis [k.first][k.second] ;26 for (int i = 0 ; i < 4 ; i++) {27 int tx = k.first + move[i][0] ;28 int ty = k.second + move[i][1] ;29 if (maze[tx][ty] == '#' || vis[tx][ty] != -1)30 continue ;31 vis[tx][ty] = vis[k.first][k.second] + 1 ;32 q.push (make_pair(tx , ty)) ;33 }34 }35 }36 37 void prim ()38 {39 int p[200] , d[200] ;40 for (int i = 1 ; i < cnt ; i++) {41 d[i] = map[1][i] ;42 p[i] = 1 ;43 }44 d[1] = 0 ;45 int ans = 0 ;46 for (int i = 1 ; i < cnt - 1 ; i++) {47 int minc = inf , k ;48 for (int j = 1 ; j < cnt ; j++) {49 if (d[j] && d[j] < minc) {50 minc = d[j] ;51 k = j ;52 }53 }54 d[k] = 0 ;55 for (int j = 1 ; 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[0]) ;74 int tol = 1 ;75 memset (l , -1 , sizeof(l)) ;76 for (int i = 0 ; i < row ; i++) {77 gets (maze[i]) ;78 for (int j = 0 ; 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 = 0 ; i < row ; i++) {85 for (int j = 0 ; j < col ; j++) {86 if (l[i][j] != -1) {87 bfs (i , j);88 }89 }90 }91 cnt = tol ;92 prim () ;93 }94 return 0 ;95 }
View Code

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

转载于:https://www.cnblogs.com/get-an-AC-everyday/p/4314061.html

你可能感兴趣的文章
有关位运算的操作+二进制状态压缩
查看>>
Eclipse插件 -- 阿里巴巴扫描编码规插件
查看>>
(1.1)学习笔记之mysql体系结构(内存、进程、线程)
查看>>
markdown测试
查看>>
Java-Maven-Runoob:Maven 依赖管理
查看>>
杂项-Log:log4net
查看>>
杂项-Java:EL表达式
查看>>
tarroni music
查看>>
unity 使用RotateAround的使用注意
查看>>
[SDOI2009]HH的项链
查看>>
CodeFirst模式,容易引发数据迁移问题(不建议使用)
查看>>
jquery的colorbox关闭并传递数据到父窗
查看>>
使用Nginx、Keepalived构建文艺负载均衡
查看>>
phpmyadmin 开放远程登录的权限
查看>>
linux安装gcc和gcc-c++
查看>>
qq登陆错误提示
查看>>
bzoj 1192: [HNOI2006]鬼谷子的钱袋 思维 + 二进制
查看>>
没写完,没调完,咕咕咕的代码
查看>>
Android Studio使用技巧:导出jar包
查看>>
Problem E. TeaTree - HDU - 6430 (树的启发式合并)
查看>>