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 <set>
#include <map>
#define LL long long
using namespace std;
const int MAXN = 100 + 10;
char G[MAXN][MAXN];
int A[MAXN][MAXN];
int N, M;
int g[][2] = {{-1,0},{1,0},{0,-1},{0,1}};
int cost[MAXN][MAXN];
int t[MAXN][MAXN];
void bfs(int sx, int sy)
{
	memset(t, -1, sizeof(t));
	queue<pair<int,int> > Q;
	Q.push(make_pair(sx,sy));
	t[sx][sy] = 0;
	while(!Q.empty())
	{
		pair<int,int> u = Q.front();
		Q.pop();
		if(A[u.first][u.second] != -1)
		{
			cost[A[sx][sy]][A[u.first][u.second]] = t[u.first][u.second];
		}
		for(int i=0;i<4;i++)
		{
			int x = u.first + g[i][0];
			int y = u.second + g[i][1];
			if(G[x][y] == '#' || t[x][y] != -1)
				continue;
			t[x][y] = t[u.first][u.second] + 1;
			Q.push(make_pair(x,y));
		}
	}
}
const int INF = 0x3f3f3f3f;
bool vis[MAXN];
int lowc[MAXN];
int prime(int n)
{
	int ans = 0;
	memset(vis, 0, sizeof(vis));
	vis[0] = true;
	for(int i=1;i<n;i++) lowc[i] = cost[0][i];
	for(int i=1;i<n;i++)
	{
		int minc = INF;
		int p = -1;
		for(int j=0;j<n;j++)
		{
			if(!vis[j] && minc > lowc[j])
			{
				minc = lowc[j];
				p = j;
			}
		}
		if(minc == INF) return -1;
		ans += minc;
		vis[p] = true;
		for(int j=0;j<n;j++)
		{
			if(!vis[j] && lowc[j] > cost[p][j])
				lowc[j] = cost[p][j];
		}
	}
	return ans;
}
int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d%d", &M, &N);
		gets(G[0]);
		int tot = 0;
		for(int i=0;i<N;i++)
			gets(G[i]);
		/*for(int i=0;i<N;i++)
		{
			for(int j=0;j<M;j++)
				cout<<G[i][j];
			cout<<endl;
		}*/
		memset(A, -1, sizeof(A));
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<M;j++)
			{
				if(G[i][j] == 'S' || G[i][j] == 'A')
					A[i][j] = tot++;
			}
		}
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<M;j++)
			{
				if(A[i][j] != -1)
					bfs(i, j);
			}
		}
		int ans = prime(tot);
		printf("%d\n", ans);
	}
	return 0;
}
		
时间: 2024-08-09 02:45:57

POJ 3026 Borg Maze(bfs + prime)的相关文章

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加最小生成树

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

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 &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 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+最小生成树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: 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

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