您的位置:首页 > 其它

POJ 3026 Borg Maze

2015-03-13 14:36 337 查看

Borg Maze

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 3026

64-bit integer IO format: %lld Java class name: Main

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

解题:数据有点坑。。

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
#define pii pair<int,int>
using namespace std;
const int maxn = 110;
const int mx = 1000000;
int e[maxn][maxn],d[maxn][maxn],hs[maxn][maxn],ds[mx],n,m,tot;
bool done[mx];
char mp[maxn][maxn];
bool isIn(int x,int y) {
return x >=0 && x < n && y >= 0 && y < m;
}
void bfs(int x,int y) {
queue< pii >q;
q.push(make_pair(x,y));
memset(d,-1,sizeof(d));
d[x][y] = 0;
static const int dir[4][2] = {-1,0,1,0,0,-1,0,1};
while(!q.empty()) {
pii now = q.front();
q.pop();
for(int i = 0; i < 4; ++i) {
int tx = now.first + dir[i][0];
int ty = now.second + dir[i][1];
if(isIn(tx,ty) && mp[tx][ty] != '#' && d[tx][ty] == -1) {
d[tx][ty] = d[now.first][now.second] + 1;
q.push(make_pair(tx,ty));
if(mp[tx][ty] == 'A' || mp[tx][ty] == 'S') {
e[hs[x][y]][hs[tx][ty]] = d[tx][ty];
}
}
}
}
}

int prim() {
int ans = 0,cnt = 0;
for(int i = 0; i < tot; ++i) {
done[i] = false;
ds[i] = INF;
}
ds[0] = 0;
while(true) {
int minV = INF,idx = -1;
for(int i = 0; i < tot; ++i)
if(ds[i] < minV && !done[i]) minV = ds[idx = i];
if(minV == INF || idx == -1) break;
ans += minV;
done[idx] = true;
for(int i = 1; i < tot; ++i)
if(!done[i] && e[idx][i] < ds[i]) ds[i] = e[idx][i];
}
return ans;
}

int main() {
int kase;
scanf("%d",&kase);
while(kase--) {
scanf("%d %d",&m,&n);
gets(mp[0]);
for(int i = tot = 0; i < n; ++i) {
gets(mp[i]);
for(int j = 0; j < m; ++j) {
if(mp[i][j] == 'A' || mp[i][j] == 'S')
hs[i][j] = tot++;
}
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j)
if(mp[i][j] == 'A' || mp[i][j] == 'S') bfs(i,j);
}
printf("%d\n",prim());
}
return 0;
}


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