poj 3026Borg Maze
http://poj.org/problem?id=3026
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define maxn 100
#define maxn1 100000
#include<queue>
using namespace std; char s[maxn][maxn];
const int inf=<<;
int n,m;
bool vis[maxn*][maxn*];
bool visi[maxn*];
int dis[maxn*][maxn*];
int c[maxn*][maxn*];
int dir[][]={{,},{-,},{,},{,-}};
int dist[maxn*];
int ans; struct node
{
int x,y;
int step;
}p[maxn1],st1,st; void bfs(struct node st2)
{
st2.step=;
queue<node>q;
q.push(st2);
memset(vis,false,sizeof(vis));
vis[st2.x][st2.y]=true;
while(!q.empty())
{
st1=q.front();
q.pop();
for(int i=; i<; i++)
{
int xx=st1.x+dir[i][];
int yy=st1.y+dir[i][];
if(xx>=&&xx<m&&yy>=&&yy<n&&!vis[xx][yy]&&s[xx][yy]!='#')
{
if(s[xx][yy]=='S'||s[xx][yy]=='A')
{
dis[c[st2.x][st2.y]][c[xx][yy]]=st1.step+;
st.x=xx;
st.y=yy;
st.step=st1.step+;
q.push(st);
vis[xx][yy]=true;
}
else
{
st.x=xx;
st.y=yy;
st.step=st1.step+;
q.push(st);
vis[xx][yy]=true;
}
}
}
}
} bool prime(int num)
{
memset(visi,false,sizeof(visi));
for(int i=; i<=num; i++)
{
dist[i]=inf;
}
ans=;dist[]=;
for(int i=; i<num; i++)
{
int temp=inf,k=;
for(int j=; j<num; j++)
{
if(!visi[j]&&dist[j]<temp)
{
temp=dist[j];
k=j;
}
}
if(temp==inf) return false;
visi[k]=true;
ans+=temp;
for(int j=; j<num; j++)
{
if(!visi[j]&&dist[j]>dis[k][j])
dist[j]=dis[k][j];
}
}
return true;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int num=;
char s1[];
scanf("%d%d",&n,&m);
gets(s1);
memset(vis,false,sizeof(vis));
memset(c,-,sizeof(c));
for(int i=; i<m; i++)
{
gets(s[i]);
for(int j=; j<n; j++)
{
if(s[i][j]=='A'||s[i][j]=='S')
{
p[num].x=i;
p[num].y=j;
c[i][j]=num;
num++;
}
}
}
for(int i=; i<num; i++)
{
bfs(p[i]);
}
prime(num);
printf("%d\n",ans);
}
return ;
}
poj 3026Borg Maze的更多相关文章
- POJ 2157 Maze
Maze Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3183 Accepted: 996 Description A ...
- {POJ}{3897}{Maze Stretching}{二分答案+BFS}
题意:给定迷宫,可以更改高度比,问如何使最短路等于输入数据. 思路:由于是单调的,可以用二分答案,然后BFS验证.这里用优先队列,每次压入也要进行检查(dis大小)防止数据过多,A*也可以.好久不写图 ...
- poj 3897 Maze Stretching 二分+A*搜索
题意,给你一个l,和一个地图,让你从起点走到终点,使得路程刚好等于l. 你可以选择一个系数,把纵向的地图拉伸或收缩,比如你选择系数0.5,也就是说现在上下走一步消耗0.5的距离,如果选择系数3,也就是 ...
- 【bfs】 poj 3984 maze 队列存储
#include <iostream> #include <stdio.h> #include <cstring> #define Max 0x7f7f7f7f u ...
- 搜索 || BFS || POJ 2157 Maze
走迷宫拿宝藏,拿到所有对应的钥匙才能开门 *解法:从起点bfs,遇到门时先放入队列中,取出的时候看钥匙够不够决定开不开门,如果不够就把它再放回队列继续往下走,当队列里只有几个门循环的时候就可以退出,所 ...
- BFS广搜题目(转载)
BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 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 ...
随机推荐
- typedef struct
突然忘了这玩意儿了..今天就来搞一发 typedef是类型定义的意思.typedef struct 是为了使用这个结构体方便.具体区别在于:若struct node {}这样来定义结构体的话.在申请n ...
- OpengGL ES2.0 Using NDK
使用C语言在Android Studio中编写OpenGL ES,首要的任务就是配置编程环境. 在最新的Android Studio中,可以直接编译C/C++源代码.本人的版本是Android Stu ...
- Java model 对象处理
public class BaseFlowBo { /** * 流量 */ private String flow; /** * Pv */ private String pv; /** * CTR ...
- oracle暂时表空间 ORA-01652:无法通过16(在表空间XXX中)扩展 temp 字段
今天在查数据的时候报错 ORA-01652:无法通过16(在表空间temp1中)扩展 temp 字段 查看表空间使用明细 SELECT b.tablespace, b.segfile# ...
- 使用 Java 配置进行 Spring bean 管理--转
概述 众所周知,Spring 框架是控制反转 (IOC) 或依赖性注入 (DI) 模式的推动因素,而这种推动是通过基于容器的配置实现的.过去,Spring 允许开发人员使用基于 XML 的配置,通过利 ...
- Oracle中的over(partition by...)分析函数及开窗函数
假设有一张表student Name Score InsertTime (Name:姓名 Score:成绩 InsertTime:考试时间) 张三 20 2015-08-08 ...
- C# 带参访问接口,WebClient方式
1.当参数的数据较大时.WebClient同步. //实例化WebClient client = new WebClient();//地址 string path = "http://oa. ...
- Android - Layout时发生'Unfortunately xxx has stopped'
概述 我在进行LinearLayout和TableLayout的嵌套布局的时候,发生题的错误.如下布局xml代码: <LinearLayout xmlns:android="http: ...
- Oracle 用户(user)和模式(schema)的区别
概述: (一)什么Oracle叫用户(user): A user is a name defined in the database that can connect to and access ob ...
- 加入强调语气,使用<strong>和<em>标签
有了段落又有了标题,现在如果想在一段话中特别强调某几个文字,这时候就可以用到<em>或<strong>标签. 但两者在强调的语气上有区别:<em> 表示强调,< ...