POJ-3026 Borg Maze---BFS预处理+最小生成树

题目链接:

https://vjudge.net/problem/POJ-3026

题目大意:

在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度。

思路:

先BFS预处理出所有的字母之间的距离,然后用prim模板

超级坑的是这里得用gets()吃掉回车符,用getchar()会WA

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<queue>
  7 #include<stack>
  8 #include<map>
  9 #include<sstream>
 10 using namespace std;
 11 typedef long long ll;
 12 typedef pair<int, int> Pair;
 13 const int maxn = 1e2 + 10;
 14 const int INF = 0x3f3f3f3f;
 15 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
 16 int T, n, m, x;
 17 int Map[maxn][maxn];//存图
 18 int lowcost[maxn], mst[maxn];
 19 void prim(int u, int n)//最小生成树起点
 20 {
 21     int sum_mst = 0;//最小生成树权值
 22     for(int i = 0; i < n; i++)//初始化两个数组
 23     {
 24         lowcost[i] = Map[u][i];
 25         mst[i] = u;
 26     }
 27     mst[u] = -1;//设置成-1表示已经加入mst
 28     for(int i = 0; i < n; i++)
 29     {
 30         int minn = INF;
 31         int v = -1;
 32         //在lowcost数组中寻找未加入mst的最小值
 33         for(int j = 0; j < n; j++)
 34         {
 35             if(mst[j] != -1 && lowcost[j] < minn)
 36             {
 37                 v = j;
 38                 minn = lowcost[j];
 39             }
 40         }
 41         if(v != -1)//v=-1表示未找到最小的边,
 42         {//v表示当前距离mst最短的点
 43             //printf("%d %d %d\n", mst[v], v, lowcost[v]);//输出路径
 44             mst[v] = -1;
 45             sum_mst += lowcost[v];
 46             for(int j = 0; j < n; j++)//更新最短边
 47             {
 48                 if(mst[j] != -1 && lowcost[j] > Map[v][j])
 49                 {
 50                     lowcost[j] = Map[v][j];
 51                     mst[j] = v;
 52                 }
 53             }
 54         }
 55     }
 56     //printf("weight of mst is %d\n", sum_mst);
 57     cout<<sum_mst<<endl;
 58 }
 59 string s[55];
 60 vector<Pair>a;
 61 int vis[55][55];
 62 void bfs(int u, int x, int y)//BFS预处理,将x,y为起点进行遍历,求出每个点离当前点距离,更新出Map距离出来
 63 {
 64     queue<Pair>q;
 65     q.push(Pair(x, y));
 66
 67     memset(vis, -1, sizeof(vis));
 68     vis[x][y] = 0;
 69     while(!q.empty())
 70     {
 71         Pair now = q.front();
 72         q.pop();
 73         for(int i = 0; i < 4; i++)
 74         {
 75             int xx = now.first + dir[i][0];
 76             int yy = now.second + dir[i][1];
 77             if(xx >= 0 && xx < n && yy >= 0 && yy < m && s[xx][yy] != ‘#‘ && vis[xx][yy] == -1)
 78             {
 79                 vis[xx][yy] = vis[now.first][now.second] + 1;
 80                 q.push(Pair(xx, yy));
 81             }
 82         }
 83     }/*
 84     for(int i = 0; i < n; i++)
 85     {
 86         for(int j = 0; j < m; j++)cout<<vis[i][j]<<" ";
 87         cout<<endl;
 88     }*/
 89     for(int i = 0; i < a.size(); i++)
 90     {
 91         if(i == u)continue;
 92         Map[u][i] = vis[a[i].first][a[i].second];
 93     }
 94 }
 95 int main()
 96 {
 97     cin >> T;
 98     getchar();
 99     while(T--)
100     {
101         for(int i = 0; i < 55; i++)s[i].clear();
102         char cc[5];
103         cin >> m >> n;
104         gets(cc);
105         a.clear();
106         for(int i = 0; i < n; i++)
107         {
108             getline(cin, s[i]);
109             for(int j = 0; j < m; j++)
110                 if(s[i][j] == ‘S‘ || s[i][j] == ‘A‘)
111                 a.push_back(Pair(i, j));
112         }
113         for(int i = 0; i < a.size(); i++)
114         {
115             for(int j = 0; j < a.size(); j++)
116             {
117                 if(i == j)Map[i][j] = 0;
118                 else Map[i][j] = INF;
119             }
120         }
121         for(int i = 0; i < a.size(); i++)
122             bfs(i, a[i].first, a[i].second);
123         /*for(int i = 0; i < a.size(); i++)
124         {
125             for(int j = 0; j < a.size(); j++)
126             {
127                 cout<<Map[i][j]<<" ";
128             }
129             cout<<endl;
130         }*/
131         prim(0, a.size());
132     }
133     return 0;
134 }

原文地址:https://www.cnblogs.com/fzl194/p/8724074.html

时间: 2024-10-10 02:37:29

POJ-3026 Borg Maze---BFS预处理+最小生成树的相关文章

POJ - 3026 Borg Maze BFS加最小生成树

Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算读懂了题目,也比较难想到这用到了最小生成树的知识,因为可以分身,所以每个点可以向其他点都连上边.可以用bfs预处理出所有的S点,A点的连线,再跑一遍最小生成树,即可得出答案.这里有几点注意,一开始我bfs没有记录step,而是直接找到一点后算曼哈顿距离,这是不对的,因为可能是绕了一个圈到了这个点.还

poj 3026 Borg Maze (bfs + 最小生成树)

链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走),空格代表空位(可走),S代表搜索起点(可走) A代表外星人站(可走),现在要从S出发,将S和所有的A之间都连通,求路线总距离最小值 分析:可以先用bfs将所有的A,S两两之间的最短距离,题目的目的是将S与所有的A连通,使得总距离最小, 所有任选一点开始按最小生成树的算法做就行,并非非要从S点开始 注:题目输入x,y后可能有很多空格,可以用gets将多余的空格取走,开数组是尽量开大点,之前虽然开的比题目数据     稍大,但一

poj 3026 Borg Maze(bfs+prim)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10810   Accepted: 3574 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to desc

POJ 3026 Borg Maze(bfs + prime)

解题思路: 先用BFS预处理出每个字母节点到其它节点的最短路径,然后套用prime算法. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <queue> #include <stack> #include <vector> #include <

POJ 3026 Borg Maze &amp; UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树)

http://poj.org/problem?id=3026 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1248 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8498   Accepted: 2862 Description The Bor

POJ 3026 Borg Maze

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7998   Accepted: 2675 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to descr

POJ 3026 Borg Maze【BFS+最小生成树MST】

Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12014 Accepted: 3925 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe

POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16625   Accepted: 5383 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to desc

poj 3026 Borg Maze 最小生成树+bfs prim算法

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to descr

POJ 3026 Borg Maze(BFS+最小生成树【有坑】)

Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12012 Accepted: 3924 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe