POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)
Posted luowentao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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.
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和A全部连在一起的最少消耗,明着最小生成树,但是每一个S和A之间的距离用bfs求出就行。
主要是题目有个坑,在n,和m后可能会有空行;
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <map> using namespace std; const int INF=0x3f3f3f3f; char mapp[155][155]; int a[155][155]; int n,m; int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; struct node{ int x,y; }; int flag[155][155]; bool vis[155]; int minn[155]; int cost[155][155];///记录总的各点到各点的距离 int cnt[155][155];///记录当前点到bfs到其他各点的距离 void bfs(int sx,int sy){ queue<node>q; while(!q.empty())q.pop(); memset(cnt,-1,sizeof(cnt)); cnt[sx][sy]=0; node first; first.x=sx,first.y=sy; q.push(first); while(!q.empty()){ node now=q.front(); q.pop(); if(a[now.x][now.y]!=-1)///记录到全局为生成树做准备 cost[a[sx][sy]][a[now.x][now.y]]=cnt[now.x][now.y]; for(int i=0;i<4;i++){ node next; next.x=now.x+dx[i]; next.y=now.y+dy[i]; if(mapp[next.x][next.y]==‘#‘||cnt[next.x][next.y]!=-1||next.x<0||next.y<0||next.x>=n||next.y>=m)continue; cnt[next.x][next.y]=cnt[now.x][now.y]+1; q.push(next); } } } int prim(int n){ int ans=0; memset(vis,false,sizeof(vis)); vis[0]=true; for(int i=1;i<n;i++)minn[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>minn[j]){ minc=minn[j]; p=j; } } ans+=minc; vis[p]=true; for(int j=0;j<n;j++) if(!vis[j]&&minn[j]>cost[p][j]){ minn[j]=cost[p][j]; } }return ans; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t; cin>>t; while(t--){ cin>>m>>n; gets(mapp[0]); memset(a,-1,sizeof(a)); int num=0; for(int i=0;i<n;i++){ gets(mapp[i]); for(int j=0;j<m;j++){ if(mapp[i][j]==‘A‘||mapp[i][j]==‘S‘) a[i][j]=num++; } } /* for(int i=0;i<n;i++){ cout<<mapp[i]<<endl; }*/ for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(a[i][j]!=-1){ bfs(i,j); } } } /* for(int i=0;i<num;i++){ for(int j=0;j<num;j++){ cout<<cost[i][j]<<" "; } cout<<endl; }*/ cout<<prim(num)<<endl; } return 0; }
以上是关于POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)的主要内容,如果未能解决你的问题,请参考以下文章
练习赛补题poj 3026 Borg Maze bfs+最小生成树坑~
POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)
POJ-3026 Borg Maze---BFS预处理+最小生成树
POJ 3026 Borg Maze(Prim+bfs求各点间距离)