POJ 3026 Borg Maze(bfs+最小生成树)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6634 | Accepted: 2240 |
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
2
6 5
#####
#A#A##
# # A#
#S ##
#####
7 7
#####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
8
11
Source
//============================================================================
// Name : POJ.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
using namespace std; char g[][];
int n,m;
int a[][];
int move[][]={{,},{-,},{,},{,-}}; int cost[][];
int t[][];
void bfs(int sx,int sy)
{
queue<pair<int,int> >q;
while(!q.empty())q.pop();
memset(t,-,sizeof(t));
t[sx][sy]=;
q.push(make_pair(sx,sy));
while(!q.empty())
{
pair<int,int> now=q.front();
q.pop();
if(a[now.first][now.second]!=-)
cost[a[sx][sy]][a[now.first][now.second]]=t[now.first][now.second];
for(int i=;i<;i++)
{
int tx=now.first+move[i][];
int ty=now.second+move[i][];
if(g[tx][ty]=='#'||t[tx][ty]!=-)continue;
t[tx][ty]=t[now.first][now.second]+;
q.push(make_pair(tx,ty));
}
}
}
const int INF=0x3f3f3f3f;
bool vis[];
int lowc[];
int Prim(int n)
{
int ans=;
memset(vis,false,sizeof(vis));
vis[]=true;
for(int i=;i<n;i++)lowc[i]=cost[][i];
for(int i=;i<n;i++)
{
int minc=INF;
int p=-;
for(int j=;j<n;j++)
if(!vis[j]&&minc>lowc[j])
{
minc=lowc[j];
p=j;
}
if(minc==INF)return -;
ans+=minc;
vis[p]=true;
for(int j=;j<n;j++)
if(!vis[j]&&lowc[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[]);
memset(a,-,sizeof(a));
int tol=;
for(int i=;i<n;i++)
{
gets(g[i]);
for(int j=;j<m;j++)
if(g[i][j]=='A'||g[i][j]=='S')
a[i][j]=tol++;
}
for(int i=;i<n;i++)
for(int j=;j<n;j++)
if(a[i][j]!=-)
bfs(i,j);
printf("%d\n",Prim(tol));
}
return ;
}
POJ 3026 Borg Maze(bfs+最小生成树)的更多相关文章
- poj 3026 Borg Maze (bfs + 最小生成树)
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
- POJ - 3026 Borg Maze bfs+最小生成树。
http://poj.org/problem?id=3026 题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地.求从S出发,经过所有A所需要的最短路.你有一个特殊能力,当走到 ...
- 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加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- 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(最小生成树+bfs)
题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显 ...
- poj 3026 Borg Maze bfs建图+最小生成树
题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...
- POJ 3026 Borg Maze bfs+Kruskal
题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...
- POJ - 3026 Borg Maze(最小生成树)
https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...
- POJ 3026 Borg Maze【BFS+最小生成树】
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
随机推荐
- $.post()
定义和用法 post() 方法通过 HTTP POST 请求从服务器载入数据. jQuery.post(url,data,success(data, textStatus, jqXHR),dataTy ...
- Android 中Activity生命周期分析(二):从AActivity 到BActivity过程分析
如果你没有动手去演示的话,你一定要去动手试试看,这个东西非学容易出错,面试中经常出现,好了,上代码: package com.king.review.base; import android.app. ...
- perl基本语法
标量 标量是 Perl 中最简单的数据类型.大多数的标量是数字(如 255 或 3.25e20)或者字符串(如 hello或者盖茨堡地址). 数字 perl中所有数字内部的格式都是双精度浮点数. 浮点 ...
- 函数xdes_calc_descriptor_page
根据偏移量计算出第几个xdes page 0 %16328 = 0 64% 16328 = 64 128 % 16328 = 128 192 % 16328 = 192 /************** ...
- JAVA调用易信接口向指定好友推送消息(二)POST测试
易信的API接口做的还算简单 http://open.yixin.im/document/oauth/api 根据指南上的步骤,利用易信提供的测试ID AppID(client_id): yxbbd0 ...
- Flexigrid自定义显示数据列
近期在搞ExtJs,发现ExJs的Grid相当的强大,后来又搞Jquery时,就对原来的表格不怎么满意了,于是,花了点时间,从网上找了个Grid插件,这个插件功能是比较强大,什么行排序.筛选.分页都有 ...
- eval绑定decimal数据后,如何去掉后面没有意义的0?
假如有个数字是 25.00 就应该只显示 25 ,而如果是25.3 则还是显示 25.3 Score.ToString("g0") 这样就可以去掉 decimal 后面多 ...
- svn 提交失败 更新失败 提示 已经锁定
出现问题的原因:在上传的时候,由于网络掉线,导致svn提交到一半就没有反应了,这个时候我点击了取消,再之后无论是进行 更新还是提交,都提示 已经锁定 解决方法:在项目的空白地方,点击SVN 清理 ...
- android开发调用c++共享库so文件
1.编写libaab.cpp #include <stdio.h>#include <stdlib.h> #ifdef __cplusplusextern "C&qu ...
- busybox filesystem httpd php-5.5.31 sqlite3 webserver
/******************************************************************** * busybox filesystem httpd php ...