码迷,mamicode.com
首页 > 其他好文 > 详细

Borg Maze(MST & bfs)

时间:2015-03-04 20:47:58      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

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

2
6 5
##### 
#A#A##
# # A#
#S  ##
##### 
7 7
#####  
#AAA###
#    A#
# S ###
#     #
#AAA###
#####  

Sample Output

8
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[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 <pair <int , int> > 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 <int , int> 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了一个下午

Borg Maze(MST & bfs)

标签:

原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4314061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!