1. 程式人生 > >【練習賽補題】poj 3026 Borg Maze 【bfs+最小生成樹】【坑~】

【練習賽補題】poj 3026 Borg Maze 【bfs+最小生成樹】【坑~】

lec pro 起點 live tin put gets work cond

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
先大致翻譯一遍:在一個地圖中,S要去到達每一個A,而S從一開始就可以分開各自去到達A,求出S到達每個A的總的最小步數。
題意:讀入一個字符矩陣,求出聯通所有A和S的最小生成樹權值,S只有一個。
思路:讀入字符圖str,將字符圖中的所有A和S進行編號(不用管S編號是否為1,因為用最小生成樹計算最小路徑權值會到達每一個頂點),遍歷字符圖,遇到A或S就進行BFS,BFS過程中,如果遇到A或S,就將步數存入
一個map圖(存編號路徑的權值),最後套用prime模板,輸出。
註意:1.讀入的時候,有n多空格的數據出現,不能用%c進行讀,正解是gets
   2.BFS時註意搜索除了#以外的所有頂點
~~~~這道題自己寫了兩天,因為用一個比較耗時的方法去寫,一直tle,但是自己始終覺得可以優化,最後還是tle~~後來小夥伴也A了這道題,就借鑒了他的思路。
我是的tle思路(hhh 奇怪的思路)是將每個編號頂點都進行遍歷查找,而小夥伴的方法是遇到一個編號頂點就整張圖進行遍歷,只要在遍歷過程中遇到了滿足要求的點就存入,大大降低查找時間,真是一個悲傷的tle~~

  
#include<stdio.h>
#include<queue>
#include<iostream>
#include<string.h>
using namespace std;
#define inf 0x3f3f3f3f
#define N 155
int map[N][N],book[55][55],e[55][55];
char str[55][55];//存儲字符圖 
int n,m,sum,count1;
int k[4][2]={0,1,0,-1,1,0,-1,0};

struct node{
    int x,y,step;
};

int BFS(int sx,int sy,int sn)
{
    int en,x,y,nx,ny,i;
    node start,tail,head;
    memset(book,0,sizeof(book));//每次查找圖都要進行清空 
    queue<node>Q;
    start.step = 0;
    start.x = sx;
    start.y = sy;
    Q.push(start);
    while(!Q.empty())
    {
        head = Q.front();
        Q.pop();
        for(i = 0; i < 4; i ++)
        {
            tail.x = head.x + k[i][0];
            tail.y = head.y + k[i][1];
            if(tail.x < 0||tail.x > n-1||tail.y < 0||tail.y > m-1)
                continue;
            if(!book[tail.x][tail.y]&&str[tail.x][tail.y]!=#)
            {
                book[tail.x][tail.y] = 1;
                tail.step = head.step+ 1;
                Q.push(tail);
                if(str[tail.x][tail.y] == A||str[tail.x][tail.y] == S)
                {//遇到符合條件的點就將起點到終點編號的權值存入map圖 
                    en = e[tail.x][tail.y];
                    map[sn][en] = tail.step;
                }
            }
            
        }
    }
}
void Getmap()
{
    int i,j;
    count1 = 0;
    memset(e,0,sizeof(e));
    for(i = 0; i < n; i ++)
        for(j = 0; j < m; j ++)
            if((str[i][j]==A||str[i][j] == S))
            {
                e[i][j] = ++count1;//對符合條件的字符進行編號 
            }
                
    for(i = 0; i < n; i ++)
        for(j = 0; j < m; j ++)
        {
             if(str[i][j] == A||str[i][j] == S)//遇到符合條件的字符就進行BFS 
            {
                BFS(i,j,e[i][j]);
            }
                
        } 
}

int Prime()//prime模板 
{
    int i,j,u,dis[N],flag[N],min;
    memset(flag,0,sizeof(flag));
    sum = 0;
    flag[1] = 1;
    for(i = 1; i <= count1; i ++)
        dis[i] = map[1][i];
    for(i = 1; i < count1; i ++)
    {
        min = inf;
        for(j = 1; j <= count1; j ++)
            if(!flag[j]&&dis[j] < min)
            {
                min = dis[j];
                u = j;
            }
        flag[u] = 1;
        sum += dis[u];
        for(j = 1; j <= count1; j ++)
            if(!flag[j]&&dis[j] > map[u][j])
                dis[j] = map[u][j];
    }
    return sum;
}

int main()
{
    int t,i,j;
    scanf("%d",&t);
    while(t --)
    {
        scanf("%d%d",&m,&n);
        gets(str[0]);
        for(i = 0; i < n; i ++)//讀圖一定註意,坑~~ 
            gets(str[i]);
        Getmap();//建圖 
        printf("%d\n",Prime());//輸出最小生成樹權值 
    } 
    return 0;
}

【練習賽補題】poj 3026 Borg Maze 【bfs+最小生成樹】【坑~】