Borg Maze(bfs+prim)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6971 | Accepted: 2345 |
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<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int INF = 0x3f3f3f3f;
int dis[][],vis[][];
int m,n,cnt;
char map[][];
struct node
{
int x,y;
int id;
}cor[];
struct sear
{
int x,y;
int step;
};
queue <sear> que; int bfs(int x, int y,int ex, int ey)
{
memset(vis,,sizeof(vis));
while(!que.empty())
que.pop();
que.push((struct sear){x,y,});
vis[x][y] = ; while(!que.empty())
{
struct sear u = que.front();
que.pop();
if(u.x == ex && u.y == ey)
return u.step;
if(map[u.x-][u.y] != '#' && !vis[u.x-][u.y])
{
vis[u.x-][u.y] = ;
que.push((struct sear){u.x-,u.y,u.step+});
}
if(map[u.x+][u.y] != '#' && !vis[u.x+][u.y])
{
vis[u.x+][u.y] = ;
que.push((struct sear){u.x+,u.y,u.step+});
}
if(map[u.x][u.y-] != '#' && !vis[u.x][u.y-])
{
vis[u.x][u.y-] = ;
que.push((struct sear){u.x,u.y-,u.step+});
}
if(map[u.x][u.y+] != '#' && !vis[u.x][u.y+])
{
vis[u.x][u.y+] = ;
que.push((struct sear){u.x,u.y+,u.step+});
}
}
}
int prim_dis[];
int prim_vis[];
int prim(int id)
{
int i,j;
int ans = ;
memset(prim_vis,,sizeof(prim_vis));
prim_vis[id] = ;
for(i = ; i < cnt; i++)
prim_dis[i] = dis[id][i];
for(i = ; i < cnt; i++)
{
int min = INF,pos;
for(j = ; j < cnt; j++)
{
if(prim_dis[j] < min && !prim_vis[j])
{
min = prim_dis[j];
pos = j;
}
}
prim_vis[pos] = ;
ans += min;
for(j = ; j < cnt; j++)
{
if(!prim_vis[j] && prim_dis[j] > dis[pos][j])
prim_dis[j] = dis[pos][j];
}
}
return ans;
} int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
cnt = ;
scanf("%d %d",&m,&n);
char space[];
gets(space);
for(i = ; i < n; i++)
{
gets(map[i]);
for(j = ; j < m; j++)
{
if(map[i][j] == 'A' || map[i][j] == 'S')
cor[cnt++] = ((struct node){i,j,cnt});
}
}
for(i = ; i < cnt; i++)
{
for(j = ; j < cnt; j++)
{
if(i == j) dis[i][j] = ;
else dis[i][j] = INF;
}
}
for(i = ; i < cnt; i++)
{
for(j = i+; j < cnt; j++)
{
dis[i][j] = dis[j][i] = bfs(cor[i].x,cor[i].y,cor[j].x,cor[j].y);
}
}
printf("%d\n",prim());
}
return ;
}
Borg Maze(bfs+prim)的更多相关文章
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- POJ3026——Borg Maze(BFS+最小生成树)
Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...
- POJ - 3026 Borg Maze BFS加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- POJ 3026 Borg Maze(Prim+bfs求各点间距离)
题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...
- POJ3026 Borg Maze(bfs求边+最小生成树)
Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...
- POJ 3026 Borg Maze bfs+Kruskal
题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...
- poj 3026 Borg Maze bfs建图+最小生成树
题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...
- poj 3026 Borg Maze (bfs + 最小生成树)
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
随机推荐
- Android用户界面概览
用户界面的概观 全部的Android应用程序的用户界面元素都是用View和ViewGroup对象构建的.View就是在手机屏幕上描绘一个能够与用户交互的一个对象.ViewGroup ...
- [ES6] Array.findIndex()
In es5, you can use indexOf to get the index of one item in an array. In es6, you can use findIndex( ...
- String.Split()函数
我们在上次学习到了 String.Join函数(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx),当中用到了String.SPl ...
- cocos2d-x项目过程记录(纹理和内存优化方面)
1.参考资料:Cocos2d-x纹理优化的一些方案 cocos2d-x如何优化内存的应用 iOS和android游戏纹理优化和内存优化(cocos2d-x) 2.加载贴图集纹理 CCSpriteF ...
- Git 版本控制工具使用介绍------Windows系统下使用
Git 是用于 Linux内核开发的版本控制工具.与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持(wingeddevil注:这得分是用什 ...
- 前端的数据库:IndexedDB 。 ps:入门
应用程序需要数据.对大多数Web应用程序来说,数据在服务器端组织和管理,客户端通过网络请求获取.随着浏览器变得越来越有能力,因此可选择在浏览器存储和操纵应用程序数据. 本文向你介绍名为IndexedD ...
- NSIndexPath初始化
在UITableView中经常用到这个类,但一直不知道怎么初始化,网上抄录的代码如下,果然好用 NSIndexPath *index = [NSIndexPath indexPathForRow:0 ...
- 9.2noip模拟试题
题目名称 改造二叉树 数字对 交换 英文名称 binary pair swap 输入文件名 binary.in pair.in swap.in 输出文件名 binary.out pair.out ...
- FineUI属性的简单总结
.PageManager控件— 页面级别的控制(包括主题.语言 等等) 覆盖web.config中自定义结点的配置 EnablePageLoading:是否启用页面的第一次加载提示,默认居中显示加载图 ...
- OJ常见问题及必须认识的对拍处理水题
HDUOJ: 常见问题及解答 Q: Online Judge(以下简称OJ)支持哪些语言? A: 目前为止,HDOJ支持C.C++.Pascal和Java四种语言. Q: 有什么条件判断我的程序是在O ...