24h購物| | PChome| 登入
2013-09-16 08:01:56| 人氣3,044| 回應0 | 上一篇 | 下一篇

[UVA] 10307 - Killing Aliens in Borg Maze

推薦 0 收藏 0 轉貼0 訂閱站台

Problem H

Killing Aliens in Borg Maze

Input: standard input

Output:  standard output

Time Limit: 3 seconds

Memory Limit: 32 MB

 

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 defined 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 containing 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 successful 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


(Joint Effort Contest, Problem Source: Swedish National Programming Contest, arranged by department of Computer Science at Lund Institute of Technology.)

這題是要同化外星人,求花費的最小成本。

至於成本的部分真的很難懂!

就當作兩個外星人之間的距離,即使重疊仍要計算到成本中。

那麼題目就變成一個最小生成樹。

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
char g[55][55];
int used[55][55], n, m, mark[55][55];
int bsize;
int ng[105][105];
void dfs(int x, int y, int label) {
    if(x < 0 || y < 0 || x >= n || y >= m)
        return;
    if(used[x][y] || g[x][y] == ' ' || g[x][y] == '#')
        return;
    bsize++;
    used[x][y] = 1;
    mark[x][y] = label;
    dfs(x+1, y, label);
    dfs(x-1, y, label);
    dfs(x, y+1, label);
    dfs(x, y-1, label);
}
void bfs(int label) {
    int dx[] = {0, 0, 1, -1};
    int dy[] = {1, -1, 0, 0};
    int x, y, tx, ty;
    int i, j, k;
    queue<int> X, Y;
    memset(used, 0, sizeof(used));
    for(i = 0; i < n; i++) {
        for(j = 0; j < m; j++) {
            if(mark[i][j] == label) {
                X.push(i), Y.push(j);
                used[i][j] = 1;
            }
        }
    }
    while(!X.empty()) {
        x = X.front(), X.pop();
        y = Y.front(), Y.pop();
        for(i = 0; i < 4; i++) {
            tx = x+dx[i], ty = y+dy[i];
            if(tx < 0 || ty < 0 || tx >= n || ty >= m)
                continue;
            if(g[tx][ty] == '#')
                continue;
            if(g[tx][ty] != ' ') {
                ng[label][mark[tx][ty]] = min(ng[label][mark[tx][ty]], used[x][y]-1);
            }
            if(used[tx][ty] == 0) {
                used[tx][ty] = used[x][y]+1;
                X.push(tx), Y.push(ty);
            }
        }
    }
}
int parent[105], rank[105];
int findp(int x) {
    return parent[x] == x ? x : (parent[x]=findp(parent[x]));
}
int joint(int x, int y) {
    x = findp(x), y = findp(y);
    if(x == y)    return 0;
    if(rank[x] > rank[y])
        rank[x] += rank[y], parent[y] = x;
    else
        rank[y] += rank[x], parent[x] = y;
    return 1;
}
struct edge {
    int x, y, v;
    bool operator<(const edge &a) const {
        return v < a.v;
    }
};
edge D[10000];
int main() {
    int testcase;
    int i, j, k;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %d", &m, &n);
        while(getchar() != '\n');
        for(i = 0; i < n; i++)
            gets(g[i]);
        memset(used, 0, sizeof(used));
        memset(mark, 0, sizeof(mark));
        int label = 0;
        bsize = 0;
        for(i = 0; i < n; i++)
            for(j = 0; j < m; j++)
                if(used[i][j] == 0 && g[i][j] != ' ' && g[i][j] != '#' && mark[i][j] == 0)
                    label++, dfs(i, j, label);
        for(i = 1; i <= label; i++) {
            for(j = 1; j <= label; j++)
                ng[i][j] = 0xfffffff;
            ng[i][i] = 0;
            bfs(i);
        }
        for(i = 1; i <= label; i++)
            parent[i] = i, rank[i] = 1;
        m = 0;
        for(i = 1; i <= label; i++) {
            for(j = i+1; j <= label; j++) {
                D[m].x = i, D[m].y = j;
                D[m].v = ng[i][j];
                m++;
            }
        }
        sort(D, D+m);
        int cost = 0;//MST
        for(i = 0; i < m; i++) {
            if(joint(D[i].x, D[i].y)) {
                cost += D[i].v;
            }
        }
        printf("%d\n", cost+bsize-1);
    }
    return 0;
}
/*
2
6 5
#####
#A#A##
# # A#
#S  ##
#####
7 7
#####  
#AAA###
#    A#
# S ###
#     #
#AAA###
#####  
*/

台長: Morris
人氣(3,044) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][dp] 11125 - Arrange Some Marbles
此分類上一篇:[UVA][dp] 976 - Bridge Building

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文