Borg Maze 分类: POJ 2015-07-27 15:28 5人阅读 评论(0) 收藏
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9971 | Accepted: 3347 |
Description
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
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
Sample Input
2
6 5
#####
#A#A##
# # A#
#S ##
#####
7 7
#####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
8
11
BFS + 最短路的prim算法;
比较坑的是输入数字之后,必须用gets(),不能用getchar()#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#define WW freopen("a1.txt","w",stdout) using namespace std; const int INF = 0x3f3f3f3f; const int MAX = 110; struct Line
{
int x;
int y;
int num;
}; char Map[MAX][MAX]; int a[MAX][MAX]; int Dis[MAX][MAX]; int Dir[][2]= {{0,1},{0,-1},{1,0},{-1,0}}; int n,m,top; void BFS(int i,int j)//计算点之间的距离
{
bool vis[MAX][MAX];
queue<Line>q;
memset(vis,false,sizeof(vis));
Line ans,ant;
ans.x=i;
ans.y=j;
ans.num=0;
vis[ans.x][ans.y]=true;
q.push(ans);
while(!q.empty())
{
ant=q.front();
q.pop();
if(a[ant.x][ant.y]!=-1)
{
Dis[a[i][j]][a[ant.x][ant.y]]=ant.num;
}
for(int k=0; k<4; k++)
{
ans.x=ant.x+Dir[k][0];
ans.y=ant.y+Dir[k][1];
if(Map[ans.x][ans.y]=='#'||vis[ans.x][ans.y])
{
continue;
}
ans.num=ant.num+1;
vis[ans.x][ans.y]=true;
q.push(ans);
}
}
}
int dis[MAX];
bool vis[MAX];
int prim()
{
memset(vis,false,sizeof(vis));
int sum=0;
for(int i=1; i<top; i++)
{
dis[i]=Dis[0][i];
}
vis[0]=true;
for(int i=1; i<top; i++)
{
int ans=INF;
int flag=-1;
for(int j=0; j<top; j++)
{
if(!vis[j]&&dis[j]<ans)
{
ans=dis[j];
flag=j;
}
}
if(flag==-1)
return -1;
vis[flag]=true;
sum+=ans;
for(int j=0; j<top; j++)
{
if(!vis[j]&&dis[j]>Dis[flag][j])
{
dis[j]=Dis[flag][j];
}
}
}
return sum;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&m,&n);
gets(Map[0]);
top=0;
memset(a,-1,sizeof(a));
for(int i=0; i<n; i++)
{
gets(Map[i]);
for(int j=0; j<m; j++)
{
if(Map[i][j]=='S'||Map[i][j]=='A')
{
a[i][j]=top++;
}
}
}
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(a[i][j]!=-1)
{
BFS(i,j);
}
}
}
printf("%d\n",prim());
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Borg Maze 分类: POJ 2015-07-27 15:28 5人阅读 评论(0) 收藏的更多相关文章
- 【solr基础教程之九】客户端 分类: H4_SOLR/LUCENCE 2014-07-30 15:28 904人阅读 评论(0) 收藏
一.Java Script 1.由于Solr本身可以返回Json格式的结果,而JavaScript对于处理Json数据具有天然的优势,因此使用JavaScript实现Solr客户端是一个很好的选择. ...
- Counterfeit Dollar 分类: POJ 2015-06-12 15:28 19人阅读 评论(0) 收藏
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41559 Accepted: 13 ...
- PIE(二分) 分类: 二分查找 2015-06-07 15:46 9人阅读 评论(0) 收藏
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...
- POJ 1068 AC 2014-01-07 15:24 146人阅读 评论(0) 收藏
POJ的题目都是英文的,所以,,,还是直接贴代码吧 #include<stdio.h> int main(){ int x,y,z; int n,nm,max; scanf("% ...
- 周赛-DZY Loves Chessboard 分类: 比赛 搜索 2015-08-08 15:48 4人阅读 评论(0) 收藏
DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input standard ...
- cf 61E. Enemy is weak 树状数组求逆序数(WA) 分类: Brush Mode 2014-10-19 15:16 104人阅读 评论(0) 收藏
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> ...
- max_flow(Dinic) 分类: ACM TYPE 2014-09-02 15:42 94人阅读 评论(0) 收藏
#include <cstdio> #include <iostream> #include <cstring> #include<queue> #in ...
- SQL 分组 加列 加自编号 自编号限定 分类: SQL Server 2014-11-25 15:41 283人阅读 评论(0) 收藏
说明: (1)日期以年月形式显示:convert(varchar(7),字段名,120) , (2)加一列 (3)自编号: row_number() over(order by 字段名 desc) a ...
- SQL 按月统计(两种方式) 分类: SQL Server 2014-08-04 15:36 154人阅读 评论(0) 收藏
(1)Convert 函数 select Convert ( VARCHAR(7),ComeDate,120) as Date ,Count(In_code) as 单数,Sum(SumTrueNum ...
随机推荐
- 转:Jmeter之Bean shell使用(二)
上一篇Jmeter之Bean shell使用(一)简单介绍了下Jmeter中的Bean shell,本文是对上文的一个补充,主要总结下常用的几种场景和方法,相信这些基本可以涵盖大部分的需求.本节内容如 ...
- 点的双联通+二分图的判定(poj2942)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10804 Acce ...
- [原创]java WEB学习笔记73:Struts2 学习之路-- strut2中防止表单重复提交
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记68:Struts2 学习之路-- 类型转换与复杂属性配合使用
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- MES: ESB
ESB定义了消息的收发和收发池,对于各种通讯方式定义了收发API,在收到信息后由eventBus来发布消息 ISender: public abstract interface ISender { p ...
- .NET: C#: System.Diagnostics
1. Trace & Debug 理解这两者的区别,Trace有个Listners.Add()非常好用,这里网上有个在ListBox里输出Debug和Trace信息的 using System ...
- springday04-go1
springmvc02:1.创建项目,导入jar包 2.复制xml文件到src下 3.在web.xml中配置DispatcherServlet(代码一致) <?xml version=" ...
- struts局部、全局类型转换器
第01步:编写bean package com.self.bean; import java.util.Date; public class User { private Date birthday ...
- Sqlserver列出所有数据库名,表名,字段名
Sqlserver列出所有数据库名,表名,字段名 1.获取所有数据库名: ? 1 SELECT Name FROM Master..SysDatabases ORDER BY Name 注 ...