(最小生成树) Borg Maze -- POJ -- 3026
链接:
http://poj.org/problem?id=3026
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#problem/J
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10713 | Accepted: 3559 |
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
代码:
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int maxn = ;
const int oo = 0xfffffff; int dir[][] = { {,},{,},{-,},{,-} };
char G[maxn][maxn]; //保存地图
int D[maxn][maxn]; //记录两点间的距离
int use[maxn][maxn]; //标记地图
int Index[maxn][maxn]; //记录‘A’或者‘B’的编号
struct node{int x, y, step;}; void BFS(int k, int M,int N, int x, int y)
{
queue<node> Q;
node s;
s.x = x, s.y = y, s.step = ;
use[s.x][s.y] = k; Q.push(s); while(Q.size())
{
s = Q.front();Q.pop();
if(G[s.x][s.y]>='A' && G[s.x][s.y] <='Z')
D[k][ Index[s.x][s.y] ] = s.step; for(int i=; i<; i++)
{
node q = s;
q.x += dir[i][], q.y += dir[i][]; if(q.x>=&&q.x<M && q.y>=&&q.y<N && G[q.x][q.y]!='#' && use[q.x][q.y]!=k)
{
use[q.x][q.y] = k;
q.step += ;
Q.push(q);
}
}
}
}
int Prim(int N) //这里面的N代表编号最多到N
{
int i, dist[maxn], vis[maxn]={, };
int ans = , T=N-; for(i=; i<=N; i++)
dist[i] = D[][i]; while(T--)
{
int k=, mini = oo; for(i=; i<=N; i++)
{
if(!vis[i] && mini > dist[i])
mini = dist[i], k=i;
}
ans += mini;
vis[k] = true; for(i=; i<=N; i++)
if(!vis[i])dist[i] = min(dist[i], D[k][i]);
} return ans;
} int main()
{
int T; scanf("%d", &T); while(T--)
{
int i, j, M, N, t=; scanf("%d%d ", &N, &M); for(i=; i<M; i++)
{
gets(G[i]);
for(j=; j<N; j++)
{
if(G[i][j]>='A' && G[i][j]<='Z')
Index[i][j] = t++;
use[i][j] = ;
}
} for(i=; i<M; i++)
for(j=; j<=N; j++)
{
if(G[i][j]>='A' && G[i][j]<='Z')
BFS(Index[i][j], M, N, i, j);
} int ans = Prim(t-); printf("%d\n", ans);
} return ;
}
(最小生成树) Borg Maze -- POJ -- 3026的更多相关文章
- Borg Maze POJ - 3026 (BFS + 最小生成树)
题意: 求把S和所有的A连贯起来所用的线的最短长度... 这道题..不看discuss我能wa一辈子... 输入有坑... 然后,,,也没什么了...还有注意 一次bfs是可以求当前点到所有点最短距离 ...
- Borg Maze poj 3026
Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...
- Borg Maze - poj 3026(BFS + Kruskal 算法)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9821 Accepted: 3283 Description The B ...
- J - Borg Maze - poj 3026(BFS+prim)
在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用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 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- POJ 3026 Borg Maze(bfs+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
- poj 3026 Borg Maze 最小生成树 + 广搜
点击打开链接 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7097 Accepted: 2389 ...
- POJ 3026 Borg Maze (最小生成树)
Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...
随机推荐
- UGUI双击事件
经测试在Android.ios平台下无效 using UnityEngine; using UnityEngine.EventSystems; using System.Collections; us ...
- 设计模式学习笔记(1)Iterator
Iterator 模式 public interface Iterator { public boolean hasNext(); public Object next(); } public int ...
- 把AVI存在资源中用TAnimate播放
Animate1.RESName := 'About'; Animate1.Active := True;
- js中常用的事件
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 吴裕雄 实战python编程(3)
import requests from bs4 import BeautifulSoup url = 'http://www.baidu.com'html = requests.get(url)sp ...
- 【Java】JVM(六)虚拟机字节码执行引擎
一.概述 执行引擎是虚拟机中最核心的部分之一, 虚拟机自己实现引擎,自己定义指令集和执行引擎的结构体系. 二.栈帧 栈帧包含(1)局部变量表.(2)操作数栈.(3)动态链接.(4)方法返回地址.(5) ...
- git cherry-pick用法
场景: 如果你的应用已经发布了一个版本2.0, 代码分支叫release-2.0, 现在正在开发3.0, 代码的分支叫dev-3.0. 那么有一天产品说, 要把正在开发的某个特性提前上线, 也就是说要 ...
- spring-boot PageHelper
分页插件PageHelper 参看了pagehelper-spring-boot,使用起来非常放方便,关于更多PageHelper可以点击https://github.com/pagehelper/M ...
- 121. Best Time to Buy and Sell Stock (Array;DP)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- DAO层注入HibernateTemplate的两种方式
-------------------------siwuxie095 DAO 层注入 HibernateTemplat ...