POj 3026 Borg Maze (bfs求权值的最小生成树)

来源:互联网 发布:电脑写小说的软件 编辑:程序博客网 时间:2024/06/04 01:14

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 letterS” 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.

2
6 5
#####
#A#A##
# # A#
#S ##
#####
7 7
#####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
8
11

题意就是做一个最小生成树,不过点是迷宫中的A和S,然后求出最小的花费

不得不说,poj提交的时候真的是各种莫名其妙的错误,有时是g++WA,C++能过,有时又反过来,这一题也是,用getchar()吸回车就是错误,用gets吸回车就能过,真的是无语,当然这个题非常不错。

思路:就是先把地图中的点全部找出来,可以把S 当做A,用结构体记录,然后再对每一个点bfs,求出他到其他点的距离,可以得到任意两个点的距离,最后再跑一下prim就能得到结果了

代码如下:

#include<cstdio>#include<cstring>#include<iostream>#include<vector>#include<cmath>#include<string>#include<queue>#include<set>#include<algorithm>using namespace std;const int MAX_V = 160;const int INF = 0x3f3f3f3f;char Map[MAX_V][MAX_V];int book[MAX_V][MAX_V];bool used[MAX_V][MAX_V];int e[MAX_V][MAX_V];int t,n,m,top;const int xx[] = {0,-1,0,1};const int yy[] = {1,0,-1,0};struct node{    int x,y;    int step;}q[MAX_V*MAX_V],es;void bfs(node a_){    queue<node> q;    memset(used,false,sizeof(used));    q.push(a_);    used[a_.x][a_.y] = true;    while(!q.empty()){        es = q.front();        q.pop();        if(book[es.x][es.y] != 0){            int x = book[es.x][es.y];//获取这是第几个点            int y = book[a_.x][a_.y];            e[x][y] = e[y][x] = es.step;//获得两点间的距离        }        node nx;        for(int i=0;i<4;i++){            nx.x = es.x + xx[i];            nx.y = es.y + yy[i];            nx.step = es.step + 1;            if(nx.x < 0 || nx.y < 0 || nx.x >= n || nx.y >= m)                continue;            if(!used[nx.x][nx.y] && Map[nx.x][nx.y] != '#'){//注意,除了墙,其他都可以经过,包括A                used[nx.x][nx.y] = true;                q.push(nx);            }        }    }}bool vis[MAX_V];int cost[MAX_V];int prim(int a_){//裸的prim    fill(cost+1,cost+1+top,INF);    memset(vis,false,sizeof(vis));    cost[a_] = 0;    int res = 0;    while(true){        int u = -1;        for(int i=1;i<=top;i++){            if(!vis[i] && (u == -1 || cost[i] < cost[u]))                u = i;        }        if(u == -1) break;        vis[u] = true;        res += cost[u];        for(int i=1;i<=top;i++){            if(!vis[i] && cost[i] > e[u][i])                cost[i] = e[u][i];        }    }    return res;}int main(void){    scanf("%d",&t);    while(t--){        scanf("%d %d",&m,&n);        char str[10];        gets(str);//这里getchar()不能过,莫名其妙        top = 0;        memset(book,0,sizeof(book));        for(int i=0;i<n;i++){            gets(Map[i]);            for(int j=0;j<m;j++){                if(Map[i][j] == 'S')                    Map[i][j] = 'A';                if(Map[i][j] == 'A'){                    top++;                    q[top].x = i;q[top].y = j;                    q[top].step = 0;                    book[i][j] = top;//记录这个坐标是第几个点                }            }        }        for(int i=1;i<=top;i++)            for(int j=1;j<=top;j++)                if(i == j)  e[i][j] = 0;                else    e[i][j] = INF;        for(int i=1;i<=top;i++){            bfs(q[i]);        }        printf("%d\n",prim(1));    }    return 0;}