博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3026, Borg Maze
阅读量:6606 次
发布时间:2019-06-24

本文共 4957 字,大约阅读时间需要 16 分钟。

Time Limit: 1000MS  Memory Limit: 65536K

Total Submissions: 2598  Accepted: 793

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


//
 POJ3026.cpp : Defines the entry point for the console application.
//
#include 
<
iostream
>
#include 
<
queue
>
#include 
<
map
>
#include 
<
string
>
#include 
<
algorithm
>
#include 
<
numeric
>
using
 
namespace
 std;
int
 main(
int
 argc, 
char
*
 argv[])
{
    
int
 cases;
    scanf(
"
%d
"
,
&
cases);
    
int
 X, Y;
    
for
 (
int
 c 
=
 
0
; c 
<
 cases; 
++
c)
    {
        
//
read maze
        
char
 maze[
51
][
51
];
        scanf(
"
%d%d\n
"
&
X, 
&
Y);
        
for
 (
int
 i 
=
 
0
; i 
<
 Y; 
++
i)gets(maze[i]);
        
//
check points
        
int
 x,y;
        map
<
string
,
int
>
 points;
        
char
 num 
=
 
0
;
        
for
 (
int
 i 
=
 
0
; i 
<
 Y; 
++
i)
            
for
 (
int
 j 
=
 
0
; j 
<
 X; 
++
j)
                
if
 (maze[i][j] 
==
 
'
S
'
 
||
 maze[i][j] 
==
 
'
A
'
)
                {
                    points.insert(make_pair(
string
(
1
,(
char
)i) 
+
 
string
(
1
,(
char
)j),num));
                    
++
num;
                };
        
        
//
init d
        
int
 d[
105
][
105
];
        
int
 SIZE 
=
 points.size();
        
for
 (
int
 i 
=
 
0
; i 
<
 SIZE; 
++
i)
            fill(
&
d[i][
0
],
&
d[i][SIZE], 
100
);
        
//
BFS
        map
<
string
,
int
>
 ::iterator iter;
        
int
 i 
=
 
0
;
        
for
 (iter 
=
 points.begin(); iter 
!=
 points.end(); 
++
iter)
        {
            
int
 dis[
51
][
51
];
            memset(dis, 
-
1
sizeof
(dis));
            
            queue
<
string
>
 q;
            dis[(iter
->
first)[
0
]][(iter
->
first)[
1
]] 
=
 
0
;
            q.push(iter
->
first);
            
while
 (
!
q.empty())
            {
                
string
 cur 
=
 q.front();
                q.pop();
                
if
 (maze[cur[
0
]][cur[
1
]] 
==
 
'
S
'
 
||
 maze[cur[
0
]][cur[
1
]] 
==
 
'
A
'
)
                {
                    d[i][points[cur]] 
=
 dis[cur[
0
]][cur[
1
]];
                }
                
if
 (dis[cur[
0
]][cur[
1
-
 
1
==
 
-
1
 
&&
 maze[cur[
0
]][cur[
1
-
 
1
!=
 
'
#
'
)
                {
                    dis[cur[
0
]][cur[
1
-
 
1
=
 dis[cur[
0
]][cur[
1
]] 
+
 
1
;
                    q.push(
string
(
1
,(
char
)cur[
0
]) 
+
 
string
(
1
,(
char
)(cur[
1
-
 
1
)));
                }
                
if
 (dis[cur[
0
]][cur[
1
+
 
1
==
 
-
1
 
&&
 maze[cur[
0
]][cur[
1
+
 
1
!=
 
'
#
'
)
                {
                    dis[cur[
0
]][cur[
1
+
 
1
=
 dis[cur[
0
]][cur[
1
]] 
+
 
1
;
                    q.push(
string
(
1
,(
char
)cur[
0
]) 
+
 
string
(
1
,(
char
)(cur[
1
+
 
1
)));
                }
                
if
 (dis[cur[
0
-
 
1
][cur[
1
]] 
==
 
-
1
 
&&
 maze[cur[
0
-
 
1
][cur[
1
]] 
!=
 
'
#
'
)
                {
                    dis[cur[
0
-
 
1
][cur[
1
]] 
=
 dis[cur[
0
]][cur[
1
]] 
+
 
1
;
                    q.push(
string
(
1
,(
char
)(cur[
0
-
 
1
)) 
+
 
string
(
1
,(
char
)cur[
1
]));
                }
                
if
 (dis[cur[
0
+
 
1
][cur[
1
]] 
==
 
-
1
 
&&
  maze[cur[
0
+
 
1
][cur[
1
]] 
!=
 
'
#
'
)
                {
                    dis[cur[
0
+
 
1
][cur[
1
]] 
=
 dis[cur[
0
]][cur[
1
]] 
+
 
1
;
                    q.push(
string
(
1
,(
char
)(cur[
0
+
 
1
)) 
+
 
string
(
1
,(
char
)cur[
1
]));
                }
            }
            
++
i;
        }
        
//
Prim
        
int
 l[
105
];
        
bool
 used[
105
];
        memset(used, 
0
sizeof
(used));
        
for
 (
int
 i 
=
 
0
; i 
<
 SIZE; 
++
i) l[i] 
=
 d[
0
][i];
        used[
0
=
 
true
;
        
for
 (
int
 i 
=
 
1
; i 
<
 SIZE; 
++
i)
        {
            
short
 len 
=
 
20000
;
            
int
 pos 
=
 
0
;
            
for
 (
int
 j 
=
 
0
; j 
<
 SIZE; 
++
j)
                
if
 (used[j] 
==
 
false
 
&&
 l[j] 
<
 len)
                {
                    len 
=
 l[j];
                    pos 
=
 j;
                };
            used[pos] 
=
 
true
;
            
for
 (
int
 j 
=
 
0
; j 
<
 SIZE; 
++
j)
                
if
 (used[j] 
==
 
false
 
&&
 l[j] 
>
 d[pos][j])l[j] 
=
 d[pos][j];
        }
        cout 
<<
 accumulate(
&
l[
0
], 
&
l[SIZE], 
0
<<
endl;
    }
    
return
 
0
;
}

转载于:https://www.cnblogs.com/asuran/archive/2009/10/01/1577185.html

你可能感兴趣的文章
年礼成快递企业不再接件主因:苹果产品最疯狂
查看>>
[转载] 七龙珠第一部——第101话 武道会结束
查看>>
Android N(7.0) 在ListView里显示EditText时软键盘弹出时会自动切换到全键盘的问题?...
查看>>
【转自论坛】如何增加表空间的大小
查看>>
【总结整理】《人人都是产品经理》---读后感
查看>>
session与cookie的区别
查看>>
java 获取IP地址
查看>>
框框下面的小箭头的实现
查看>>
android studio解决微信登录,百度地图等调试问题
查看>>
ural 1109,NYOJ 239,匈牙利算法邻接表
查看>>
P147、面试题26:复杂链表的复制
查看>>
文件及IO操作(三)
查看>>
割点与桥
查看>>
51.字符串操作函数
查看>>
ASP.NET MVC5中View显示Html
查看>>
Eclipse连接到My sql数据库的操作总结/配置数据库驱动
查看>>
python 将unicode编码转换为汉字的几种方法
查看>>
服务器负载粗略估算
查看>>
Spring 中 ApplicationContext 和 BeanFactory 的区别
查看>>
3.28Day09函数
查看>>