标签:algorithm bfs 图论 最小生成树 prim
http://poj.org/problem?id=3026
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1248
Borg Maze
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 the group consciousness of the Borg civilization. Each Borg individual is linked to the collective by a sophisticated
subspace network that insures each member is given constant supervision and guidance.
Your task is to help the Borg (yes, really) by developing a program which helps the Borg to estimate the minimal cost of scanning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is that the beginning of the search is conducted by a large group of over 100 individuals. Whenever an alien is assimilated, or at the beginning of the search, the group may split in two or more groups (but their consciousness is still collective.). The cost of searching a maze is definied as the total distance covered by all the groups involved in the search together. That is, if the original group walks five steps, then splits into two groups each walking three steps, the total distance is 11=5+3+3. Input
On the first line of input there is one integer, N <= 50, giving the number of test cases in the input. Each test case starts with a line containg two integers x, y such that 1 <= x,y <= 50. After this, y lines follow, each which x characters. For each character,
a space `` ‘‘ stands for an open space, a hash mark ``#‘‘ stands for an obstructing wall, the capital letter ``A‘‘ stand for an alien, and the capital letter ``S‘‘ stands for the start of the search. The perimeter of the maze is always closed, i.e., there
is no way to get out from the coordinate of the ``S‘‘. At most 100 aliens are present in the maze, and everyone is reachable.
Output
For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.
Sample Input 2 6 5 ##### #A#A## # # A# #S ## ##### 7 7 ##### #AAA### # A# # S ### # # #AAA### ##### Sample Output 8 11 Source |
题意:
给出一个迷宫,‘#’是墙壁,‘ ’(空格)可走,‘S’是起点,‘A’是目标,一个群体从S点开始,每次可以走周围相邻的4个格子,走到某个目标的花费是从上一个目标(或起点)开始计算的步数,群体可且仅可在S或A出分成若干个(可以看成是无数个,即使在同一个格子中)群体。比如从S开始走5步到A1,在A1分成两个群体,其中一个到达A2走3步,另一个到达A3也走3步,那么总花费是5+3+3=11。求到达所有A的最小花费。
分析:
题意很难理解,其实就是个最小生成树,用BFS在平面内模拟prim算法即可,这里要用到优先队列,因为到达某个A后就相当于步数清0,每次选走过步数最少的扩展状态,注意格子是可以重复走的,但只有步数比之前到达这个格子的步数少才走。
POJ数据好恶心,每行后面还有一堆空格,UVA就没这种情况。
/* * * Author : fcbruce * * Date : 2014-08-11 15:26:39 * */ #include <cstdio> #include <iostream> #include <cstdlib> #include <algorithm> #include <ctime> #include <cctype> #include <cmath> #include <string> #include <cstring> #include <stack> #include <queue> #include <list> #include <vector> #include <map> #include <set> #define sqr(x) ((x)*(x)) #define LL long long #define itn int #define INF 0x3f3f3f3f #define PI 3.1415926535897932384626 #define eps 1e-10 #define maxm #define maxn using namespace std; struct node { int x,y,w; bool operator < (const node &n)const { return w>n.w; } }; int n,m,total; char G[101][101]; char buf[101]; int vis[101][101]; int sx,sy; int dx[]={0,0,1,-1}; int dy[]={1,-1,0,0}; int bfs() { int res=0; priority_queue<node > q; q.push( (node){sx,sy,0}); memset(vis,0x3f,sizeof vis); vis[sx][sy]=0; while (!q.empty() || total) { node s=q.top();q.pop(); if (G[s.x][s.y]=='A') { res+=s.w; s.w=0; G[s.x][s.y]=' '; total--; } for (int i=0;i<4;i++) { int tx=s.x+dx[i]; int ty=s.y+dy[i]; int tw=s.w+1; if (G[tx][ty]!='#' && vis[tx][ty]>tw) { q.push( (node){tx,ty,tw}); vis[tx][ty]=tw; } } } return res; } int main() { #ifndef ONLINE_JUDGE freopen("/home/fcbruce/文档/code/t","r",stdin); #endif // ONLINE_JUDGE int T_T; scanf( "%d",&T_T); while (T_T--) { scanf( "%d %d",&m,&n);gets(buf); for (int i=0;i<n;i++) gets(G[i]); total=0; for (int i=0;i<n;i++) { for (int j=0;j<m;j++) { if (G[i][j]=='S') { sx=i;sy=j; } if (G[i][j]=='A') total++; } } printf( "%d\n",bfs()); } return 0; }
POJ 3026 Borg Maze & UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树),布布扣,bubuko.com
POJ 3026 Borg Maze & UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树)
标签:algorithm bfs 图论 最小生成树 prim
原文地址:http://blog.csdn.net/u012965890/article/details/38493999