POJ 3026 Borg Maze(BFS+prim)
来源:互联网 发布:菜鸟裹裹抢单软件 编辑:程序博客网 时间:2024/06/05 08:47
传送门:http://poj.org/problem?id=3026
这题题意较为复杂,就是从S出发,要走到所有的A点,在每个点都可以分裂出多个,也就是说,我们可以把S点也看成是一个A点,就是从一个A点出发,在所有A点相连的路径中选择一些路径,使得走过的路最小,简化一下题意,其实就是一个最小生成树,但是需要预先处理好每个点之间的距离,但是因为所有距离都求出来了,是稠密图,所以用prim会比较好吧(不知道kruskal会不会更快)。
这题的输入也是有点坑,在图中会有空格出现,不能用%s,所以我就用了fgets然后从字符串中读取内容。 为了给每个A编号,还用了一个map来记录编号,距离和编号处理完之后就是一个简单的prim了,因为是最小生成树,所以也不一定要从S点开始,随便挑一个点,prim结束就完成了。
这题的思路还是非常重要的,题目长的也不像是最小生成树,非常好的题目!
代码:
#include<stdio.h>#include<string.h>#include<ctype.h>#include<time.h>#include<vector>#include<string>#include<queue>#include<map>#include<iostream>#include<cmath>#include<algorithm>using namespace std;const int INF = 999999999;int n, m;char input[300];char pic[60][60];int dis[110][110];int go[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };int d[110];struct pos {int x, y;bool operator < (pos a)const {if (x != a.x)return x < a.x;return y < a.y;}pos(int a, int b) :x(a), y(b) {}};struct node{int x,y,step;};int cnt;map<pos, int> id;vector<pos> point;bool judge(int x,int y){return x >= 0 && x < n&&y >= 0 && y < m;}int ID(pos x){if (id.count(x))return id[x];cnt++;return id[x] = cnt;}void init(){memset(dis, 0, sizeof(dis));cnt = 0;id.clear();point.clear();}void BFS(int _x, int _y, int st){bool vis[55][55] = {false};queue<node> Q;node start, use;start.step = 0;start.x = _x;start.y = _y;vis[_x][_y] = true;Q.push(start);while (!Q.empty()){start = Q.front();Q.pop();use.step = start.step + 1;for (int i = 0; i < 4; i++){use.x = start.x + go[i][0];use.y = start.y + go[i][1];if (judge(use.x, use.y) && pic[use.x][use.y] != '#'&&vis[use.x][use.y]==false){vis[use.x][use.y] = true;if (pic[use.x][use.y] == 'A' || pic[use.x][use.y] == 'S')//建图的过程{dis[st][ID(pos(use.x, use.y))] = use.step;}Q.push(use);}}}}void prim(){int ans = 0;int i, j;cnt++;//一共有【1-cnt】个点,就+1变成【1,cnt)开区间for (i = 2; i < cnt; i++){d[i] = dis[1][i];}d[1] = -1;for (i = 2; i < cnt; i++)//因为cnt+1了,所以一共需要cnt-2条边{int minn = INF;int v = -1;for (j = 2; j < cnt; j++){if (d[j] != -1 && d[j] < minn){minn = d[j];v = j;}}ans += minn;d[v] = -1;for (j = 2; j < cnt; j++){if (dis[v][j] < d[j])d[j] = dis[v][j];}}printf("%d\n", ans);}int main(){//freopen("D://input.txt", "r", stdin);//freopen("D://output.txt", "w", stdout);int T;scanf("%d", &T);getchar();while (T--){init();int i, j, k;fgets(input, 300, stdin);sscanf(input, "%d%d", &m, &n);for (i = 0; i < n; i++){fgets(input, 300, stdin);for (j = 0; j < m; j++) {pic[i][j] = input[j];if (input[j] == 'A' || input[j] == 'S')//读入的时候记录每个S和A点,全部放到vector里{point.push_back(pos(i, j));}}}for (k = 0; k < point.size(); k++)//对每个S或A都进行一遍BFS来建图{BFS(point[k].x, point[k].y, ID(point[k]));}prim();}//printf("%.6f\n", (double)clock() / CLOCKS_PER_SEC);return 0;}
0 0
- POJ 3026 Borg Maze BFS+Prim
- POJ 3026--Borg Maze (BFS+Prim)
- poj 3026 Borg Maze (BFS + Prim)
- poj 3026 Borg Maze (bfs+prim)
- 【BFS+Prim】poj 3026 Borg Maze
- POJ 3026:Borg Maze:BFS+prim
- POJ 3026 Borg Maze (BFS+Prim)
- POJ 3026 Borg Maze(BFS+Prim)
- poj 3026 Borg Maze bfs+prim
- POJ 3026 Borg Maze BFS和prim
- POJ 3026 Borg Maze(BFS+prim)
- poj-3026 -Borg Maze -bfs+prim(MST)
- POJ 3026 Borg Maze (prim+bfs)
- poj 3026 Borg Maze(BFS+Prim)
- POJ 3026 Borg Maze (bfs+prim)
- POJ 3026 Borg Maze (bfs+prim)
- poj 3026 Borg Maze(bfs+prim)
- poj 3026 Borg Maze 【prim+bfs】
- 简易计算器(文本框灰色文字提示)
- linux/unix下的静态库和共享库
- 国家版权局:网盘禁止用户上传分享侵权作品
- 【Android成长之路】Menu的简单应用
- 华三一面面试经验分享
- POJ 3026 Borg Maze(BFS+prim)
- Xcode一些使用技巧
- Bootstrap插件collapse源码的学习
- cf 2a Winner
- 【Space Shoot Project】Game Controller
- 如何制作Ext4文件系统镜像
- Java finally语句是在return之前还是之后执行?
- RecycleView显示不同类型的Item
- Python进阶教程