博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(最小生成树) Borg Maze -- POJ -- 3026
阅读量:6308 次
发布时间:2019-06-22

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

链接:

 

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10713   Accepted: 3559

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

 

 

代码:

#include
#include
#include
#include
using namespace std;const int maxn = 105;const int oo = 0xfffffff;int dir[4][2] = { {
1,0},{
0,1},{-1,0},{
0,-1} };char G[maxn][maxn]; //保存地图int D[maxn][maxn]; //记录两点间的距离int use[maxn][maxn]; //标记地图int Index[maxn][maxn]; //记录‘A’或者‘B’的编号struct node{
int x, y, step;};void BFS(int k, int M,int N, int x, int y){ queue
Q; node s; s.x = x, s.y = y, s.step = 0; use[s.x][s.y] = k; Q.push(s); while(Q.size()) { s = Q.front();Q.pop(); if(G[s.x][s.y]>='A' && G[s.x][s.y] <='Z') D[k][ Index[s.x][s.y] ] = s.step; for(int i=0; i<4; i++) { node q = s; q.x += dir[i][0], q.y += dir[i][1]; if(q.x>=0&&q.x
=0&&q.y
dist[i]) mini = dist[i], k=i; } ans += mini; vis[k] = true; for(i=1; i<=N; i++) if(!vis[i])dist[i] = min(dist[i], D[k][i]); } return ans;}int main(){ int T; scanf("%d", &T); while(T--) { int i, j, M, N, t=1; scanf("%d%d ", &N, &M); for(i=0; i
='A' && G[i][j]<='Z') Index[i][j] = t++; use[i][j] = 0; } } for(i=0; i
='A' && G[i][j]<='Z') BFS(Index[i][j], M, N, i, j); } int ans = Prim(t-1); printf("%d\n", ans); } return 0;}

 

转载于:https://www.cnblogs.com/YY56/p/4735119.html

你可能感兴趣的文章
路由器ospf动态路由配置
查看>>
zabbix监控安装与配置
查看>>
python 异常
查看>>
last_insert_id()获取mysql最后一条记录ID
查看>>
可执行程序找不到lib库地址的处理方法
查看>>
bash数组
查看>>
Richard M. Stallman 给《自由开源软件本地化》写的前言
查看>>
oracle数据库密码过期报错
查看>>
修改mysql数据库的默认编码方式 .
查看>>
zip
查看>>
How to recover from root.sh on 11.2 Grid Infrastructure Failed
查看>>
rhel6下安装配置Squid过程
查看>>
《树莓派开发实战(第2版)》——1.1 选择树莓派型号
查看>>
在 Linux 下使用 fdisk 扩展分区容量
查看>>
结合AlphaGo算法和大数据的量化基本面分析法探讨
查看>>
如何在 Ubuntu Linux 16.04 LTS 中使用多个连接加速 apt-get/apt
查看>>
《OpenACC并行编程实战》—— 导读
查看>>
机器学习:用初等数学解读逻辑回归
查看>>
如何在 Ubuntu 中管理和使用逻辑卷管理 LVM
查看>>
Oracle原厂老兵:从负面案例看Hint的最佳使用方式
查看>>