EricYang

Tech Spot of Eric

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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;
}

posted on 2011-01-03 16:27  Eric-Yang  阅读(229)  评论(0编辑  收藏  举报