最小生成树+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点. 思路: 感觉就算 ...
随机推荐
- May Challenge 2019 Division 2 水题讲解
Reduce to One 这题其实蛮水的? 题意就是说: 给定一个 1~n 的序列,每次挑两个数 x y 合并,合并值为 \(x+y+xy\) ,然后求不断合并最后剩下的一个的最大值 随便搞搞发现答 ...
- 使用Oracle实现的MyBatis分页查询效果
1.mybatis.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configur ...
- Scala-基础-函数(2)
import junit.framework.TestCase //函数(2) //知识点-默认参数,带名参数,变长参数,过程 class Demo1 extends TestCase { //测试方 ...
- php-5.3源码编译autoconf版本不符合解法
1. 网上下载符合版本的autoconf 2. 卸载本地原本的autoconf 3. 解压autoconf 后进入目录 ./configure && make && s ...
- hdu5122 K.Bro Sorting
思路: 模拟. 实现: #include <iostream> #include <cstdio> using namespace std; ], n, t; int main ...
- TOP5_3:定制简单的进度条
结构: Activity: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xm ...
- (转)中国电信友华PT921、PT921G光猫设置路由,无线WIFI设置
中国电信友华PT921.PT921G光猫设置路由,无线WIFI设置. 第一步,用管理员帐号密码登陆,打开浏览器,输入http://192.168.1.1 登陆帐号:telecomadmin登陆密码:n ...
- Windows离线安装Python第三方库的方法
在window中,离线安装第三方模块, 1.下载第三方库的压缩文件,解压,将解压后的文件放到Python安装目录下的Lib\site_packages中 2. 将Python添加到环境变量里 3.进入 ...
- HDU_2079_(01背包)(dfs)
选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Crash reporter
A crash reporter is a software application whose function is to identify report crash details and to ...