Borg Maze

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 20129

 

Accepted: 6419

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

bfs+最短路

首先要用bfs 处理 每两个点之间的距离。

我的超时代码是调用n*n次bfs去搜距离,结果tle。

再优化一下:将n*n复杂度可以降到n。也就是遍历n个点,以其中每个点作起点一次,来搜到达其他各点

的最短距离。 然后就是最短路 板子。

我的超时代码:

/*
x,y
空格是 空地 #号是障碍 A 是外星人 S代表开始调查的地点
这个地图是封闭的,最多100个外星人 每个都可到达
求搜索地图的最小成本
*/
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#define Max 111
#define inf 0x3f3f3f3f
using namespace std;
int t, n, m, cnt, ans;
char mapp[55][55];
int edge[Max][Max], vis[Max],dis[Max];
int dir[4][2]={{0, 1},{0, -1},{1, 0},{-1, 0}};
struct Node{
int x, y;
int step;
}node[Max];
void init() {
for(int i = 0; i < cnt; i++) {
for(int j = 0; j < cnt; j++) {
if(i==j) edge[i][j] = 0;
else edge[i][j] = edge[j][i] =inf;
}
}
}
//肯定能到达
int bfs(Node st,Node ed){
int vis[Max][Max];
queue<Node> q;
while(!q.empty()) q.pop();
memset(vis, 0, sizeof(vis));
q.push(st);
vis[st.x][st.y] = 1;
st.step = 0;
while(!q.empty()) {
Node u = q.front(),v;
q.pop();
if(u.x == ed.x && u.y == ed.y ){
return u.step;
}
for(int i = 0; i < 4; i++) {
v.x = u.x + dir[i][0];
v.y = u.y + dir[i][1];
if(v.x < 0 || v.y < 0 || v.x >= n || v.y >= m || mapp[v.x][v.y] == '#' || vis[v.x][v.y]) continue;
v.step = u.step + 1;
vis[v.x][v.y] = 1;
q.push(v);
}
}
}
void prim(){
memset(vis, 0, sizeof(vis));
memset(dis, inf, sizeof(dis));
for(int i = 0; i < cnt; i++) {
dis[i] = edge[0][i];
}
dis[0] = 0;
vis[0] = 1;
for(int i = 1; i < cnt; i++) {
int minn = inf, k = -1;
for(int j = 0; j < cnt; j++){
if(!vis[j] && dis[j] < minn) {
minn = dis[j];
k = j;
}
}
vis[k] = 1;
ans += minn;
// cout << "k: " << k << "minn: " << minn << endl;
for(int v = 0; v < cnt; v++) {
if(!vis[v] && dis[v] > edge[k][v]) {
dis[v] = edge[k][v];
}
}
}
}
int main() {
// ios::sync_with_stdio(false);
scanf("%d",&t);
for(int i = 0; i < t; i++) {
// cin >> n >> m;
scanf("%d %d",&n,&m);
cnt = 0;
for(int j = 0; j < n; j++) {
// getline(cin, mapp[j]);
gets(mapp[j]);
for(int k = 0; k < strlen(mapp[j]); k++) {
if(mapp[j][k] == 'A'||mapp[j][k] == 'S') {
node[cnt].x = j;
node[cnt].y = k; node[cnt++].step = 0;
}
}
}
init();
// cout << "----" << cnt << endl;
for(int i = 0; i < cnt; i++){
for(int j = i+1; j < cnt; j++) {
int len = bfs(node[i], node[j]);
// cout << i << " " << j << "len : "<< len << endl;
edge[i][j] = edge[j][i] = len;

}
}
ans = 0;
prim();
// cout << ans << endl;
printf("%d\n",ans);
}
return 0;
}

 

总算是改好了。awsl

