首页 > 数据结构 > Hash表 > POJ 3026 Borg Maze [解题报告] Java
2013
11-12

POJ 3026 Borg Maze [解题报告] Java

Borg Maze

问题描述 :

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.

输入:

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

解题代码:

import java.io.PrintWriter;  
 import java.util.LinkedList;  
 import java.util.Queue;  
 import java.util.Scanner;  
    
public class Main {  
  static class Point{  
  int x,y,step;  
   int result = 1;  
   char c;  
   public Point(int x, int y, char c){  
   this.x = x;  
    this.y = y;  
  this.c = c;  
    step = 1;  
  }  
  public Point(int x, int y,int step, char c){  
    this.x = x;  
   this.y = y;  
    this.c = c;  
    this.step = step;  
  }  
   @Override 
   public int hashCode() {  
    if(result != 1){  
     return result;  
    }  
    final int prime = 31;  
    result = 1;  
    result = prime * result + c;  
    result = prime * result + x;  
    result = prime * result + y;  
    return result;  
   }  
   @Override 
   public boolean equals(Object obj) {  
    if (this == obj)  
     return true;  
    Point other = (Point) obj;  
    if (c != other.c)  
     return false;  
    if (x != other.x)  
     return false;  
    if (y != other.y)  
     return false;  
    return true;  
   }  
      
  }  
  static int[][] all = new int[150][150];  
  static int[] DX = {0,-1,0,1};//e n w s  
  static int[] DY = {-1,0,1,0};  
  public static void main(String[] args) {  
   Scanner scn = new Scanner(System.in);  
  
   PrintWriter out = new PrintWriter(System.out);  

      
   int n = scn.nextInt(),x,y;  
   int num;//S 和 A 的数量  
   char[][] data = new char[101][101];  
  int[][] table = new int[101][101];  
   Point[] points = new Point[101];  
   Point point;  
   String str;  
  while(n-- > 0){  
    x = scn.nextInt();  
    y = scn.nextInt();  
    
    scn.nextLine();  
    for(int i = 0; i < y; i++){  
     str = scn.nextLine();  
     data[i] = str.toCharArray();  
    }  
    //得到顶点数,并将其存入points数组中来  
   num = getNum(data,y,x,points);  
   for(int i = 0; i < num; i++){  
     bfs(data,table,points[i]);  
    }  
    out.println(prim(table, num));  
   }  
   out.flush();  
  }  
 private static void bfs(char[][] data, int[][] table, Point point) {  
   Queue queue = new LinkedList();  
   queue.add(point);  
   int x,y,nx,ny;  
   int px = all[point.x][point.y];  
   int[][] visted = new int[150][150];  
   while(!queue.isEmpty()){  
    point = queue.poll();  
    x = point.x;  
    y = point.y;  
    for(int i = 0; i < 4; i++){  
     nx = x + DX[i];  
    ny = y + DY[i];  
     if(data[nx][ny] == '#'){  
      continue;  
     }  
     if(visted[nx][ny] == 1){  
      continue;  
     }  
     visted[nx][ny] = 1;  
     if(data[nx][ny] == 'A' || data[nx][ny] == 'S'){  
      table[px][all[nx][ny]] = point.step;  
     }  
     queue.add(new Point(nx,ny,point.step + 1,data[nx][ny]));  
    }  
  }  
 }  

     
  /**  
   * 得到 y 和 s 的数量  
   * @param data  
   * @param y  
   * @param x  
   * @return  
   */ 
  private static int getNum(char[][] data, int x, int y,Point[] points) {  
   int num = 0;  
   char c;  
   for(int i = 0; i < x; i++){  
    for(int j = 0; j < y; j++){  
     c = data[i][j];  
     if(c == 'A' || c == 'S'){  
      all[i][j] = num;  
      points[num] = new Point(i,j,c);  
      num++;  
     }  
    }  
   }  
   return num;  
  }  
  public static int prim(int[][] table, int n) {  
    
   int[] lowcost = new int[n];  
   boolean[] s = new boolean[n];  
   int sum = 0;  
   s[0] = true;// 从第一个位置开始  
   for (int i = 1; i < n; i++) {  
    lowcost[i] = table[0][i];  
    s[i] = false;  
   }  
   // 找到 j - s 中的最小权边  
   for (int i = 1; i < n; i++) {  
    int min = Integer.MAX_VALUE;  
    int j = 1;  
   for (int k = 1; k < n; k++) {  
     if ((lowcost[k] < min) && (!s[k])) {  
      min = lowcost[k];  
      j = k;  
     }  
    }  
    s[j] = true;// 加入到j 加入到 s 集合中来  
    // sum += min;  
   for (int k = 1; k < n; k++) {  
     if (table[j][k] < lowcost[k] && (!s[k])) {  
      lowcost[k] = table[j][k];  
     }  
    }  
   }  
      
  for (int i = 0; i < n; i++) {  
    sum += lowcost[i];  
   }  
   return sum;  
  }  
    }

  1. 漂亮。佩服。
    P.S. unsigned 应该去掉。换行符是n 不是/n
    还可以稍微优化一下,
    int main() {
    int m,n,ai,aj,bi,bj,ak,bk;
    while (scanf("%d%d",&m,&n)!=EOF) {
    ai = sqrt(m-1);
    bi = sqrt(n-1);
    aj = (m-ai*ai-1)>>1;
    bj = (n-bi*bi-1)>>1;
    ak = ((ai+1)*(ai+1)-m)>>1;
    bk = ((bi+1)*(bi+1)-n)>>1;
    printf("%dn",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
    }
    }

  2. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
    O(n) = O(n-1) + O(n-2) + ….
    O(n-1) = O(n-2) + O(n-3)+ …
    O(n) – O(n-1) = O(n-1)
    O(n) = 2O(n-1)

  3. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。