最小生成树+BFS J - Borg Maze
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 对所有S和A建立统统BFS一遍,建立邻接矩阵,然后Prim算法,这是第一次错误代码。没找出哪里错了。。。。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 101
#define INF 0x3f3f3f3f
int dis[MAXN][MAXN],lowcost[MAXN],m,n,tol,dir[][]={{,},{-,},{,},{,-}};
char g[MAXN][MAXN];
bool been[MAXN][MAXN];
struct node
{
int x,y;
};
struct qnode
{
int x,y,step;
};
node a[MAXN];
void BFS(int x,int y,int i)
{
memset(been,false,sizeof(been));
qnode tmp;
tmp.x = x;
tmp.y = y;
tmp.step = ;
been[x][y] = true;
queue<qnode> q;
q.push(tmp);
while(!q.empty())
{
tmp = q.front();
q.pop();
if(g[tmp.x][tmp.y]>=''&&g[tmp.x][tmp.y]<='')
{
dis[i][g[tmp.x][tmp.y]-''] = tmp.step;
}
for(int i=;i<;i++)
{
int tx = tmp.x+dir[i][],ty = tmp.y+dir[i][];
if(tx>=&&ty>=&&tx<n&&ty<m&&g[tx][ty]!='#'&&!been[tx][ty])
{
been[tx][ty] = true;
qnode n_node;
n_node.x = tx;
n_node.y = ty;
n_node.step = tmp.step+;
q.push(n_node);
}
}
}
}
int Prim()
{
int ret = ;
bool vis[MAXN];
memset(vis,false,sizeof(vis));
for(int i=;i<tol;i++)
lowcost[i] = dis[][i];
lowcost[] = ;
vis[] = true;
for(int j=;j<tol;j++)
{
int Minc = INF,k=-;
for(int i=;i<tol;i++)
{
if(!vis[i]&&lowcost[i]<Minc)
{
k = i;
Minc = lowcost[i];
}
}
if(k==-) return -;
vis[k] = true;
ret+=Minc;
for(int i=;i<tol;i++)
{
if(!vis[i]&&dis[k][i]<lowcost[i])
lowcost[i] = dis[k][i];
}
}
return ret;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>m>>n;//n行m列
tol = ;
string str;
for(int i=;i<n;i++)
{
getline(cin,str);
if(str.empty())
{
i--;
continue;
}
for(int j=;j<m;j++)
{
g[i][j] = str[j];
if(str[j]=='A'||str[j]=='S')
{
g[i][j] = ''+tol;
a[tol].x = i;
a[tol++].y = j;
}
}
}
for(int i=;i<tol;i++)
for(int j=;j<tol;j++)
dis[i][j] = INF;
for(int i=;i<tol;i++)
BFS(a[i].x,a[i].y,i);
int ans = Prim();
cout<<ans<<endl;
}
return ;
}
最小生成树+BFS J - Borg Maze的更多相关文章
- J - Borg Maze
J - Borg Maze 思路:bfs+最小生成树. #include<queue> #include<cstdio> #include<cstring> #in ...
- J - Borg Maze - poj 3026(BFS+prim)
在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, *************** ...
- poj 3026 bfs+prim Borg Maze
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9718 Accepted: 3263 Description The B ...
- J - Borg Maze +getchar 的使用注意(二维字符数组的输入)
题目链接: https://vjudge.net/contest/66965#problem/J 具体思路: 首先将每个点之间的最短距离求出(bfs),A 或者 S作为起点跑bfs,这样最短距离就求出 ...
- 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 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 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 ...
- POJ - 3026 Borg Maze BFS加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
随机推荐
- 第三章 K近邻法(k-nearest neighbor)
书中存在的一些疑问 kd树的实现过程中,为何选择的切分坐标轴要不断变换?公式如:x(l)=j(modk)+1.有什么好处呢?优点在哪?还有的实现是通过选取方差最大的维度作为划分坐标轴,有何区别? 第一 ...
- less新手入门(五)—— CssGuards、循环、合并
九. CssGuards 警卫也可以应用于css选择器,这是一种语法糖,用于声明mixin,然后立即调用它. 例如,在1.5.0之前,您必须这样做 .my-optional-style() when ...
- java 并发编程 Executor框架
http://blog.csdn.net/chenchaofuck1/article/details/51606224 demo package executor; import java.util. ...
- jsonp 监控简陋代码
url: window.location.href Agent: navigator.userAgent var tkInfo = { VisitUrl: window.location.href, ...
- C#命名空间 using的用法
using的用法: 1. using指令:引入命名空间 这是最常见的用法,例如: using System; using Namespace1.SubNameSpace; 2. using stati ...
- N - Binomial Showdown (组合数学)
Description In how many ways can you choose k elements out of n elements, not taking order into acco ...
- 常用点击事件(鼠标、光标、键盘、body)
常用事件: 鼠标: onclick(单击) ondblclick(双击) oncontextmenu(右击) onmouseover onmouseout 光标: onfocus onblur 键盘: ...
- jQuery实现将div中滚动条滚动到指定位置的方法
1.JS代码: onload = function () { //初始化 scrollToLocation(); }; function scrollToLocation() { var mainCo ...
- python加载不了cookirlib模块的问题
Python 3 改成 http.cookiejar了,所以import cookielib只要改成import http.cookiejar,就可以了.
- sp_Msforeachtable与sp_Msforeachdb详解
一.简要介绍: 系统存储过程sp_MSforeachtable和sp_MSforeachdb,是微软提供的两个不公开的存储过程.从mssql6.5开始,存放在SQL Server的MASTER数据 ...