您的位置:首页 > 其它

poj3026 Borg Maze

2017-09-20 23:15 302 查看
Borg Maze

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15358 Accepted: 4973
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

题意:就是从S点有一伙人出发去消灭A点的敌人,在S点或者A点可以分裂成几个小队然后分别走,这样路径=总队路径+各个小队路径   问你怎样路径最短

题解:首先把S点和A点抽象成图,然后就是最小生成树问题了。关键是怎么求各点之间的距离,对每个点都用一次BFS即可。

#include<iostream>
#include<stdio.h>
#include<queue>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAXN = 50 + 2;
const int MAX = 100 + 2;
const int INF = 0x3f3f3f3f;

//BFS
int row,col;
char map[MAXN][MAXN];
int alien[MAXN][MAXN];
int vis[MAXN][MAXN];
int dis[MAXN][MAXN];
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};

//Prim
int G[MAX][MAX];
int addvnew[MAX];
int dist[MAX];
int n; // vertex num

void bfs(int sx, int sy){
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
int pos = sx * col + sy;
queue<int> q;
q.push(pos);
vis[sx][sy] = 1;
while(!q.empty()){
int temp = q.front();
int px = temp / col;
int py = temp % col;
q.pop();
if(alien[px][py])
G[alien[sx][sy]][alien[px][py]] = dis[px][py];
for(int d = 0;d < 4; ++d){
int fx = px + dx[d];
int fy = py + dy[d];
if(fx >= 0 && fy >= 0 && fx <= row && fy <= col && map[fx][fy] != '#' && !vis[fx][fy]){
vis[fx][fy] = 1;
dis[fx][fy] = dis[px][py] + 1;
q.push(fx*col+fy);
}
}
}
}

void prim(int s)
{
int sum = 0;
for(int i = 1; i <= n; ++i){
dist[i] = G[s][i];
addvnew[i] = 0;
}
addvnew[s] = 1;

for(int i = 1;i <= n ; ++i){
if(i != s){
int min = INF;
int v = -1;
for(int j = 1; j <= n; ++j){
if(!addvnew[j] && dist[j] < min){
min = dist[j];
v = j;
}
}

if(v != -1){
addvnew[v] = 1;

sum += dist[v];

for(int j = 1;j <= n; ++j){
if(!addvnew[j] && G[v][j] < dist[j]){
dist[j] = G[v][j];

}
}
}
}
}

printf("%d\n",sum);

}

int main()
{
int cnt;
cin >> cnt;
while(cnt--){
memset(G,0,sizeof(G));
memset(dist,0,sizeof(dist));
memset(addvnew,0,sizeof(addvnew));
memset(map,0,sizeof(map));
memset(alien,0,sizeof(alien));

cin >> col >> row;

char ch;
n = 0;
while((ch = getchar()) != '\n')
continue;

for(int i = 0;i < row;++i){
gets(map[i]);
for(int j = 0;j < col; ++j){
if(map[i][j] == 'A' || map[i][j] == 'S')
alien[i][j] = ++n;
}
}

for(int i = 0;i < row; ++i){
for(int j = 0;j < col; ++j){
if(alien[i][j])
bfs(i,j);
}
}

prim(1);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: