POJ3026(最小生成树)
Borg Maze
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
题意
在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度。
思路
将每一个字母看成一个点。如果可以知道每两个点之间的距离。那么直接用Prim算法活着Kruskal算法求最小生成树即可。求每个点到其他个点的距离,只需对每一个点进行一次DFS或BFS即可。
代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BorgMaze {
private static int max = Integer.MAX_VALUE;
static int[][] move = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
static class Point {
private int x;
private int y;
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void setX(int x) {
this.x = x;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
for (int i = 0; i < count; i++) {
int n = sc.nextInt();
int m = sc.nextInt();
int[][] map = new int[m][n];//地图
List<Point> points = new ArrayList<Point>();
sc.nextLine();
for (int j = 0; j < m; j++) {
String str = sc.nextLine();
for (int k = 0; k < n; k++) {
char c = str.charAt(k);
if (c == 'S' || c == 'A') {
Point point = new Point();
point.setX(j);
point.setY(k);
points.add(point);
}
map[j][k] = c;
}
}
int[][] graph = new int[points.size()][points.size()];//地图里A和S构成点图的邻接矩阵
for (int j = 0; j < points.size(); j++) {
int dis[][] = bfs(map, points.get(j));//点points.get(j)到各个点的距离
int index = 0;
for (int k = 0; k < dis.length; k++) {
for (int l = 0; l < dis[k].length; l++) {
if (dis[k][l] != max && map[k][l] != ' ') {
graph[j][index++] = dis[k][l];
}
}
}
}
int result = prim(graph,0);
System.out.println(result);
}
}
public static int[][] bfs(int[][] map, Point start) {
int[][] dis = new int[map.length][map[0].length];
int[][] vist = new int[map.length][map[0].length];
for (int i = 0; i < dis.length; i++) {
for (int j = 0; j < dis[i].length; j++) {
dis[i][j] = max;
vist[i][j] = 0;
}
}
List<Point> queue = new ArrayList<Point>();
queue.add(start);
vist[start.getX()][start.getY()] = 1;
dis[start.getX()][start.getY()] = 0;
int front = 1;
int rear = 0;
int round = 1;//轮次,代表距离
int flag = front;
while (rear < front) {
Point point = queue.get(rear++);//出队
for (int i = 0; i < move.length; i++) {
if (point.getX() + move[i][0] >= 0 && point.getX() + move[i][0] < map.length//x在地图范围内
&& point.getY() + move[i][1] >= 0 && point.getY() + move[i][1] < map[0].length//y在地图范围内
&& map[point.getX() + move[i][0]][point.getY() + move[i][1]] != '#'//不是墙壁
&& vist[point.getX() + move[i][0]][point.getY() + move[i][1]] == 0) {//未被访问
vist[point.getX() + move[i][0]][point.getY() + move[i][1]] = 1;
Point tmp = new Point();
tmp.setX(point.getX() + move[i][0]);
tmp.setY(point.getY() + move[i][1]);
queue.add(tmp);
front++;
dis[point.getX() + move[i][0]][point.getY() + move[i][1]] = round;
}
}
if (rear == flag) {
round++;
flag = front;
}
}
return dis;
}
public static int prim(int[][] graph, int startPoint) {
int result = 0;
int[] lowcost = new int[graph.length];
int[] closest = new int[graph.length];
int minDis = max;
for (int i = 0; i < lowcost.length; i++) {//lowcost和closest的初始值
lowcost[i] = graph[startPoint][i];
closest[i] = startPoint;
}
for (int i = 0; i < lowcost.length - 1; i++) {//除了startPoint之外的n-1个点
minDis = max;
int index = startPoint;
for (int j = 0; j < lowcost.length; j++) {
if (lowcost[j] != 0 && lowcost[j] < minDis) {
minDis = lowcost[j];
index = j;
}
}
result = result + minDis;
lowcost[index] = 0;
for (int j = 0; j < lowcost.length; j++) {//更新lowcost和closest
if (graph[index][j] != 0 && graph[index][j] < lowcost[j]) {
lowcost[j] = graph[index][j];
closest[j] = index;
}
}
}
return result;
}
}