Borg Maze (BFS预处理+最小生成树)

题目连接:https://vjudge.net/problem/POJ-3026

 

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

 

思路:

从每个‘A’ ‘S’ 开始进行bfs,从而得到从当前点到其他每个点的最短路程。之后就是很常规的建树kruscal了。

 

  1 #include <math.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <string>
  7 #include <string.h>
  8 #include <vector>
  9 #include <map>
 10 #include <stack>
 11 #include <set>
 12 #include <queue>
 13 
 14 
 15 #define LL long long
 16 #define INF 0x3f3f3f3f
 17 #define ls nod<<1
 18 #define rs (nod<<1)+1
 19 const int maxn = 2e5+7;
 20 const double eps = 1e-9;
 21 
 22 int n,m,ans,cnt,fa[5050];
 23 
 24 int row,col;
 25 int a[60][60],vis[60][60];
 26 int dirx[4] = {1, 0, -1, 0};
 27 int diry[4] = {0, 1, 0, -1};
 28 struct P {
 29     int x,y;
 30     int step;
 31 }st,ed;
 32 
 33 struct node{
 34     int u, v, w;
 35 }e[maxn];
 36 
 37 bool cmp(node a, node b)
 38 {
 39     return a.w < b.w;
 40 }
 41 
 42 int fid(int x)
 43 {
 44     return x == fa[x] ? x : fid(fa[x]);
 45 }
 46 
 47 void init(int n)
 48 {
 49     for(int i = 1; i <= n; i++) {
 50         fa[i] = i;
 51     }
 52     ans = 0;
 53     cnt = 0;
 54 }
 55 
 56 void kruskal()
 57 {
 58     std::sort(e+1, e+m+1, cmp);
 59     for(int i = 1; i <= m; i++) {
 60         int eu = fid(e[i].u);
 61         int ev = fid(e[i].v);
 62         if(eu == ev) {
 63             continue;
 64         }
 65         ans += e[i].w;
 66         fa[ev] = eu;
 67         if(++cnt == n-1) {
 68             break;
 69         }
 70     }
 71 }
 72 
 73 void bfs(int x,int y,int idx) {
 74     memset(vis,0, sizeof(vis));
 75     std::queue<P> q;
 76     st.x = x;
 77     st.y = y;
 78     st.step = 0;
 79     vis[x][y] = 1;
 80     q.push(st);
 81     while (!q.empty()) {
 82         st = q.front();
 83         q.pop();
 84         for (int i=0;i<4;i++) {
 85             ed.x = st.x + dirx[i];
 86             ed.y = st.y + diry[i];
 87             ed.step = st.step + 1;
 88             if (ed.x>=1 && ed.x<=row && ed.y>=1 && ed.y<=col && !vis[ed.x][ed.y] && a[ed.x][ed.y] != -1) {
 89                 vis[ed.x][ed.y] = 1;
 90                 q.push(ed);
 91                 if (a[ed.x][ed.y] > 0) {
 92                     e[m].u = idx;
 93                     e[m].v = a[ed.x][ed.y];
 94                     e[m++].w = ed.step;
 95                 }
 96             }
 97         }
 98     }
 99 }
100 
101 
102 int main() {
103     int T;
104     scanf("%d",&T);
105     char ch[100];
106     while (T--) {
107         scanf("%d%d",&col,&row);
108         gets(ch);
109         n = 1,m = 1;
110         memset(a,0, sizeof(a));
111         for (int i=1;i<=row;i++) {
112             gets(ch);
113             for (int j=0;j<col;j++) {
114                 if (ch[j] == '#')
115                     a[i][j+1] = -1;
116                 else if (ch[j] == 'A' || ch[j] == 'S')
117                     a[i][j+1] = n++;
118                 else if (ch[j] == ' ')
119                     a[i][j+1] = 0;
120             }
121         }
122         for (int i=1;i<=row;i++) {
123             for (int j=1;j<=col;j++) {
124                 if (a[i][j] > 0)
125                     bfs(i,j,a[i][j]);
126             }
127         }
128         n--;
129         m--;
130         init(n);
131         kruskal();
132         printf("%d\n",ans);
133     }
134     return 0;
135 }

 

 

 

 

全部评论

相关推荐

(黑话警告⚠️:hc=岗位数量,&nbsp;mt=导师,&nbsp;ld=直属领导,&nbsp;cr=代码审查)25年1月,我加入了字节某前端团队,并期望能在这里待到秋招并尝试转正。然而,就在上周,ld&nbsp;找我1v1,告诉我,我的能力和团队预期不太匹配,并和我劝退。晴天霹雳吗?肯定是有的。那一刻,脑子里嗡嗡作响,各种情绪翻涌。但冷静下来想想,这几个月,自己在能掌控的范围内,确实有不少地方做得不尽如人意。所以,我想把这段不算成功的经历复盘一下,希望能给同样在努力转正的你提个醒,避开我踩过的坑。一、ld&nbsp;的要求要注意刚进组时,ld就和我聊过转正的事。我当时发问:“咱们这儿有hc&nbsp;吗?”&nbsp;ld没直接回答,只是说:“看能力,能力到了...
牛客上的彭于晏:过来人告诉你,入职后要做的第一件事儿不是说主动找活儿做,你要先学会融入团队,摸清ld的性格,投其所好。然后才是展示你的能力,能力上可以说技术或者业务,以业务能力为主,技术能力为辅。优先保证自己对业务需求的开发保证质量效率,然后再谈技术的问题,不要你觉得啥啥啥不行就想着整体优化了(发现校招生最喜欢干这事儿),我工作快5年了发现搞这种的最后都没啥好的结果,产出没有还引入新的bug,校招或者实习的水平看到的问题别人看不到嘛?为什么别人不去搞?浪费时间还没收益的事儿不要去做,技术上的能力体现在对于一个新需求,在不符合现在业务发展的架构设计上,你能拿出好的技术方案同时能考虑到后续业务发展逐渐将技术架构引入合理的架构,这是一个漫长的过程而不是一次性的
点赞 评论 收藏
分享
05-11 11:48
河南大学 Java
程序员牛肉:我是26届的双非。目前有两段实习经历,大三上去的美团,现在来字节了,做的是国际电商的营销业务。希望我的经历对你有用。 1.好好做你的CSDN,最好是直接转微信公众号。因为这本质上是一个很好的展示自己技术热情的证据。我当时也是烂大街项目(网盘+鱼皮的一个项目)+零实习去面试美团,但是当时我的CSDN阅读量超百万,微信公众号阅读量40万。面试的时候面试官就告诉我说觉得我对技术挺有激情的。可以看看我主页的美团面试面经。 因此花点时间好好做这个知识分享,最好是单拉出来搞一个板块。各大公司都极其看中知识落地的能力。 可以看看我的简历对于博客的描述。这个帖子里面有:https://www.nowcoder.com/discuss/745348200596324352?sourceSSR=users 2.实习经历有一些东西删除了,目前看来你的产出其实很少。有些内容其实很扯淡,最好不要保留。有一些点你可能觉得很牛逼,但是面试官眼里是减分的。 你还能负责数据库表的设计?这个公司得垃圾成啥样子,才能让一个实习生介入数据库表的设计,不要写这种东西。 一个公司的财务审批系统应该是很稳定的吧?为什么你去了才有RBAC权限设计?那这个公司之前是怎么处理权限分离的?这些东西看着都有点扯淡了。 还有就是使用Redis实现轻量级的消息队列?那为什么这一块不使用专业的MQ呢?为什么要使用redis,这些一定要清楚, 就目前看来,其实你的这个实习技术还不错。不要太焦虑。就是有一些内容有点虚了。可以考虑从PR中再投一点产出
点赞 评论 收藏
分享
爱喝奶茶的垂耳兔拥抱太阳:感觉项目和实习没有技术亮点和难点,单纯说了自己干了啥
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务