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

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

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6634   Accepted: 2240

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

 
 
 
 
 
一开始一直看不懂题目意思。
 
关键就是 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.). 
 
 
这样其实就是求最小生成树了
 
先对有A或S的地方进行bfs。得出距离
 
//============================================================================// Name        : POJ.cpp// Author      : // Version     :// Copyright   : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================#include 
#include
#include
#include
#include
#include
using namespace std;char g[100][100];int n,m;int a[100][100];int move[][2]={ { 1,0},{-1,0},{ 0,1},{ 0,-1}};int cost[110][110];int t[100][100];void bfs(int sx,int sy){ queue
>q; while(!q.empty())q.pop(); memset(t,-1,sizeof(t)); t[sx][sy]=0; q.push(make_pair(sx,sy)); while(!q.empty()) { pair
now=q.front(); q.pop(); if(a[now.first][now.second]!=-1) cost[a[sx][sy]][a[now.first][now.second]]=t[now.first][now.second]; for(int i=0;i<4;i++) { int tx=now.first+move[i][0]; int ty=now.second+move[i][1]; if(g[tx][ty]=='#'||t[tx][ty]!=-1)continue; t[tx][ty]=t[now.first][now.second]+1; q.push(make_pair(tx,ty)); } }}const int INF=0x3f3f3f3f;bool vis[110];int lowc[110];int Prim(int n){ int ans=0; memset(vis,false,sizeof(vis)); vis[0]=true; for(int i=1;i
lowc[j]) { minc=lowc[j]; p=j; } if(minc==INF)return -1; ans+=minc; vis[p]=true; for(int j=0;j
cost[p][j]) lowc[j]=cost[p][j]; } return ans;}int main(){// freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout); int T; scanf("%d",&T); while(T--) { scanf("%d%d",&m,&n); gets(g[0]); memset(a,-1,sizeof(a)); int tol=0; for(int i=0;i

 

 
 

转载地址:http://flgdl.baihongyu.com/

你可能感兴趣的文章
spring boot 项目启动无任何反应
查看>>
ASP.NET Core 2.1 : 十.升级现有Core2.0 项目到2.1
查看>>
架构师修炼之路
查看>>
会人之不会成为能
查看>>
ICMP报文 (1)
查看>>
JAVA array,map 转 json 字符串
查看>>
Apache Flink,如何重新定义计算?
查看>>
微软 Azure 容器服务要关停,你想好怎么迁移了吗?
查看>>
ROG全球行销总监尤彦博:强劲超薄的GX501与中国电竞产业
查看>>
开发者是保护代码道德的最后防线?
查看>>
阿里云跻身Gartner魔力象限,引外媒纷纷点赞
查看>>
苹果也得为它打工 iPhoneX大卖 它将入账143亿美元
查看>>
金立S10“又”入围吉林6月畅销手机排行榜,短时间内连下五省!
查看>>
375万买垃圾房,爆改后变千万豪宅,你也能做到
查看>>
沈颖刚:生物柴油或是高原柴油货车污染治理有效途径
查看>>
掌阅公布数字阅读报告:00后成第二大阅读群体
查看>>
冬季风暴席卷美国致航班取消车祸频发 20万人断电
查看>>
民航局正式启动北斗星基增强系统民航应用验证评估工作
查看>>
北京新机场 严打无人机“黑飞”
查看>>
8点1氪|阿里巴巴第三财季营收破千亿;传滴滴拟裁员25%;饿了么口碑超30亿美元融资已逐步到位...
查看>>