poj3026

Borg Maze

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3842 Accepted: 1271

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

解题思路:BFS搜索地图,找出各个节点相对距离

              prim求最小生成树,然后计算edge和。

              这是一个非常蛋疼的题,在x,y之后有很多空格,导致wa了几次。

 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

#define MAX_VALUE 65535

#define debug 0

using namespace std;

typedef struct
{
    int x,y;
    int step;
}NODE;

int x,y;
int number;
int map[50][50];
NODE node[101]; //node[0] for start, node[1...100] for aliens
int graph[101][101];
int d[101];

void prim()
{
    int minValue, minNode;
    int visit[101];
    int totalDist;

    fill(d,d+number+1,MAX_VALUE);
    memset(visit,0,sizeof(visit));
    d[0]=0;
    totalDist=0;
    minNode=0;
    for(int i=0; i<=number; i++)
    {
        minValue=MAX_VALUE;
        for(int j=0; j<=number; j++)
        {
            if(!visit[j]&&d[j]<minValue)
            {
                minValue=d[j];
                minNode=j;
            }
        }
        visit[minNode]=true;
        totalDist+=d[minNode];
        for(int j=0; j<=number; j++)
        {
            if(!visit[j]&&d[j]>graph[minNode][j])
            {
                d[j]=graph[minNode][j];
            }
        }
    }
    printf("%d\n",totalDist);
}
void BFS()
{
    queue<NODE> Q;
    bool visit[50][50]; //record map visited
    NODE tmpNode1,tmpNode2;
    int find;

    for(int i=0; i<=number; i++) // for all nodes, do BFS
    {
        for(int j=0; j<=number; j++)
        {
            node[j].step=0;
        }
        memset(visit,0,sizeof(visit));
        find=0;
        tmpNode1.x=node[i].x;
        tmpNode1.y=node[i].y;
        tmpNode1.step=node[i].step;
        //BFS
        while(!Q.empty())
        {
            Q.pop();
        }
        Q.push(tmpNode1);
        visit[tmpNode1.x][tmpNode1.y]=true;
        while(!Q.empty() && find!=number)
        {
            tmpNode1=Q.front();
            Q.pop();
            if(tmpNode1.x-1>=0 && !visit[tmpNode1.x-1][tmpNode1.y] && map[tmpNode1.x-1][tmpNode1.y]!=-2) //left move
            {
                tmpNode2.x=tmpNode1.x-1;
                tmpNode2.y=tmpNode1.y;
                tmpNode2.step=tmpNode1.step+1;
                visit[tmpNode2.x][tmpNode2.y]=true;
                if(map[tmpNode2.x][tmpNode2.y]>=0)
                {
                    find++;
                    graph[i][map[tmpNode2.x][tmpNode2.y]]=tmpNode2.step;
                }
                Q.push(tmpNode2);
            }
            if(tmpNode1.x+1<x && !visit[tmpNode1.x+1][tmpNode1.y] && map[tmpNode1.x+1][tmpNode1.y]!=-2) //right move
            {
                tmpNode2.x=tmpNode1.x+1;
                tmpNode2.y=tmpNode1.y;
                tmpNode2.step=tmpNode1.step+1;
                visit[tmpNode2.x][tmpNode2.y]=true;
                if(map[tmpNode2.x][tmpNode2.y]>=0)
                {
                    find++;
                    graph[i][map[tmpNode2.x][tmpNode2.y]]=tmpNode2.step;
                }
                Q.push(tmpNode2);
            }
            if(tmpNode1.y-1>=0 && !visit[tmpNode1.x][tmpNode1.y-1] && map[tmpNode1.x][tmpNode1.y-1]!=-2) //up move
            {
                tmpNode2.x=tmpNode1.x;
                tmpNode2.y=tmpNode1.y-1;
                tmpNode2.step=tmpNode1.step+1;
                visit[tmpNode2.x][tmpNode2.y]=true;
                if(map[tmpNode2.x][tmpNode2.y]>=0)
                {
                    find++;
                    graph[i][map[tmpNode2.x][tmpNode2.y]]=tmpNode2.step;
                }
                Q.push(tmpNode2);
            }
            if(tmpNode1.y+1<y && !visit[tmpNode1.x][tmpNode1.y+1] && map[tmpNode1.x][tmpNode1.y+1]!=-2) //down move
            {
                tmpNode2.x=tmpNode1.x;
                tmpNode2.y=tmpNode1.y+1;
                tmpNode2.step=tmpNode1.step+1;
                visit[tmpNode2.x][tmpNode2.y]=true;
                if(map[tmpNode2.x][tmpNode2.y]>=0)
                {
                    find++;
                    graph[i][map[tmpNode2.x][tmpNode2.y]]=tmpNode2.step;
                }
                Q.push(tmpNode2);
            }

        }
    }


}
int main()
{
    int N;
    char ch[52];

    cin>>N;
    while(N--)
    {
        cin>>x>>y;
        number=0;
        cin.getline(ch,sizeof(ch));//吃掉空格。。。。eggache
        for(int i=0; i<y; i++)
        {
            cin.getline(ch,sizeof(ch));
            ch[x]='\n';
            for(int j=0; ch[j]!='\n'; j++)
            {
                switch(ch[j])
                {
                    case '#':
                        map[i][j]=-2;
                        break;
                    case '\n':
                        break;
                    case ' ':
                        map[i][j]=-1;
                        break;
                    case 'A':
                        number++;
                        map[i][j]=number;
                        node[number].x=i;
                        node[number].y=j;
                        break;
                    case 'S':
                         map[i][j]=0;
                         node[0].x=i;
                         node[0].y=j;
                         break;
                }
            }
        }
#if debug
        for(int p=0; p<y; p++)
        {
            for(int q=0; q<x; q++)
            {
                printf("%d ", map[p][q]);
            }
            printf("\n");
        }
#endif
        BFS();
#if debug
        for(int p=0; p<=number; p++)
        {
            for(int q=0; q<=number; q++)
            {
                printf("%d ", graph[p][q]);
            }
            printf("\n");
        }
#endif
        prim();
    }
    return 0;
}

转载于:https://www.cnblogs.com/eric-blog/archive/2011/01/03/1924804.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值