zoukankan      html  css  js  c++  java
  • POJ 3026, Borg Maze

    Time Limit: 1000MS  Memory Limit: 65536K
    Total Submissions: 2598  Accepted: 793


    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

    Source
    Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001


    // POJ3026.cpp : Defines the entry point for the console application.
    //

    #include 
    <iostream>
    #include 
    <queue>
    #include 
    <map>
    #include 
    <string>
    #include 
    <algorithm>
    #include 
    <numeric>
    using namespace std;

    int main(int argc, char* argv[])
    {
        
    int cases;
        scanf(
    "%d",&cases);

        
    int X, Y;
        
    for (int c = 0; c < cases; ++c)
        {
            
    //read maze
            char maze[51][51];
            scanf(
    "%d%d\n"&X, &Y);
            
    for (int i = 0; i < Y; ++i)gets(maze[i]);

            
    //check points
            int x,y;
            map
    <string,int> points;
            
    char num = 0;
            
    for (int i = 0; i < Y; ++i)
                
    for (int j = 0; j < X; ++j)
                    
    if (maze[i][j] == 'S' || maze[i][j] == 'A')
                    {
                        points.insert(make_pair(
    string(1,(char)i) + string(1,(char)j),num));
                        
    ++num;
                    };
            
            
    //init d
            int d[105][105];
            
    int SIZE = points.size();
            
    for (int i = 0; i < SIZE; ++i)
                fill(
    &d[i][0],&d[i][SIZE], 100);

            
    //BFS
            map<string,int> ::iterator iter;
            
    int i = 0;
            
    for (iter = points.begin(); iter != points.end(); ++iter)
            {
                
    int dis[51][51];
                memset(dis, 
    -1sizeof(dis));
                
                queue
    <string> q;
                dis[(iter
    ->first)[0]][(iter->first)[1]] = 0;
                q.push(iter
    ->first);
                
    while (!q.empty())
                {
                    
    string cur = q.front();
                    q.pop();

                    
    if (maze[cur[0]][cur[1]] == 'S' || maze[cur[0]][cur[1]] == 'A')
                    {
                        d[i][points[cur]] 
    = dis[cur[0]][cur[1]];
                    }

                    
    if (dis[cur[0]][cur[1- 1== -1 && maze[cur[0]][cur[1- 1!= '#')
                    {
                        dis[cur[
    0]][cur[1- 1= dis[cur[0]][cur[1]] + 1;
                        q.push(
    string(1,(char)cur[0]) + string(1,(char)(cur[1- 1)));
                    }
                    
    if (dis[cur[0]][cur[1+ 1== -1 && maze[cur[0]][cur[1+ 1!= '#')
                    {
                        dis[cur[
    0]][cur[1+ 1= dis[cur[0]][cur[1]] + 1;
                        q.push(
    string(1,(char)cur[0]) + string(1,(char)(cur[1+ 1)));
                    }
                    
    if (dis[cur[0- 1][cur[1]] == -1 && maze[cur[0- 1][cur[1]] != '#')
                    {
                        dis[cur[
    0- 1][cur[1]] = dis[cur[0]][cur[1]] + 1;
                        q.push(
    string(1,(char)(cur[0- 1)) + string(1,(char)cur[1]));
                    }
                    
    if (dis[cur[0+ 1][cur[1]] == -1 &&  maze[cur[0+ 1][cur[1]] != '#')
                    {
                        dis[cur[
    0+ 1][cur[1]] = dis[cur[0]][cur[1]] + 1;
                        q.push(
    string(1,(char)(cur[0+ 1)) + string(1,(char)cur[1]));
                    }
                }
                
    ++i;
            }

            
    //Prim
            int l[105];
            
    bool used[105];
            memset(used, 
    0sizeof(used));
            
    for (int i = 0; i < SIZE; ++i) l[i] = d[0][i];
            used[
    0= true;
            
    for (int i = 1; i < SIZE; ++i)
            {
                
    short len = 20000;
                
    int pos = 0;
                
    for (int j = 0; j < SIZE; ++j)
                    
    if (used[j] == false && l[j] < len)
                    {
                        len 
    = l[j];
                        pos 
    = j;
                    };

                used[pos] 
    = true;

                
    for (int j = 0; j < SIZE; ++j)
                    
    if (used[j] == false && l[j] > d[pos][j])l[j] = d[pos][j];
            }

            cout 
    << accumulate(&l[0], &l[SIZE], 0<<endl;
        }

        
    return 0;
    }

  • 相关阅读:
    poj 2046 Power Strings KMP
    谈谈需求变更的用户签字确认问题
    Oracle 10g客户端 安装(配图)
    如何让有能力的下属承担更多职责
    软件性能测试工具在测试工作中的重要性
    Oracle 10g 服务器端 安装图解
    JS 操作页面基础操作:禁止另存 防止复制 防止选择
    Javascript 长整型 转 C# DateTime
    配置AJAX Enabled WCF在hosting时: Showing the serviceMetadata in an ASP.NET AJAX Service
    ICON资源网站
  • 原文地址:https://www.cnblogs.com/asuran/p/1577185.html
Copyright © 2011-2022 走看看