POJ - 3062 Borg Maze
题目链接:http://poj.org/problem?id=3026
Svenskt Masterskap我程序员/ Norgesmesterskapet 2001
Description
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
Output
Sample Input #####
#A#A##
# # A#
#S ##
##### #####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<math.h>
#include<queue>
#include<map>
using namespace std; #define INF 0x3f3f3f3f
#define N 1200 int n,m,maps[N][N],b[N][N],dist[N],vis[N][N],viss[N];
char s[N][N];
int dir[][]={ {,},{,-},{,},{-,} }; struct node
{
int x,y,step;
}a[N]; void Init()
{
int i,j;
for(i=;i<m*n;i++)
dist[i]=INF;
for(j=;j<n*m;j++)
maps[i][j]=(i==j)?:INF;
} void bfs(int ss,int x,int y)
{
int i; memset(vis,,sizeof(vis));
queue<node>Q;
node B,Next,Now;
B.x=x;
B.y=y;
B.step=;
Q.push(B);
vis[x][y]=; while(Q.size())
{
Now=Q.front();
Q.pop(); if(s[Now.x][Now.y]>='A'&&s[Now.x][Now.y]<='Z')
maps[ss][b[Now.x][Now.y]]=Now.step; for(i=;i<;i++)
{
Next.x=Now.x+dir[i][];
Next.y=Now.y+dir[i][];
if(Next.x<m&&Next.x>=&&Next.y<n&&Next.y>=&&vis[Next.x][Next.y]==&&s[Next.x][Next.y] != '#')
{
vis[Next.x][Next.y]=;
Next.step=Now.step+;
Q.push(Next);
}
}
}
} int Prim(int e)
{
int i,j;
viss[]=;
for(int i=;i<=e;i++)
dist[i]=maps[][i];
int sum=;
for(i=;i<=e;i++)
{
int Min=INF,index=-; for(j=;j<=e;j++)
if(!viss[j]&&Min>dist[j])
{
Min=dist[j];
index=j;
}
if(index==-)break;
sum+=Min;
viss[index]=; for(j=;j<=e;j++)
if(!viss[j]&&dist[j]>maps[index][j])
dist[j]=maps[index][j];
} return sum;
} int main()
{
int T,i,j; scanf("%d", &T); while(T--)
{
memset(a,,sizeof(a));
memset(viss,,sizeof(viss)); scanf("%d%d ", &n,&m);
Init(); int cnt=;
for(i=;i<m;i++)
{
gets(s[i]);
for(j=;j<n;j++)
{
if(s[i][j]<='Z'&&s[i][j]>='A')
b[i][j]=cnt++;///记录cnt号动点位置
}
} for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
if(s[i][j]<='Z'&&s[i][j]>='A')
bfs(b[i][j],i,j);///搜索,记录各个点之间步数
}
} int ans=Prim(cnt-);///最小生成树求最终值
printf("%d\n", ans);
}
}
POJ - 3062 Borg Maze的更多相关文章
- POJ 3026 Borg Maze(bfs+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- POJ 3026 Borg Maze【BFS+最小生成树】
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ - 3026 Borg Maze BFS加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- poj 3026 Borg Maze 最小生成树 + 广搜
点击打开链接 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7097 Accepted: 2389 ...
- POJ 3026 Borg Maze (最小生成树)
Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...
- POJ 3026 Borg Maze
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7998 Accepted: 2675 Descrip ...
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16625 Accepted: 5383 Descri ...
随机推荐
- 利用POI获取Excel中图片和图片位置
利用POI获取Excel中图片和图片位置(支持excel2003or2007多sheet) 转自:http://blog.csdn.net/delongcpp/article/details/8833 ...
- 一个简单的servlet小程序
servlet是不能单独运行的,他是运行在web服务器或应用服务器上的java程序,或者可以说是在servlet容器上运行的,我们经常使用到的tomcat就是一个servlet容器. 他是处理HTTP ...
- 2015 Multi-University Training Contest 10
1001 CRB and Apple 1002 CRB and Candies 1003 CRB and Farm 1004 CRB and Graph 1005 CRB and His Birthd ...
- go语法
背景 go语言算是比较常用的开发语言了,但是我发现自己在写代码的时候仍无法做到熟练掌握语法的程度,这个博客是我在因为语法不熟练而必须停下来的地方,整理下来方便查阅和记忆. 数组 ]int //arra ...
- CentOS 6.4 系统下的MySQL的主从库配置
首先了解到一. 二一.(MySQL下创建用户并且赋予权限)root用户创建yong用户的SQL语句 CREATE USER 'yong'@'localhost' IDENTIFIED BY 'yong ...
- MapReduce初级案例
1.数据去重 "数据去重"主要是为了掌握和利用并行化思想来对数据进行有意义的筛选.统计大数据集上的数据种类个数.从网站日志中计算访问地等这些看似庞杂的任务都会涉及数据去重.下面就 ...
- iosOC不可变字典和可变字典
//key 和 value 都属于(id)对象类型 //key常用字符串NSString来表示 //存储数值型 一般可用 NSString //int age ->@(age) // [di ...
- 反编译app方法
如果你没有代码,那么可以反编译该app. 这里将用到2个工具,分别是dex2jar和jd-gui.你可以在这里下载目前为止的最新版本以及示例apk. 我们以工具包里的ContactManager.ap ...
- hdu_3709_Balanced Number(数位DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题意:给你一个区间,让你找平衡数的个数 题解:设dp[i][j][k]为前i位以第j位为支撑点的 ...
- mybatis 做 insert操作的时候返回插入的那条数据的id
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:吃丸子的小鹿链接:http://www.zhihu.com/question/20810321/answer/16843223 ...