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 ...
随机推荐
- 【笔记】Loadrunner添加OS类型为Linux的服务器
参考文章:http://www.51testing.com/?uid-145985-action-spacelist-type-blog-itemtypeid-11954 在给Loadrunner添加 ...
- 【Loadrunner】初学Loadrunner——安装
一.准备工作 1.下载Loadrunner可以参考网上百度得到的可以在下面这个地址下载,比较大,4G左右 http://www.genilogix.com/downloads/loadrunner/l ...
- 工作中用到的简单linux命令
1.rpm包查询.卸载.安装: rpm包查询 rpm -q 包名(不带版本号.后缀等) q----query rpm包卸载 rpm -e 包名(不带版本号.后缀等)e----erase rpm包安装 ...
- DirectShow音频采集声音不连续问题分析与解决办法经验总结
最近广州大雨不断,并且多数无前兆,突然就来场大雨,给同学们降降温,说来本也是好事,但有时候下的真不是时候,最近这段时间都是即将下班了,大雨就来了,昨晚快下班前又出现了大雨,北方人总爱忘带雨伞,这不就被 ...
- 项目中 添加 swift代码 真机调试 错误
错误: dyld: Library not loaded: @rpath/libswiftCore.dylib Referenced from: /private/var/mobile/Contain ...
- zstu 4215 多起点bfs
input n m 1<=n,m<=1000 n*m的地图,全为大写字母 7 10 WWWWWCCDEW WWWWCCEEEW WTWWWCCCCW WWFFFFFFWW WWFAAAA ...
- Ext 下拉框联动第一次显示不正常的问题
做下拉框联动,异步加载数据,第一次显示时数据不准确,不要在combo_2的下拉框直接绑定store,在combo_1的改变事件里调用下面的方法 function GetAllCustomerBrand ...
- HDU1115--Lifting the Stone(求凸多边形的重心)
Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...
- ExecutorService的submit(Runnable x)和execute(Runnable x) 两个方法的本质区别
Runnable任务没有返回值,而Callable任务有返回值.并且Callable的call()方法只能通过ExecutorService的submit(Callable <T> tas ...
- DOM操作-引用同级的元素
代码: ———————————————————————————————— <script type="text/javascript"> //获取 ...