POJ3026 Borg Maze(Prim)(BFS)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12729 | Accepted: 4153 |
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
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
Sample Input
2
6 5
#####
#A#A##
# # A#
#S ##
#####
7 7
#####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
8
11
【题意】在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度。
【分析】一开始没懂题意,后来懂了,就是简单的BFS+Prim。但是WA了好几发,伤心至极,去看看Discuss。发现好多人都被坑了,输入法人x,y后面还有好多空格,必须提前gets掉。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
int n,m,t,kk,ii,cnt,edg[N][N],lowcost[N];
int d[][]= {,,,,-,,,-};
int vis[N][N];
int num[N][N];
char w[N][N];
struct man {
int x,y,step;
};
void bfs(man s) {
queue<man>q;
while(!q.empty())q.pop();
q.push(s);
vis[s.x][s.y]=;
while(!q.empty()) {
man t=q.front();
q.pop();
if(w[t.x][t.y]=='A'||w[t.x][t.y]=='S') {
int u=num[s.x][s.y];
int v=num[t.x][t.y];
edg[u][v]=edg[v][u]=t.step;
}
for(int l=; l<; l++) {
int xx=t.x+d[l][],yy=t.y+d[l][];
if(xx>=&&xx<n&&yy>=&&yy<m&&vis[xx][yy]==&&w[xx][yy]!='#') {
man k;
k.x=xx,k.y=yy;
k.step=t.step+;
q.push(k);
vis[xx][yy]=;
}
}
}
}
void Prim() {
for(int i=; i<cnt; i++) {
lowcost[i]=edg[ii][i];
}
lowcost[ii]=-;
int sum=;
for(int i=; i<cnt; i++) {
int minn=inf;
for(int j=; j<cnt; j++) {
if(lowcost[j]!=-&&lowcost[j]<minn) {
minn=lowcost[j];
kk=j;
}
}
sum+=minn;
lowcost[kk]=-;
for(int j=; j<cnt; j++) {
if(edg[j][kk]<lowcost[j]) {
lowcost[j]=edg[j][kk];
}
}
}
printf("%d\n",sum);
}
int main() {
scanf("%d",&t);
while(t--) {
memset(edg,,sizeof(edg));
memset(lowcost,,sizeof(lowcost));
scanf("%d%d",&m,&n);
char tmp[N];gets(tmp);//一定要有这个,这是这个题目最坑的地方
for(int i=; i<n; i++) {
gets(w[i]);
}
cnt=;
for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
if(w[i][j]=='A'||w[i][j]=='S') {
num[i][j]=cnt++;
if(w[i][j]=='S') {
ii=cnt-;
}
}
}
}
for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
if(w[i][j]=='A'||w[i][j]=='S') {
man s;
s.x=i;
s.y=j;
s.step=;
memset(vis,,sizeof(vis));
bfs(s);
}
}
}
Prim();
}
return ;
}
POJ3026 Borg Maze(Prim)(BFS)的更多相关文章
- POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- POJ3026——Borg Maze(BFS+最小生成树)
Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...
- Borg Maze(MST & bfs)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9220 Accepted: 3087 Descrip ...
- POJ 3026 Borg Maze(bfs+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
- POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16625 Accepted: 5383 Descri ...
- POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14165 Accepted: 4619 Descri ...
- Borg Maze(BFS+MST)
Borg Maze http://poj.org/problem?id=3026 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3026 Borg Maze【BFS+最小生成树】
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
随机推荐
- 【NOIP 模拟赛】钟 模拟+链表
biubiu~~ 这道题实际上就是优化模拟,就是找到最先死的让他死掉,运用时间上的加速,题解上说,要用堆优化,也就是这个意思. 对于链表,单项链表和循环链表都不常用,最常用的是双向链表,删除和插入比较 ...
- [lucene系列笔记3]用socket把lucene做成一个web服务
上一篇介绍了用lucene建立索引和搜索,但是那些都只是在本机上运行的,如果希望在服务器上做成web服务该怎么办呢? 一个有效的方法就是用socket通信,这样可以实现后端与前端的独立,也就是不管前端 ...
- Codeforces Round #520 (Div. 2) B. Math
B. Math time limit per test:1 second memory limit per test:256 megabytes Description: JATC's math te ...
- HDU 多校对抗第三场 L Visual Cube
Problem L. Visual Cube Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java ...
- 在eclipse中从gitlab上面下载项目
(1)在eclipse中 import --git--uri--输入用户名密码,下载,这个时候是在本地建立了一个本地仓库 (2)把仓库中的项目导入到eclipse的工作空间中. (3)将所需要的项目转 ...
- tomcat 配置文件中设置JAVA_HOME
Tomcat默认情况下会用系统的环境变量中找到JAVA_HOME和JRE_HOME.但是有的时候我们需要不同版本的JDK共存. 可以在${TOMCAT_HOME}/bin/setclasspath.b ...
- 移动端去掉a标签点击时出现的背景
之前做移动端的Portal时,手机上测试,点击a标签总是出现一个背景框 在CSS中添加 -webkit-tap-highlight-color: rgba(0, 0, 0, 0);就可以了 a:act ...
- ES6学习笔记(二)——数组的扩展
扩展运算符 ... 将数组转化成用逗号分隔的参数序列 * 扩展运算符背后调用的是遍历器接口(Symbol.iterator),如果一个对象没有部署这个接口,就无法转换. 应用 1. 合并数组 2. 将 ...
- 学习正则表达式及c#应用
1.0正则表达式语法 正则表达式是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为“元字符”).模式描述在搜索文本时要匹配的一个或多个字符串. 正则表达式示例 表达式 ...
- redis连接池自动释放
http://blog.itpub.net/29485627/viewspace-1977880/