您的位置:首页 > 其它

POJ训练计划3026_Borg Maze(最小生成树+BFS)

2014-04-29 16:06 375 查看
Borg Maze

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7724 Accepted: 2594
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

解题报告

POJ训练计划之最小生成树

这题先要BFS求个点距离。。。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#define inf 99999999
using namespace std;
struct no
{
int x,y,s;
} Q[100000];
int n,m,a,b;
int mapp[210][210];
int mmap[55][55];
int vis[210][210];
int dx[4]= {0,-1,0,1};
int dy[4]= {1,0,-1,0};
void bfs(int s,int e)
{
//queue<no>Q;
no next,tail;
tail.x=s;
tail.y=e;
tail.s=0;
int t,f;
t=f=0;
Q[t++]=tail;
//Q.push(tail);
vis[s][e]=1;
while(t>f)
{
//tail=Q.front();
//Q.pop();
tail=Q[f++];
if(mmap[tail.x][tail.y]>0)
{
//printf("%d %d %d\n",mmap[s][e],mmap[tail.x][tail.y],tail.s);
mapp[mmap[s][e]][mmap[tail.x][tail.y]]=tail.s;
}
for(int i=0; i<4; i++)
{
next.x=tail.x+dx[i];
next.y=tail.y+dy[i];
if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&!vis[next.x][next.y]&&mmap[next.x][next.y]>=0)
{
next.s=tail.s+1;
vis[next.x][next.y]=1;
//Q.push(next);
Q[t++]=next;
}
}
}
}
void prim()
{
int i,j,u,minn,ans=0;
int dis[110];
int v[110];
for(i=1; i<=a; i++)
{
dis[i]=mapp[1][i];
v[i]=0;
}
v[1]=1;
dis[1]=0;
for(i=0; i<a-1; i++)
{
u=0;
minn=inf;
for(j=1; j<=a; j++)
{
if(!v[j]&&dis[j]<minn)
{
minn=dis[j];
u=j;
}
}
ans+=minn;
v[u]=1;
for(j=1; j<=a; j++)
{
if(!v[j]&&dis[j]>mapp[u][j])
{
dis[j]=mapp[u][j];
}
}
}
printf("%d\n",ans);

}
int main()
{
int t,i,j;
scanf("%d",&t);
char str[55];
while(t--)
{
int nx[210],ny[210];
memset(nx,0,sizeof(nx));
memset(ny,0,sizeof(ny));
memset(mmap,0,sizeof(mmap));
memset(mapp,0,sizeof(mapp));
scanf("%d%d",&m,&n);
char c=getchar();
while(c!='\n')
c=getchar();
a=0;
for(i=0; i<n; i++)
{
gets(str);
for(j=0; str[j]!='\0'; j++)
{
if(str[j]=='A'||str[j]=='S')
{
mmap[i][j]=++a;
}
else if(str[j]==' ')mmap[i][j]=0;
else mmap[i][j]=-1;
}
}
//printf("%d\n",a);
//        for(i=0; i<n; i++)
//        {
//            for(j=0; j<m; j++)
//                printf("%d ",mmap[i][j]);
//            printf("\n");
//        }
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
if(mmap[i][j]>0)
{
memset(vis,0,sizeof(vis));
bfs(i,j);}
}
//        for(i=1; i<=a; i++)
//        {
//            for(j=1; j<=a; j++)
//                printf("%d ",mapp[i][j]);
//            printf("\n");
//        }
prim();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: