Borg Maze
poj3026:http://poj.org/problem?id=3026
题意:在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度。
题解:一格的长度为1,而且移动的方法只有上、下、左、右,所以在无任何墙的情况下(根据题意的“分离”规则,重复走过的路不再计算因此当使用prim算法求L的长度时,根据算法的特征恰好不用考虑这个问题(源点合并很好地解决了这个问题),L就是最少生成树的总权值W由于使用prim算法求在最小生成树,因此无论哪个点做起点都是一样的,(通常选取第一个点),因此起点不是S也没有关系所以所有的A和S都可以一视同仁,看成一模一样的顶点就可以了剩下的问题关键就是处理 任意两字母间的最短距离,由于存在了“墙#” ,这个距离不可能单纯地利用坐标加减去计算,必须额外考虑,推荐用BFS(广搜、宽搜),这是本题的唯一难点,因为prim根本直接套用就可以了。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>s
#include<queue>
#include<string>
#define INF 10000000
using namespace std;
int xn,yn,top,pos;
int n,m;
char map[][];
int dist[][],lowcost[],g[][],visit[][];
struct Node{
int x;
int y;
int step;
};
struct Node1{
int x;
int y;
}node[];
void init(){
for(int i=;i<=xn;i++)
for(int j=;j<=yn;j++)
dist[i][j]=INF;
memset(visit,,sizeof(visit));
}
void bfs(Node1 a){
init();
dist[a.x][a.y]=;
queue<Node>Q;
Node temp;
temp.x=a.x;
temp.y=a.y;
temp.step=;
visit[a.x][a.y]=true;
Q.push(temp);
while(!Q.empty()){
Node tmp=Q.front();
Q.pop();
int x=tmp.x;
int y=tmp.y;
if(x>=&&map[x-][y]!='#'&&!visit[x-][y]){
visit[x-][y]=true;//bfs收索时候,一定要在入队之前标记,如果在出队
dist[x-][y]=tmp.step+;//时在标记,会使得一个元素在队列中可能会被加入多次。
temp.x=x-;
temp.y=y;
temp.step=tmp.step+;
Q.push(temp);
}
if(x<xn&&map[x+][y]!='#'&&!visit[x+][y]){
visit[x+][y]=true;
dist[x+][y]=tmp.step+;
temp.x=x+;
temp.y=y;
temp.step=tmp.step+;
Q.push(temp);
}
if(y>=&&map[x][y-]!='#'&&!visit[x][y-]){
visit[x][y-]=true;
dist[x][y-]=tmp.step+;
temp.x=x;
temp.y=y-;
temp.step=tmp.step+;
Q.push(temp);
}
if(y<yn&&map[x][y+]!='#'&&!visit[x][y+]){
visit[x][y+]=true;
dist[x][y+]=tmp.step+;
temp.x=x;
temp.y=y+;
temp.step=tmp.step+;
Q.push(temp);
}
}
}
void prim(int v0){
int sum=;
for(int i=;i<top;i++){
lowcost[i]=g[v0][i];
}
lowcost[v0]=-;
for(int i=;i<top-;i++){
int min=INF;
int v=-;
for(int j=;j<top;j++){
if(lowcost[j]<min&&lowcost[j]!=-){
min=lowcost[j];
v=j;
}
}
if(v!=-){
sum+=lowcost[v];
lowcost[v]=-;
for(int k=;k<top;k++)
if(lowcost[k]!=-&&g[v][k]<lowcost[k])
lowcost[k]=g[v][k];
}
}
printf("%d\n",sum);
}
int main(){
int cas;string line;
scanf("%d",&cas);
char sss[];
while(cas--){
top=;
scanf("%d%d",&yn,&xn);
gets(sss);//这里如果用gethchar()oj会判wa
for(int i=;i<=xn;i++){
getline(cin,line);//读取一整行 for(int j=;j<=yn;j++){
map[i][j]=line[j-];
if(map[i][j]!=' '&&map[i][j]!='#'){//记录每个A和S
node[top].x=i;
node[top++].y=j;
}
if(map[i][j]=='S'){//记录起点
pos=top-;
}
}
}
for(int i=;i<top;i++){
bfs(node[i]);//一遍bfs找出了所有其他点与改点的距离
for(int j=i+;j<top;j++){//建边
g[i][j]=g[j][i]=dist[node[j].x][node[j].y];
}
}
for(int i=;i<top;i++)//初始化
for(int j=;j<top;j++){
if(i==j)g[i][j]=;
else if(g[i][j]==)g[i][j]=INF;
}
prim(pos);
}
}
Borg Maze的更多相关文章
- Borg Maze(MST & bfs)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9220 Accepted: 3087 Descrip ...
- 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 最小生成树 + 广搜
点击打开链接 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 ...
- 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)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- POJ 3026 Borg Maze
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7998 Accepted: 2675 Descrip ...
- POJ3026——Borg Maze(BFS+最小生成树)
Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...
- POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14165 Accepted: 4619 Descri ...
- Borg Maze(BFS+MST)
Borg Maze http://poj.org/problem?id=3026 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
随机推荐
- [置顶] Linux 流量控制
在如今的网络界,也许TC知道的人并不多了,这篇文章做留恋吧. 以前研究TC时记录下的讲解与配置文件. eth1:192.168.1.1,内网口 业务需求:保证正常的网页浏览,FTP,SMTP,POP ...
- 使用fastjson前台报406的问题解决方法
返回的json数据前台页面报406,而后台没有报错,下面为解决方法 <?xml version="1.0" encoding="UTF-8"?> & ...
- SQL Server2005 表分区三步曲(zz)
前言 SQL Server 2005开始支持表分区,这种技术允许所有的表分区都保存在同一台服务器上.每一个表分区都和在某个文件 组(filegroup)中的单个文件关联.同样的一个文件/文件组可以容纳 ...
- ASP.NET Webform或者ASP.NET MVC站点部署到IIS下,默认情况下.json文件是不能被访问的,如果请求访问.json文件,则会出现找不到文件的404错误提示
解决方法 <system.webServer> <staticContent> <remove fileExtension=".woff" /> ...
- C# 各种集合
大多数集合都在 System.Collections,System.Collections.Generic两个命名空间. 其中System.Collections.Generic专门用于泛型集合. ...
- reactjs 入门
地址搜集:http://www.cocoachina.com/webapp/20150604/12035.html class 属性需要写成 className ,for 属性需要写成 htmlFor ...
- 非常不错的ASP操作数据库类,支持多数据库MSSQL,ACCESS,ORACLE,MYSQL等
可同时操作多个不同类型的数据库. 完全不用考虑数据类型的差别,再也不用想字符型字段加不加单引号. 调用非常简单,对数据库的主要操作一般只需要一行代码. 支持mssql事务回滚. 可自动生成和输出sql ...
- WCF存储图片到指定文件夹下
string path = System.IO.Directory.GetCurrentDirectory() + @"\POIImages\"; Guid imgid = Gui ...
- Core Data 教学
看了一篇国外的文章,关于iOS9的Core Data教学,在这里做了一下总结 Core Data 教学 示例开源地址:LastDayCoreData 在这篇文章中我们将学习Core Data的系列教程 ...
- 330. Patching Array--Avota
问题描述: Given a sorted positive integer array nums and an integer n, add/patch elements to the array s ...