poj 3026 Borg Maze (BFS + Prim)
来源:互联网 发布:lamp兄弟连java 编辑:程序博客网 时间:2024/06/05 08:40
#include<stdio.h>#include<queue>#include<string.h>using namespace std;char map[2000][2000];int map1[2000][2000];bool vis[2000][2000];int gra[2000][2000];int m , n;int dir[4][2] = { {1 , 0} , {-1 , 0} , {0 , 1} , {0 , -1}};struct node { int x; int y; int step; int c;};queue<node> q;void bfs(int x , int y , int sc){ while (!q.empty()) q.pop(); memset(vis , false , sizeof(vis)); node t , temp; t.x = x; t.y = y; t.c = sc; t.step = 0; q.push(t); vis[x][y] = true; while(!q.empty()){ t = q.front(); if(t.c >= 1){ gra[sc][t.c] = t.step; } q.pop(); for(int i = 0 ; i < 4 ; i ++){ temp.x = t.x + dir[i][0]; temp.y = t.y + dir[i][1]; temp.step = t.step + 1; if(temp.x >= 0 && temp.x < n && temp.y >= 0 && temp.y < m && vis[temp.x][temp.y] == false && map1[temp.x][temp.y] != -1){ vis[temp.x][temp.y] = true; temp.c = map1[temp.x][temp.y]; q.push(temp); } } } return ;}int dist[1040];bool flag[1040];int prim(int sc , int tot){ int ans = 0; int temp; memset(flag , false , sizeof(flag)); flag[sc] = true; for(int i = 1 ; i <= tot ; i ++){ dist[i] = gra[sc][i]; } int k = tot - 1; int min; int minid; while(k --){ min = 0x3f3f3f3f; for(int i = 1 ; i <= tot ; i ++){ if(dist[i] < min && flag[i] == false){ min = dist[i]; minid = i; } } flag[minid] = true; ans += min; //printf("ans = %d\n" , ans); for(int i = 1 ; i <= tot ; i ++){ if(dist[i] > gra[minid][i] && flag[i] == false){ dist[i] = gra[minid][i]; } } } return ans;}int main(){ int T; int tot; while(~scanf("%d" , &T)){ int x , y; char tt[100]; while(T --){ tot = 2; scanf("%d%d" , &m , &n); gets(tt); for(int i = 0 ; i < n ; i ++){ gets(map[i]); } for(int i = 0 ; i <n ; i ++){ for(int j = 0 ; j < m ; j ++){ map1[i][j] = 0; } } for(int i = 0 ; i < n ; i ++){ for(int j = 0 ; j < m ; j ++){ if(map[i][j] == 'S'){ map1[i][j] = 1; } if(map[i][j] == 'A'){ map1[i][j] = tot ++; } if(map[i][j] == '#'){ map1[i][j] = -1; } } } tot --; for(int i = 0 ; i < n ; i ++){ for(int j = 0 ; j < m ; j ++){ if(map1[i][j] >= 1){ bfs(i , j , map1[i][j]); } } } printf("%d\n" , prim(1 , tot)); } } return 0;}
思路:
题意大致是一堆人从S出发,目的地是A,到一次A,S就可以分裂成多堆,走向其他的A。我们的任务就是求一种走法,使经过的路程最短。
我先用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)
- 【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】
- 服务器性能估算
- poj3322 Bloxorz I
- NQueues
- mysql事务
- Delphi 调用SQL Server 2008存储过程
- poj 3026 Borg Maze (BFS + Prim)
- javascript状态桂文字由右端飞入
- mysql优化查询的方法
- eclipse快捷键
- poj 1125 Stockbroker Grapevine (Floyed)
- fedora framebuffer fb0 的创建 关于/dev/fb0找不到
- poj 1465 Multiple (BFS)
- 架构设计师—你在哪层楼?
- 系统性能评价中的几个指标