Poj 3026 Borg maze

Source: Internet
Author: User

In the eyes of many great gods, This is a water problem, but cainiao cannot afford to hurt, and it is not easy to understand English, and it is found that the BFS written for one day yesterday has been relentlessly wrong. ah, it's Valentine's Day yesterday, diaosi is typing on the code. It's still wrong. It cannot hurt... Now let's get down to the point. This is a question of the Minimum Spanning Tree, but the weights between each point are not given every day, we need to use BFs to find out. My BFS is always wrong. I learned about pengge, and even forget it. I have to do more exercises, and cainiao cannot rest ,,,

There is also a problem with the test data, which is described in discuss of poj. When you do this, you can take a look at it ,,

Borg maze
Time limit:1000 ms   Memory limit:65536 K
Total submissions:7006   Accepted:2362

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 conducting 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
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#define oo 1 << 30using namespace std;int a[100][100],b[120][120];int c[120][4],d[120];bool v[120];int status[120][120],queue[100000][2];int vex[]= {-1,1,0,0};int vey[]= {0,0,-1,1};int n,m,s;string s1;int prim(){    int n = s-1;    memset(v , false , sizeof(v));    int sum = 0;    for(int i = 1; i <= n; i++) d[i] = b[1][i];    d[1] = 0;    v[1] = true;    for(int i = 1; i <= n-1; i++)    {        int x, m = oo;        for(int y = 1; y <= n; y++) if(!v[y] && d[y]<=m) m = d[x=y];        sum += m;        v[x] = true;        for(int y = 1; y <= n; y++) if(!v[y] && d[y] > b[x][y]) d[y] = b[x][y];    }    return sum;}void bfs(int x, int y){    int i, base, top, posx, posy;    int xend, yend, sum[120][120];    posx = x;    posy = y;    memset(status , 0 , sizeof(status));    memset(sum , 0 , sizeof(sum));    base = top = 0;    queue[top][0] = x;    queue[top++][1] = y;    status[x][y] = 1;    while(base < top)    {        x = queue[base][0];        y = queue[base++][1];        for(i = 0; i <= 3; i++)        {            xend = x+vex[i];            yend = y+vey[i];            if(xend >= 1 && xend <= n && yend >= 1 && yend <= m && !status[xend][yend] && a[xend][yend]!=0)            {                sum[xend][yend] = sum[x][y]+1;                if(a[xend][yend] != -1)                {                    b[a[posx][posy]][a[xend][yend]]=sum[xend][yend];                    b[a[xend][yend]][a[posx][posy]]=sum[xend][yend];                }                queue[top][0] = xend;                queue[top++][1] = yend;                status[xend][yend] = 1;            }        }    }}int main(){    int i, j, t;    char d, str[100];    cin >>t;    while(t--)    {        cin >>m>>n;        gets(str);        s = 2;        for(i = 1; i <= n; i++)        {            for(j = 1; j <= m; j++)            {                d=cin.get();                if(d == '#')                    a[i][j] = 0;                else if(d == 'A')                {                    a[i][j] = s;                    c[s][0] = i;                    c[s][1] = j;                    s++;                }                else if(d == 'S')                {                    a[i][j] = 1;                    c[1][0] = i;                    c[1][1] = j;                }                else if(d == ' ')                    a[i][j] = -1;            }            cin.get();        }        for(i = 1; i <= s-1; i++)            bfs(c[i][0], c[i][1]);        for(i = 1; i < s-1; i++)            for(j = 1; j <= s-1; j++)                if(i == j)                    b[i][j] = 0;        printf("%d\n",prim());    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.