POJ 3026 bfs+最小生成树
来源:互联网 发布:数据运营是干什么的 编辑:程序博客网 时间:2024/06/03 05:31
Borg Maze
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8445 Accepted: 2833
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
26 5##### #A#A### # A##S ####### 7 7##### #AAA#### A## S #### ##AAA########
Sample Output
811
Source
题目就是求一个图中 s,A的 最小生成树,不过有点变态,将出现的s和A按出现的顺序依次递增赋值,用bfs求出这个点和别的S或A的值,然后最小生成树
具体看代码,坑点在代码中解释
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;#define maxe 100000#define N 1005 //一定要开大,刚开始105一直wa%>_<%int a[N][N];int father[N];struct stud{int a,b;int len;}f[maxe];struct stud1{int x,y;int time;};int step[4][2]={1,0,-1,0,0,1,0,-1};int n,m;int vis[N][N];int k;int judge(int x,int y){ if(x>=0&&x<n&&y>=0&&y<m) return 1; return 0;}int cha(int x){ if(x!=father[x]) father[x]=cha(father[x]); return father[x];}int cmp(stud a,stud b){ return a.len<b.len;}void bfs(int x,int y){ int i,j; queue<stud1>q; while(!q.empty()) q.pop(); struct stud1 cur,next; cur.x=x; cur.y=y; cur.time=0; q.push(cur); memset(vis,0,sizeof(vis)); vis[x][y]=1; while(!q.empty()) { cur=q.front(); q.pop(); for(i=0;i<4;i++) { int xx=cur.x+step[i][0]; int yy=cur.y+step[i][1]; if(!judge(xx,yy)||a[xx][yy]<0||vis[xx][yy]) continue; next.x=xx; next.y=yy; next.time=cur.time+1; vis[xx][yy]=1; if(a[xx][yy]>=1) { f[k].a=a[x][y]; f[k].b=a[xx][yy]; f[k++].len=next.time; } q.push(next); } }}int main(){ int t,i,j; scanf("%d",&t); while(t--) { scanf("%d%d",&m,&n); char s[1000]; gets(s); //小心是一组空格,必须用gets int num=0; k=0; char c; for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%c",&c); if(c=='#') a[i][j]=-1; else if(c==' ') a[i][j]=0; else a[i][j]=++num; //出现的S或A依次递增赋值 } getchar(); } for(i=0;i<n;i++) for(j=0;j<m;j++) if(a[i][j]>0) bfs(i,j); for(i=0;i<=num;i++) father[i]=i; sort(f,f+k,cmp); j=1; int ans=0; for(i=0;i<k;i++) { int aa=cha(f[i].a); int bb=cha(f[i].b); if(aa!=bb) { j++; ans+=f[i].len; father[aa]=bb; if(j==num) break; } } printf("%d\n",ans); } return 0;}
代码不怎么好读,sorry,
1 0
- poj 3026(BFS+最小生成树)
- POJ 3026 bfs+最小生成树
- poj-3026—bfs+最小生成树
- POJ 3026 (BFS+最小生成树)
- POJ 3026(BFS+最小生成树)
- poj 3026 Borg Maze 最小生成树+bfs
- POJ 3026 Borg Maze bfs + 最小生成树
- poj 3026 Borg Maze bfs建图+最小生成树
- poj 3026 Borg Maze (bfs+最小生成树)
- POJ 3026 Borg Maze【BFS+最小生成树】
- POJ 3026 Borg Maze (最小生成树 + BFS)
- POJ 3026 Borg Maze(BFS+最小生成树)
- POJ训练计划3026_Borg Maze(最小生成树+BFS)
- poj 3026 Borg Maze (bfs + 最小生成树)
- POJ 3026 Borg Maze (bfs+最小生成树)
- poj 3026 Borg Maze bfs+最小生成树
- POJ 3026 Brog Maze BFS+最小生成树
- POJ 3026 —— Borg Maze BFS + 最小生成树
- ASP.NET MVC 單元測試系列 (4):單元測試的目的與價值
- algorithm 简单用法
- 程序员必须知道的10大基础实用算法
- 字符串
- ASP.NET MVC 單元測試系列 (5):瞭解 Stub 假物件
- POJ 3026 bfs+最小生成树
- C++中WORD到底是个什么类型
- Google带给我们的C++福利之一google code sytle
- ASP.NET MVC 單元測試系列 (6):測試 Routing 路由規則
- 最少拦截系统(贪心)
- WCF跨域策略实现(原)
- ASP.NET MVC 單元測試系列 (7):Visual Studio Unit Test
- hdoj.2092 整数解 20140805
- POJ 2250 Compromise (DP,最长公共子序列)