POJ 3026 - Borg Maze(最小树+BFS)

来源:互联网 发布:闪烁特效的软件 编辑:程序博客网 时间:2024/04/29 10:34

Borg Maze
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7226 Accepted: 2429

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


==========================

只能在S或A处分裂,满足最小生成树,对每个点都bfs,求出各个点之间的距离,再求最小生成树


#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int INF=0x3f3f3f3f;int dir[4][2]={{-1,0},{0,-1},{0,1},{1,0}};int dis[111][111],vis[111][111],mark[111][111];int m,n,cnt;char map[111][111];struct point{    int x,y,step,id;}a[333],p,temp;bool check(int x,int y){    if(x>=1&&x<=n&&y>=1&&y<=m) return 1;    else return 0;}void bfs(point start){    int num=0;    queue<point>que;    p.x=start.x;p.y=start.y;p.step=0;    que.push(p);    vis[p.x][p.y]=1;    while(!que.empty())    {        temp=que.front();        que.pop();        for(int i=0;i<4;i++)        {            p.x=temp.x+dir[i][0];            p.y=temp.y+dir[i][1];            p.step=temp.step+1;            if(!check(p.x,p.y)||map[p.x][p.y]=='#'||vis[p.x][p.y]) continue;            que.push(p);            vis[p.x][p.y]=1;            if((map[p.x][p.y]=='A'||map[p.x][p.y]=='S'))            {                if(dis[ mark[p.x][p.y] ][start.id]==INF)                    dis[ mark[p.x][p.y ]][start.id]=dis[start.id][ mark[p.x][p.y] ]=p.step;                num++;            }            if(num==cnt-1) return;        }    }}int prim(int s){    int res=0,vis[111],d[111];    for(int i=1; i<=cnt; i++)    {        d[i]=INF;        vis[i]=false;    }    d[s]=0;    while(1)    {        int u=0,mn=INF;        for (int i=1;i<=cnt;i++)        {            if (!vis[i]&&d[i]<mn)            {                mn=d[i];                u=i;            }        }        if(u==0) break;        vis[u]=true;        res+=mn;        //cout<<"u="<<u<<" res="<<res<<" "<<endl;        for (int i=1;i<=cnt;i++)        {            if (d[i]>dis[u][i]&&u!=i)                d[i]=dis[u][i];        }    }    return res;}int main(){    int T;char str[1111];    scanf("%d",&T);    while(T--)    {        memset(dis,INF,sizeof(dis));        memset(mark,0,sizeof(mark));        scanf("%d%d",&m,&n);        gets(str);        cnt=0;        for(int i=1;i<=n;i++)        {            gets(map[i]+1);            for(int j=1;j<=m;j++)            {                if(map[i][j]=='S'||map[i][j]=='A')                {                    a[++cnt].id=cnt;                    a[cnt].x=i;                    a[cnt].y=j;                    mark[i][j]=cnt;                }            }        }        for(int i=1;i<=cnt;i++)            dis[i][i]=0;        for(int i=1;i<=cnt;i++)        {            memset(vis,0,sizeof(vis));            bfs(a[i]);        }        /*for(int i=1;i<=cnt;i++)        {            for(int j=1;j<=cnt;j++)                cout<<dis[i][j]<<" ";            cout<<endl;        }*/        printf("%d\n",prim(1));    }    return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 顾客拿过期的食品过来投诉怎么办 老婆总是埋怨我父母我该怎么办? 代款公司如果使用暴力追债怎么办 法院拍卖款分配有疑意怎么办 法院拍卖买到的房子里有户口怎么办 新注册手机邮箱不和电脑同步怎么办 移动4g盒当月流量封顶怎么办 昆仑加油卡密码忘记了怎么办 昆仑银行e盾密码忘记了怎么办 中石化加油卡密码忘记了怎么办 壳牌加油卡密码忘了怎么办 中国石化加油卡密码忘了怎么办 中石化加油卡密码忘了怎么办 中石化加油卡需要密码忘了怎么办 求不熟领导办事送礼不收怎么办 送礼给领导不收好像很生气怎么办 加油卡没有密码加油后锁住怎么办 个人怎么办中石化油卡怎么开公司票 中石化副卡挂失后钱怎么办 中石化的加油卡丢了怎么办 得仕卡过期3年了怎么办 如果在超市买到过期商品怎么办 华润万家买的豆干过期了吃了怎么办 华润万家购物卡过期了怎么办 杜鹃花水浇多了树叶都掉了怎么办 游客行程因天气原因无法进行怎么办 携程旅游途中提前结束行程怎么办 小区附近商家卖东西很吵怎么办? 小区门口卖东西的喇叭太吵怎么办 我老婆是二婚带的小孩怎么办户口 ck手表带了脱不下来怎么办 消毒柜开孔尺寸高度太高了怎么办 苏宁易购买的东西比专卖店贵怎么办 手机打字键盘上没有语音功能怎么办 网贷催收发语音侮辱人怎么办 康力无机房电梯到楼层不关门怎么办 档案工龄少算了工资领开了怎么办 别人骚扰我打电话报警人走了怎么办 因外借医保卡被香港保险拒保怎么办 医保和社保断了一个月怎么办 苹果8呼叫受限请勿越权使用怎么办