Borg Maze(bfs+prim)
发布日期:2021-10-16 05:05:08 浏览次数:5 分类:技术文章

本文共 3992 字,大约阅读时间需要 13 分钟。

Borg Maze

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 73   Accepted Submission(s) : 24
Problem 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
26 5##### #A#A### # A##S  ####### 7 7#####  #AAA####
A## S ####
 ##AAA########
 

Sample Output
811
 

Source
PKU
 

自己真的没有思路,看的题解。。。借鉴的大牛的。。。

题意:

一个迷宫,给了几个特殊的点,多个A和S,让你求从S出发走遍所有A的最短路径,且在起点处或A点时,可分成多条路。

思路:先记录下所有'A'和'S'点的坐标,求出他们两两之间的距离,然后转化为最小生成树问题,用prime解决

最坑的是数据的输入。

代码:

#include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include 
        
         #include 
         
          #define inf 0x3f3f3f3fusing namespace std;struct point{int x,y,step,number;}p[10100],a,b;  //p是用来存放A的坐标以及编号,step是bfs时来记录路径 int sum;char mp[60][60]; int n,m,k;int dir[4][2]={   {1,0},{-1,0},{0,1},{0,-1}}; int pri[10100][10100];  //转化为邻接矩阵 bool v[55][55];   //记录在迷宫中是否访问过 int v1[10100],dis[10100];//prim算法中记录最小边权 bool ok(int xxx,int yyy) //判断是否符合继续广搜的条件 {
          
if(xxx<0||xxx>=m||yyy<0||yyy>=n||mp[xxx][yyy]=='#'||v[xxx][yyy])return 0;
return 1;}void bfs(int aa,int bb,int num)  //广搜,分别是坐标和编号 {
int i;
memset(v,0,sizeof(v));   //初始化所有点都未访问过
 queue q;
a.x=aa;
a.y=bb;
a.step=0;
v[aa][bb]=1;
q.push(a);
while(!q.empty())
{
a=q.front();
q.pop();
for(i=1;i
{
if(p[i].x==a.x&&p[i].y==a.y){
 //走到了A点,记录下路径
 pri[num][p[i].number]=pri[p[i].number][num]=a.step;
}
}
for(i=0;i<4;i++)  //沿四个方向广搜
 {
b.x=a.x+dir[i][0];
b.y=a.y+dir[i][1];
if(ok(b.x,b.y))
{
b.step=a.step+1;
v[b.x][b.y]=1;
q.push(b);
}
}
}}void prim(){
int i,j,tem;
int mm;
memset(v1,0,sizeof(v1));
for(i=1;i
dis[i]=pri[1][i];//从第一个A点生成树,并不非得在S 
v1[1]=1;
sum=0;
for(i=1;i
{
mm=inf;
for(j=1;j
{
if(v1[j]==0&&dis[j]
{
mm=dis[j];
tem=j;
}
}
if(mm==inf)
break;
v1[tem]=1;
sum+=mm;
for(j=1;j
{
if(v1[j]==0)
dis[j]=min(dis[j],pri[tem][j]);
}
}
cout< <
int t,i,j;
char ch[205];
cin>>t;
while(t--)
{
cin>>n>>m;
gets(ch);   //坑人的地方,nm之后可能有空格
 k=1;
for(i=0;i
{
gets(mp[i]);
}
for(i=0;i
for(j=0;j
{
if(mp[i][j]=='A'||mp[i][j]=='S')
{
p[k].x=i;
p[k].y=j;
p[k].number=k;
k++;
}
}
for(i=1;i  {
int xx=p[i].x;
int yy=p[i].y;
bfs(xx,yy,i);
}
prim();}return 0;}
心得:

能力还是不行,多努力!!!

转载地址:https://blog.csdn.net/sinat_37668729/article/details/77018264 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Pie (二分)
下一篇:8月9日训练笔记

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.172.111.71]2022年05月22日 08时37分22秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

最新文章