【POJ - 3026】Borg Maze(bfs预处理 + 最小生成树,建图)

题干:

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点开始去抓捕,抓捕到外星人后这个人可以分身为任意个(也可以不分身),求把所有外星人抓完所用的最小距离,距离包括分身走的距离(即所有的距离都算上,比如:两个分身同时走三步,那么走的距离就是6,而不是3),在迷宫之中只能向东南西北四个方向移动。

换种说法:

给你一个地图,地图中A代表外星人,S代表出发点,从S点出发,在起点S 或是 每遇到一个A点 便可不分裂或是分裂成多个。求把所有A都吃完需要多少步。注意不要以为当前点随时都能分裂,而是只有在S或是遇到A才能分裂。

解题报告:

  注意这题样例 n m 和字符串之间可能有一行空行(而不一定是一个空格)真tm格式、、所以getchar会WA的。。

因为不难发现,既然这个题意的话,那么起点在哪并没有怎么所谓了,因为S的所有特点和A的所有特点是完全相同的,所以可以把S点也看成是一个A点,那么题意转化成:从一个A点出发,在所有A点相连的路径中选择一些路径,使得走过的路最小。

那么解法很显然了,假设有tot个S和A,那么用bfs构造出这tot个点之间的距离,然后求个MST就好,因为稠密图而且是完全图所以首选prim。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 200 + 5;
const int INF = 0x3f3f3f3f;
int n,m;
char s[MAX][MAX];
int bk[MAX][MAX],cost[MAX][MAX],tot;
bool vis[MAX][MAX];
int nx[4] = {0,1,0,-1};
int ny[4] = {1,0,-1,0};
struct Node {int x,y,t;Node(int x=0,int y=0,int t=0):x(x),y(y),t(t){}
};
void bfs(int stx,int sty) {int id1 = bk[stx][sty];queue<Node> q;q.push(Node(stx,sty,0));memset(vis,0,sizeof vis);vis[stx][sty]=1;while(!q.empty()) {Node cur = q.front();q.pop();int id2 = bk[cur.x][cur.y];if(bk[cur.x][cur.y]) cost[id1][id2] = cur.t;for(int k = 0; k<4; k++) {int tx = cur.x+nx[k];int ty = cur.y+ny[k];if(tx<0||tx>n||ty<1||ty>m) continue;if(vis[tx][ty] || s[tx][ty] == '#') continue;vis[tx][ty] = 1;q.push(Node(tx,ty,cur.t+1));}}	
}
int VIS[MAX*MAX],dis[MAX*MAX];
int prim() {int res = 0;for(int i = 1; i<=tot; i++) dis[i] = INF,VIS[i] = 0;dis[1] = 0;while(1) {int minv,minw=INF;for(int i = 1; i<=tot; i++) {if(VIS[i] == 0 && dis[i] < minw) minw = dis[i],minv = i;}if(minw == INF) break;VIS[minv] = 1;for(int i = 1; i<=tot; i++) {if(VIS[i] == 0 && dis[i] > cost[minv][i]) dis[i] = cost[minv][i];}res += dis[minv];}return res;		
}
int main()
{int t;cin>>t;while(t--) {tot=0;memset(bk,0,sizeof bk);memset(cost,0,sizeof cost);scanf("%d%d",&m,&n);string qq;getline(cin,qq);for(int i = 1; i<=n; i++) {cin.getline(s[i]+1,1005);for(int j = 1; j<=m; j++) {if(s[i][j] == 'S' || s[i][j] == 'A') bk[i][j] = ++tot;}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(bk[i][j]) bfs(i,j);}}printf("%d\n",prim());} return 0 ;
}

刚开始WA在数组就开了50*50这么大,导致cost数组也开了[50][50],但是其实应该[2501][2501],但是其实这题说了最多100个外星人,所以可以开[105][105]就够了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440350.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机统考测试,计算机统考专业测试题.doc

文档介绍&#xff1a;应用所有单选题1、下面是某单位主页地址的,其中符合格式的是。A:B:C:D:答案:C知识点:应用部分\和的使用\浏览器的使用\1网页的几个基本术语2、用浏览器浏览网页,在地址栏中输入网址时,通常可以省略的是。A:B:C:D:答案:A知识点:应用部分\和的使用\浏览器的使…

3.Your First Machine Learning Model

Selecting Data for Modeling 你的数据集有太多的变量包裹住你的头。你怎么能把这些压倒性的数据削减到你能理解的东西&#xff1f; 我们首先使用我们的直觉选择一些变量。 后面的课程将向您展示自动确定变量优先级的统计技巧。 要选择变量/列&#xff0c;我们需要查看数据集中…

【POJ - 3020】Antenna Placement (匈牙利算法,二分图最小边覆盖)

题干&#xff1a; The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the job, is their discovery of a new, highly noise resistant, antenna. It …

计算机教室安全预案 博客,校园安全应急预案

校园安全应急预案为了确保师生的人身安全&#xff0c;严格执行上级安全工作的管理要求&#xff0c;保证一旦发生安全事故能够及时处理&#xff0c;特制定我校安全应急预案。一、领导小组组 长&#xff1a;副组长&#xff1a;成 员&#xff1a;全体教师二、主要职责1、组长任校…

4.Model Validation

你已经建立了一个模型。 但它有多好&#xff1f; 在本课程中&#xff0c;您将学习如何使用模型验证来衡量模型的质量。 测量模型质量是迭代改进模型的关键。 What is Model Validation 你几乎要评估你构建的每个模型。在大多数&#xff08;尽管不是全部&#xff09;应用中&am…

【POJ - 2195】Going Home(二分图最优匹配,费用流 或 KM)

题干&#xff1a; On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step h…

微机原理实验8254计算机钢琴,GitHub - SincereXIA/PianoMFC: 西电微机原理课设项目,键盘电子乐器演奏程序设计(电子琴),MFC...

PianoMFC西电微机原理课设项目&#xff0c;键盘电子乐器演奏程序设计(电子琴)&#xff0c;MFC需要连接西电微机原理实验室提供的 QTH9054 微机试验箱&#xff0c;使用其蜂鸣器发声&#xff0c;若不连接&#xff0c;程序会直接播放 mp3 文件模拟钢琴声。请在 release 处下载编译…

5.Underfitting and Overfitting

在这一步结束时&#xff0c;您将了解欠拟合和过拟合的概念&#xff0c;并且您将能够应用这些办法来使您的模型更准确。 Experimenting With Different Models 现在您已经有了一种可靠的方法来测量模型精度&#xff0c;您可以尝试使用其他模型&#xff0c;并查看哪种模型可以提…

福建省计算机初级职称,2019福建助理工程师职称评定条件材料及审核管理制度...

一学历、资历条件要求(破格申报不在此列&#xff0c;详情请咨询了解)申报工程技术系列中级工程师须符合下列条件之一&#xff1a;1.博士研究生毕业&#xff1b;2.硕士研究生毕业后&#xff0c;从事所申报专业工作满3年&#xff1b;3.本科毕业后&#xff0c;从事所申报专业工作满…

【POJ - 2594】Treasure Exploration(floyd传递闭包 + 最小路径覆盖,图论)

题干&#xff1a; Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored treasure? If you never have such experiences, you would never know what fun treasure exploring brings…

6.Random Forests

Introduction 决策树会让您做出艰难的决定。 有很多树叶的深树将会过拟合&#xff0c;因为每个预测都来自其叶子上只有少数房屋的历史数据。 但是叶子很少的浅树会表现不佳&#xff0c;因为它无法捕获原始数据中的许多区别。 即使在今天&#xff0c;最成熟的建模技术也面临着过…

7.Handling Missing Values

本教程是学习机器学习课程的第2部分。 本教程选择了1级完成的位置&#xff0c;因此如果您从1级完成练习&#xff0c;您将获得最大的收益。 在此步骤中&#xff0c;您将学习三种处理缺失值的方法。 然后&#xff0c;您将学习如何比较这些方法在任何给定数据集上的有效性。 Intr…

打开电脑计算机超级慢,手把手教你电脑开机慢怎么办

等到花都谢了&#xff0c;你怎么还不开机&#xff1f;这电脑开机真是离奇的慢&#xff0c;有心将它换了&#xff0c;奈何兜里空空。凑合着用又无法忍受这种煎熬。其实你只需要用鼠标点几下就可以不用等待这漫长的开机过程了。高铁&#xff0c;飞机&#xff0c;网络&#xff0c;…

【POJ - 1486】Sorting Slides(思维建图,二分图求必须边,关建边,图论)

题干&#xff1a; Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he is not a very tidy person and has put all his transparencies on one big heap. Before giving the talk, he has to sort the slides. Being a kind of minima…

用OpenSSL编写SSL,TLS程序

http://zhoulifa.bokee.com/6134045.html http://blog.sina.com.cn/s/blog_86ca13bb0100vaph.html http://blog.chinaunix.net/uid-26575352-id-3048856.html 一、简介: SSL(SecureSocket Layer)是netscape公司提出的主要用于web的安全通信标准,分为2.0版和3.0版.TLS(Transport…

信息技术计算机伦理与安全教案,龙教版信息技术七年级下册第7课 安全与道德 教案...

ID:9954219分类&#xff1a;全国,2019资源大小&#xff1a;228KB资料简介:题 目第七课 安全与道德总课时1设计来源自我设计教学时间教材分析这节课计算机与网络安全部分定义介绍和叙述较多,所以为了避免枯燥可以设计课件和并准备病毒计算机安全报道的视频、多媒体讲解、图片等…

【HDU - 5706】GirlCat(bfs)

题干&#xff1a; As a cute girl, Kotori likes playing Hide and Seek with cats particularly. Under the influence of Kotori, many girls and cats are playing Hide and Seek together. Koroti shots a photo. The size of this photo is nmnm, each pixel of the ph…

8.Using Categorical Data with One Hot Encoding

本教程是机器学习系列的一部分。 在此步骤中&#xff0c;您将了解“分类”变量是什么&#xff0c;以及处理此类数据的最常用方法。 Introduction 分类数据是仅采用有限数量值的数据。 例如&#xff0c;如果人们回答一项关于他们拥有哪种品牌汽车的调查&#xff0c;结果将是明…

iPhone换屏幕测试软件,怎样检验iPhone是否更换过屏幕?

原标题&#xff1a;怎样检验iPhone是否更换过屏幕&#xff1f;关注下图公众号&#xff0c;鉴定苹果手机真假↓↓↓购买新手机时&#xff0c;到手后会想手机各零部件是否是正品原装&#xff0c;就好比屏幕是否原装屏&#xff01;入手一部iPhone新机的时候&#xff0c;该如何检验…

*【HDU - 5707】Combine String(dp)

题干&#xff1a; Given three strings aa, bb and cc, your mission is to check whether cc is the combine string of aa and bb. A string cc is said to be the combine string of aa and bb if and only if cc can be broken into two subsequences, when you read the…