最小生成树+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点. 思路: 感觉就算 ...
随机推荐
- Codeforces 1131 (div 2)
链接:http://codeforces.com/contest/1131 A Sea Battle 利用良心出题人给出的图,不难看出答案为\(2*(h1+h2)+2*max(w1,w2)+4\)由于 ...
- Python 模拟postman上传文件
最近工作需求:写的程序要用到python模拟postman操作,基于flask框架 代码很简单 但是百度一半天: import requests files = {'skFile': open(r&q ...
- C:\Windows\System32\drivers\etc\hosts文件显示
attrib -s -h C:\Windows\System32\drivers\etc\hosts
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
- CF864D Make a Permutation!
思路: 贪心,构造,模拟. 实现: #include <bits/stdc++.h> using namespace std; ], a[], vis[], n; int main() { ...
- CF848A From Y to Y
思路1: 每次贪心地选择满足i * (i - 1) / 2 <= k最大的i并从k中减去i * (i - 1) / 2,直至k为0.由于函数x * (x - 1) / 2的增长速度比2x要慢,所 ...
- python自动化--mock、webservice及webdriver模拟手机浏览器
一.mock实现 自定义一个类,用来模拟未完成部分的开发代码 class Say(): def say_hello(self): pass 自定义返回值 import unittest from un ...
- Linux 学习(二)
Linux相关命令 命令 说明 startx 当前用户界面切换至图形界面 init5 切换至另一用户的图形化界面 init3 从图形界面切换回文本界面 pwd 显示当前用户路径 logout 注销 s ...
- Angular——自定义过滤器
基本介绍 除了使用AngularJS内建过滤器外,还可以根业务需要自定义过滤器,通过模块对象实例提供的filter方法自定义过滤器. 基本使用 (1)input是将绑定的数据以参数的形式传入 (2)i ...
- 关于vue构建项目的一些指令
第一步: brew install nodejs(MAC机子下) Windows直接官网下载对应版本node.js 第二步: 获取nodejs模块安装目录访问权限(Windows系统忽略)sudo ...