/*
x,y
空格是 空地 #号是障碍 A 是外星人 S代表开始调查的地点
这个地图是封闭的,最多100个外星人 每个都可到达
求搜索地图的最小成本
*/
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <cstdio>
#define Max 111
#define inf 0x3f3f3f3f
using namespace std;
int t, n, m, cnt, ans;
char mapp[55][55];
int edge[Max][Max], vis[Max],dis[Max];
int dir[4][2]={{0, 1},{0, -1},{1, 0},{-1, 0}};
struct Node{
int x, y;
int step;
}node[Max];
void init() {
for(int i = 0; i < cnt; i++) {
for(int j = 0; j < cnt; j++) {
if(i==j) edge[i][j] = 0;
else edge[i][j] = edge[j][i] =inf;
}
}
}
//肯定能到达
// int bfs(Node st,Node ed){
// int vis[Max][Max];
// queue<Node> q;
// while(!q.empty()) q.pop();
// memset(vis, 0, sizeof(vis));
// q.push(st);
// vis[st.x][st.y] = 1;
// st.step = 0;
// while(!q.empty()) {
// Node u = q.front(),v;
// q.pop();
// if(u.x == ed.x && u.y == ed.y ){
// return u.step;
// }
// for(int i = 0; i < 4; i++) {
// v.x = u.x + dir[i][0];
// v.y = u.y + dir[i][1];
// if(v.x < 0 || v.y < 0 || v.x >= n || v.y >= m || mapp[v.x][v.y] == '#' || vis[v.x][v.y]) continue;
// v.step = u.step + 1;
// vis[v.x][v.y] = 1;
// q.push(v);
// }
// }
// }
void bfs(int st) {
int vis[Max][Max],dist[Max][Max];
queue<Node> q;
while(!q.empty()) q.pop();
memset(vis,0,sizeof(vis));
memset(dist,0,sizeof(dist));
q.push(node[st]);
vis[node[st].x][node[st].y] = 1;
dist[node[st].x][node[st].y] = 0;
while(!q.empty()) {
Node u = q.front(), v;
q.pop();
for(int i = 0; i < 4; i++) {
v.x = u.x + dir[i][0];
v.y = u.y + dir[i][1];
if(v.x < 0 || v.y < 0 || v.x >= n || v.y >= m || mapp[v.x][v.y] == '#' || vis[v.x][v.y]) continue;
v.step = u.step + 1;
if(mapp[v.x][v.y] == 'A' || mapp[v.x][v.y] == 'S'){
dist[v.x][v.y] = v.step;
}
vis[v.x][v.y] = 1;
q.push(v);
}
}
for(int i = 0; i < cnt; i++) {
edge[st][i] = edge[i][st] = dist[node[i].x][node[i].y];
}
}
void prim(){
memset(vis, 0, sizeof(vis));
memset(dis, inf, sizeof(dis));
for(int i = 0; i < cnt; i++) {
dis[i] = edge[0][i];
}
dis[0] = 0;
vis[0] = 1;
for(int i = 1; i < cnt; i++) {
int minn = inf, k = -1;
for(int j = 0; j < cnt; j++){
if(!vis[j] && dis[j] < minn) {
minn = dis[j];
k = j;
}
}
vis[k] = 1;
ans += minn;
// cout << "k: " << k << "minn: " << minn << endl;
for(int v = 0; v < cnt; v++) {
if(!vis[v] && dis[v] > edge[k][v]) {
dis[v] = edge[k][v];
}
}
}
}
int main() {
// ios::sync_with_stdio(false);
scanf("%d",&t);
for(int i = 0; i < t; i++) {
// cin >> n >> m;
scanf("%d %d",&n,&m);
getchar();
cnt = 0;
for(int j = 0; j < n; j++) {
// getline(cin, mapp[j]);
gets(mapp[j]);
for(int k = 0; k < strlen(mapp[j]); k++) {
if(mapp[j][k] == 'A'||mapp[j][k] == 'S') {
node[cnt].x = j;
node[cnt].y = k; node[cnt++].step = 0;
}
}
}
init();
// cout << "----" << cnt << endl;

//TLE
// for(int i = 0; i < cnt; i++){
// for(int j = i+1; j < cnt; j++) {
// int len = bfs(node[i], node[j]);
// // cout << i << " " << j << "len : "<< len << endl;
// edge[i][j] = edge[j][i] = len;

// }
// }
for(int i = 0; i < cnt; i++) {
edge[i][i] = 0;
bfs(i);
}
ans = 0;
prim();
// cout << ans << endl;
printf("%d\n",ans);
}
return 0;
}