Chat: 最近进入考试周了。。。 写题速度越来越慢, 考完试之后准备继续投入战斗~
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.
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.
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.
For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.
2 6 5 ##### #A#A## # # A# #S ## ##### 7 7 ##### #AAA### # A# # S ### # # #AAA### #####
8 11
题目大意:
这题我看了一遍又一遍, 最后还是靠着翻译 + 来回理解才弄懂的。 大致意思就是说在这么一张地图里S代表出发的地点, A代表外星人, 现在我们要找到这张地图中所有的外星人。 但是有一个特殊条件, 就是人可以分成几路去寻找Aliens, 求最后所走的最小步数。
解题思路:
首先拿到这么一张地图, 我们要是直接去求最小步数的话是没办法下手的, 所以我们要根据图中所给的A和S的坐标新建一张点和点之间最短距离的权值图, 那么这个时候其实A和S都可以归为一类点来处理了。 对于最短距离, 我们首先想到的肯定是BFS, 好了, 当权值图建出来之后那么现在这道题又变成了最小生成树的题, 至于从哪个点开始, 想从哪一个点开始都行喽~ 因为A和S其实已经没有区别了。
代码:
#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
using namespace std;
/*tools:
*ios::sync_with_stdio(false);
*freopen("input.txt", "r", stdin);
*/
*ios::sync_with_stdio(false);
*freopen("input.txt", "r", stdin);
*/
typedef long long ll;
typedef unsigned long long ull;
const int dir[9][2] = { 0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1, 0, 0 };
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = (int) 1e9 + 7;
const int Max = (int) 1e2 + 10;
typedef unsigned long long ull;
const int dir[9][2] = { 0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1, 0, 0 };
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = (int) 1e9 + 7;
const int Max = (int) 1e2 + 10;
int n, m, tot, temp[Max][Max];
int c[Max][Max], t[Max][Max];
char Map[Max][Max];
bool vis[Max];
int c[Max][Max], t[Max][Max];
char Map[Max][Max];
bool vis[Max];
void BFS(int x, int y) {
queue < pair<int, int> > Q;
while (!Q.empty()) Q.pop();
memset(t, -1, sizeof(t));
t[x][y] = 0;
Q.push(make_pair(x, y));
while (!Q.empty()) {
pair<int, int> now = Q.front();
Q.pop();
if (temp[now.first][now.second] != -1) {
//from x y to now.x now.y
c[temp[x][y]][temp[now.first][now.second]] = t[now.first][now.second];
}
for (int i = 0; i < 4; ++i) {
int xx = now.first + dir[i][0];
int yy = now.second + dir[i][1];
if (Map[xx][yy] == '#' || t[xx][yy] != -1) continue;
t[xx][yy] = t[now.first][now.second] + 1;
Q.push(make_pair(xx, yy));
}
}
}
queue < pair<int, int> > Q;
while (!Q.empty()) Q.pop();
memset(t, -1, sizeof(t));
t[x][y] = 0;
Q.push(make_pair(x, y));
while (!Q.empty()) {
pair<int, int> now = Q.front();
Q.pop();
if (temp[now.first][now.second] != -1) {
//from x y to now.x now.y
c[temp[x][y]][temp[now.first][now.second]] = t[now.first][now.second];
}
for (int i = 0; i < 4; ++i) {
int xx = now.first + dir[i][0];
int yy = now.second + dir[i][1];
if (Map[xx][yy] == '#' || t[xx][yy] != -1) continue;
t[xx][yy] = t[now.first][now.second] + 1;
Q.push(make_pair(xx, yy));
}
}
}
void Init() {
tot = 0;
memset(temp, -1, sizeof(temp));
for (int i = 0; i < n; ++i) {
gets(Map[i]);
for (int j = 0; j < m; ++j) {
if (Map[i][j] == 'A' || Map[i][j] == 'S') {
temp[i][j] = tot++;
}
}
}
tot = 0;
memset(temp, -1, sizeof(temp));
for (int i = 0; i < n; ++i) {
gets(Map[i]);
for (int j = 0; j < m; ++j) {
if (Map[i][j] == 'A' || Map[i][j] == 'S') {
temp[i][j] = tot++;
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (temp[i][j] != -1) {
BFS(i, j);
}
}
}
}
for (int j = 0; j < n; ++j) {
if (temp[i][j] != -1) {
BFS(i, j);
}
}
}
}
int Prim(int n) {
int dis[Max], ans = 0;
memset(vis, 0 ,sizeof(vis));
vis[0] = 1;
for (int i = 1; i < n; ++i) {
dis[i] = c[0][i];
}
int dis[Max], ans = 0;
memset(vis, 0 ,sizeof(vis));
vis[0] = 1;
for (int i = 1; i < n; ++i) {
dis[i] = c[0][i];
}
for (int i = 1; i < n; ++i) {
int mark, Min = inf;
for (int j = 0; j < n; ++j) {
if (!vis[j] && dis[j] < Min) {
Min = dis[mark = j];
}
}
if (Min == inf) return -1;
vis[mark] = 1;
ans += Min;
for (int j = 0; j < n; ++j) {
if (!vis[j] && dis[j] > c[mark][j]) {
dis[j] = c[mark][j];
}
}
}
return ans;
}
int mark, Min = inf;
for (int j = 0; j < n; ++j) {
if (!vis[j] && dis[j] < Min) {
Min = dis[mark = j];
}
}
if (Min == inf) return -1;
vis[mark] = 1;
ans += Min;
for (int j = 0; j < n; ++j) {
if (!vis[j] && dis[j] > c[mark][j]) {
dis[j] = c[mark][j];
}
}
}
return ans;
}
int main() {
// definition
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &m, &n);
gets(Map[0]);
Init();
printf("%d\n", Prim(tot));
}
return 0;
}
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &m, &n);
gets(Map[0]);
Init();
printf("%d\n", Prim(tot));
}
return 0;
}