POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 12086 | Accepted: 3953 |
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
做法:直接用BFS算出每个A或者S到其他A或者S的距离,建出图后用Prim算法直接做就可以了。(有一个坑点就是题目的Input貌似有点错误,输入完两个整数后需要再输入一个串再输入图。我也是看了Discuss才知道,还有个奇奇怪怪的地方,有人知道什么回事请告诉下我,谢谢)。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 210
#define INF 1000000000
struct node
{
int x,y;
};
//node alien[210]; //这句是我之前写留下来的,可是不知道为什么删了之后就WA了,如果有人知道请务必告诉我(现在发现原来交C++就会错,交G++就不会了,不知道什么情况呀)
char maze[][];
int mp[][],low[],used[],vis[][],d[][];
int n,m,dx[]={,-,,},dy[]={,,,-},cnt; bool check(int x,int y)
{
if(d[x][y]==-&&maze[x][y]!='#'&&<=x&&x<=m&&<=y&&y<=n) return true;
else return false;
} void bfs(int sx,int sy)
{
queue<node> que;
while(!que.empty()) que.pop();
memset(d,-,sizeof(d));
node a;
a.x=sx;a.y=sy;
d[a.x][a.y]=;
que.push(a);
while(!que.empty()){
node b;
a=que.front();que.pop();
if(d[a.x][a.y]!=-){
mp[vis[sx][sy]][vis[a.x][a.y]]=d[a.x][a.y];
}
for(int k=;k<;k++){
int nx=dx[k]+a.x,ny=dy[k]+a.y;
if(check(nx,ny)){
b.x=nx,b.y=ny,d[b.x][b.y]=d[a.x][a.y]+;
que.push(b);
}
}
}
} int Prim()
{
int pos=,res=;
for(int i=;i<cnt;i++) low[i]=mp[pos][i];
for(int i=;i<cnt;i++){
int mi=INF;
for(int j=;j<cnt;j++){
if(!used[j]&&low[j]<mi){
mi=low[j],pos=j;
}
}
used[pos]=;
res+=low[pos];
for(int j=;j<cnt;j++){
if(!used[j]&&low[j]>mp[pos][j]){
low[j]=mp[pos][j];
}
}
}
return res;
} int main()
{
int t;
cin>>t;
while(t--){
memset(used,,sizeof(used));
memset(vis,-,sizeof(vis));
for(int i=;i<;i++)
for(int j=;j<;j++) mp[i][j]=INF;
cin>>n>>m;
char s[];
gets(s); //后面有些奇怪的东西,加上就AC了
cnt=;
for(int i=;i<m;i++){
gets(maze[i]);
} for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(maze[i][j]=='S'||maze[i][j]=='A'){
vis[i][j]=cnt++;
}
}
}
for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(vis[i][j]!=-) bfs(i,j);
}
}
int ans=Prim();
cout<<ans<<endl;
}
return ;
}
2016-05-20
POJ 3026 : Borg Maze(BFS + Prim)的更多相关文章
- POJ 3026 Borg Maze(bfs+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
- Borg Maze(BFS+MST)
Borg Maze http://poj.org/problem?id=3026 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- 【POJ 3026】Borg Maze
id=3026">[POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队能够无限切割 问搜索到全部alien所须要的总步数 即求一个无向图 包括全部的点而且总权值 ...
- POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16625 Accepted: 5383 Descri ...
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- POJ 2796:Feel Good(单调栈)
http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://ww ...
- POJ 3318:Matrix Multiplication(随机算法)
http://poj.org/problem?id=3318 题意:问A和B两个矩阵相乘能否等于C. 思路:题目明确说出(n^3)的算法不能过,但是通过各种常数优化还是能过的. 这里的随机算法指的是随 ...
- POJ 1200:Crazy Search(哈希)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32483 Accepted: 8947 Des ...
- J - Borg Maze - poj 3026(BFS+prim)
在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, *************** ...
随机推荐
- Oracle数据库之SQL基础(二)
一.约束 ❤ 1.约束概述 约束作用: (1)定义规则 (2)确保完整性:包括数据的精确性.可靠性.以确保数据不会出错,或者尽量减少出错. 约束的类型: (1)非空约束 (2)主键约束 (3)外键约束 ...
- [RGeos]手簿
1.屏幕坐标以像素为单位,地图坐标通常以米为单位,CAD制图默认以毫米为单位. DPI是“dot per inch”的缩写.顾名思义,就是指在每英寸长度内的点数.通常,我们都使用dpi来作为扫描器和打 ...
- Perl Debug error: SetConsoleMode failed, LastError=|6|
Windows Strawberry Perl. 解决办法: 1. 设置环境变量 TERM = dumb 2. 重启 CMD 参考资料: http://padre.perlide.org/trac/t ...
- RPC、SQL、NFS属于OSI的哪一层
第一层:物理层 第二层:数据链路层 802.2.802.3ATM.HDLC.FRAME RELAY 第三层:网络层 IP.IPX.ARP.APPLETALK.ICMP 第四层:传输层 TCP.UDP. ...
- 使用duplicate target database ... from active database复制数据库
使用duplicate target database ... from active database复制数据库 source db:ora11auxiliary db:dupdb 1.修改监听文件 ...
- eclipse下部署web工程的两种方式
习惯了Eclipse 的开发,就觉得不想那么懒去用MyEclipse傻瓜式的部署工程. 第一种,手动部署工程. 情况一:如果工程目录在tomcat目录的webapp目录下,这种情况就不需要有工程部署的 ...
- Java基础(32):String与StringBuilder、StringBuffer的区别(String类)
在Java中,除了可以使用 String 类来存储字符串,还可以使用 StringBuilder 类或 StringBuffer 类存储字符串,那么它们之间有什么区别呢? String 类具有是不可变 ...
- java中hashCode()方法的作用
hashcode方法返回该对象的哈希码值. hashCode()方法可以用来来提高Map里面的搜索效率的,Map会根据不同的hashCode()来放在不同的位置,Map在搜索一个对象的时候先 ...
- java经典小算法
package com.shb.java; public class Demo4 { /**时间有限 先不写文字了 自己随便敲的 * @param args * @author shaobn */ p ...
- 开发系统时候运行程序突然报出“WebDev.WebServer40.exe已停止工作”的错误
已经解决,问题描述:在开发系统时候运行程序突然报出“WebDev.WebServer40.exe已停止工作”的错误,程序调试运行,发现程序在打开数据库时候报错,也就是Connection.Open() ...