码迷,mamicode.com
首页 > 其他好文 > 详细

图论 --- BFS + MST

时间:2014-05-26 19:42:29      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:des   style   c   class   blog   code   

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7844   Accepted: 2623

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

 

 

 

 

bubuko.com,布布扣
#include<queue>
#include<cstdio>
#define INF 1<<30
#include<cstring>
#include<algorithm>
using namespace std;

int n,m,ans,cnt;
struct Node
{
    int x,y,step;
};
Node node[110];
int row,line;
int dist[60];
char Map[60][60];
bool vis[60][60],vist[60]; //邻接矩阵存储
int mark[60][60],dis[110][110];     //给每个字母一个编号
int dir[4][2]={-1,0, 0,1, 1,0, 0,-1};

void bfs(int index)
{
    queue<Node> que;
    memset(vis, false, sizeof(vis));
    Node now, next;
    node[index].step = 0;
    que.push(node[index]);
    vis[node[index].x][node[index].y] = true;
    while(!que.empty())
    {
        now = que.front();
        que.pop();
        int x = now.x, y = now.y;
        for(int i = 0; i < 4; ++i)
        {
            int tx = x + dir[i][0], ty = y +dir[i][1];
            if(vis[tx][ty] == false && Map[tx][ty] != #) //如果这一步可以走
            {
                next.x = tx;
                next.y = ty;
                vis[tx][ty] = true;
                next.step = now.step + 1;
                que.push(next);
                if(Map[next.x][next.y] == A || Map[next.x][next.y] == S)
                    dis[ mark[ node[index].x ][ node[index].y ] ][ mark[next.x][next.y] ] = next.step;
            }

        }
    }
}


int prim()
{
    for(int i = 0; i < cnt; ++i)
    {
        dist[i] = INF;
        vist[i] = false;
    }
    dist[0] = 0;
    while(1)
    {
        int min = INF, now = -1;
        for(int i = 0; i < cnt; ++i)
        {
            if(min > dist[i] && vist[i] == false)
            {
                min = dist[i];
                now = i;
            }
        }
        if(now == -1)
            return ans;
        ans += min;
        vist[now] = true;
        for(int i = 0; i < cnt; ++i)
            if(vist[i] == false && dist[i] > dis[now][i])
                dist[i] = dis[now][i];
    }
    return ans;
}

int main()
{
    int n_case;
    scanf("%d", &n_case);
    while(n_case--)
    {
        cnt = ans = 0;
        memset(mark, 0, sizeof(mark));
        scanf("%d%d", &row, &line);
        char ch;
        while(ch = getchar(), ch != \n);
        for(int i = 0; i < line; ++i)
        {
            for(int j = 0; j < row; ++j)
            {
                Map[i][j] = getchar();
                if(Map[i][j] == A || Map[i][j] == S)
                {
                    mark[i][j] = cnt;
                    node[cnt].x = i;
                    node[cnt++].y = j;
                }
            }
            while(ch = getchar(), ch != \n);
        }
        for(int i = 0; i < cnt; ++i)
            bfs(i);  
        prim();
        printf("%d\n", ans);
    }
    return 0;
}
bubuko.com,布布扣

 

 

图论 --- BFS + MST,布布扣,bubuko.com

图论 --- BFS + MST

标签:des   style   c   class   blog   code   

原文地址:http://www.cnblogs.com/acmer-jsb/p/3751838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!