Borg Maze(MST & bfs)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9220 | Accepted: 3087 |
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
Source
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int col , row , cnt;
char maze[][] ;
int l[][] ;
int vis[][] ;
int map[][] ;
const int inf = 0x3f3f3f3f ;
int move[][] = { , , , , - , , , -} ; void bfs (int sx , int sy)
{
queue <pair <int , int> > q ;
while (!q.empty ())
q.pop () ;
memset (vis , - , sizeof(vis)) ;
vis[sx][sy] = ;
q.push (make_pair(sx , sy)) ;
while (!q.empty ()) {
pair <int , int> k = q.front () ;
q.pop () ;
if (l[k.first][k.second] != -)
map [l[sx][sy]] [l[k.first][k.second]] = vis [k.first][k.second] ;
for (int i = ; i < ; i++) {
int tx = k.first + move[i][] ;
int ty = k.second + move[i][] ;
if (maze[tx][ty] == '#' || vis[tx][ty] != -)
continue ;
vis[tx][ty] = vis[k.first][k.second] + ;
q.push (make_pair(tx , ty)) ;
}
}
} void prim ()
{
int p[] , d[] ;
for (int i = ; i < cnt ; i++) {
d[i] = map[][i] ;
p[i] = ;
}
d[] = ;
int ans = ;
for (int i = ; i < cnt - ; i++) {
int minc = inf , k ;
for (int j = ; j < cnt ; j++) {
if (d[j] && d[j] < minc) {
minc = d[j] ;
k = j ;
}
}
d[k] = ;
for (int j = ; j < cnt ; j++) {
if (d[j] && d[j] > map[k][j]) {
d[j] = map[k][j] ;
p[j] = k ;
}
}
ans += minc ;
}
printf ("%d\n" , ans) ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin) ;
int T ;
scanf ("%d" , &T) ;
while (T--) {
scanf ("%d%d" , &col , &row) ;
gets(maze[]) ;
int tol = ;
memset (l , - , sizeof(l)) ;
for (int i = ; i < row ; i++) {
gets (maze[i]) ;
for (int j = ; j < col ; j++) {
if (maze[i][j] == 'A' || maze[i][j] == 'S') {
l[i][j] = tol++ ;
}
}
}
for (int i = ; i < row ; i++) {
for (int j = ; j < col ; j++) {
if (l[i][j] != -) {
bfs (i , j);
}
}
}
cnt = tol ;
prim () ;
}
return ;
}
这道题有巨坑,收空格一定要用gets , 我用getchar RE了一个下午
Borg Maze(MST & bfs)的更多相关文章
- Borg Maze(BFS+MST)
Borg Maze http://poj.org/problem?id=3026 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- 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 ...
- POJ3026 Borg Maze(Prim)(BFS)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12729 Accepted: 4153 Descri ...
- 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
题目链接:http://poj.org/problem?id=3026. Description The Borg is an immensely powerful race of enhanced ...
- poj 3026 Borg Maze (最小生成树+bfs)
有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...
随机推荐
- JS判断输入是否为整数和数字的正则表达式
"^\\d+$" //非负整数(正整数 + 0) "^[0-9]*[1-9][0-9]*$" //正整数 "^((-\\d+)|(0+))$" ...
- nodejs初学————安装篇(iis8.5+windows8.1)
nodejs很久前就想玩玩,不过一直没时间,昨晚花了4个小时来捣鼓到iis上架设成功了,废话不说了. PS:我的系统是windows8.1 x64,所以自带iis8.5的,下载的文件也是x64的. N ...
- 按照需要分别率长宽比导出图片(python 3)
效率提升的问题 之前朋友需要把大量的图片用分辨率进行区分查找,他说都是打开图片,然后用尺子在屏幕上量......我也是瀑布汗....花的点时间帮他写的小软件,解决这个蛋疼的问题 解决方案 本想用批处理 ...
- 自动备份SQL数据库到云存储Storage
如何自动备份SQL数据库到Storage呢. 前提条件需要SQL Server2012 SP1 CU2或更高版本 1. 备份SQL Azure数据库到云存储Storage 1)在SQL Server ...
- Daily English
- EntityFramework_MVC4中EF5 新手入门教程之六 ---6.通过 Entity Framework 更新关联数据
在前面的教程中,您将显示相关的数据 :在本教程中,您会更新相关的数据.对于大多数的关系,这个目标是可以通过更新相应的外键字段来达到的.对于多对多关系,实体框架并不直接,暴露联接表,因此您必须显式添加和 ...
- jQuery应用之(二)使用jQuery管理选择结果(荐)
使用jQuery选择出来的元素与数组非常类似,可以通过jQuery提供的一系列方法对其进行处理,包括长度.查找某个元素,截取某个段落等. 1.获取元素的个数. 在jQuery中可以通过size()方法 ...
- 由“js跨域”想到"AJAX也不一定要XMLHttpRequest"
关键字:jsonp jsonp的原理:同源约束限制了js脚本的跨域访问,但是<script>和<iframe>的src标签引用的js文件(只要响应正文是符合js语法的文本即可, ...
- php 字符串的一些操作,以便记忆
php 字符串的操作 trim($str,'特殊字符')-----去除字符串左右两边的字符,返回字符串 ltrim(),rtrim()--------------------左,由两边,与trim() ...
- oracle-2-sql数据操作和查询
主要内容: >oracle 数据类型 >sql建表和约束 >sql对数九的增删改 >sql查询 >oracle伪例 1.oracle的数据类型 oracle数据库的核心是 ...