Description
On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.
Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a ‘.’ means an empty space, an ‘H’ represents a house on that point, and am ‘m’ indicates there is a little man on that point.
You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.
Input
There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of ‘H’s and ‘m’s on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.
Output
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
Sample Input
2 2 .m H. 5 5 HH..m ..... ..... ..... mm..H 7 8 ...H.... ...H.... ...H.... mmmHmmmm ...H.... ...H.... ...H.... 0 0
Sample Output
2 10 28
Solution below . . .
/* * The Hungarian algorithm (https://en.wikipedia.org/wiki/Hungarian_algorithm) * can be used to solve assignment problems. Given an NxN matrix, where the * element in the i-th row and j-th column represents the cost of assigning * the i-th house to the j-th man, the algorithm finds an assignment of men * to houses with minimum cost. */ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { // a[1..n][1..m] >= 0, n <= m public static int solveAssignmentProblem(int[][] a) { int n = a.length - 1; int m = a[0].length - 1; int[] u = new int[n + 1]; int[] v = new int[m + 1]; int[] p = new int[m + 1]; int[] way = new int[m + 1]; for (int i = 1; i <= n; ++i) { p[0] = i; int j0 = 0; int[] minv = new int[m + 1]; Arrays.fill(minv, Integer.MAX_VALUE); boolean[] used = new boolean[m + 1]; do { used[j0] = true; int i0 = p[j0]; int delta = Integer.MAX_VALUE; int j1 = 0; for (int j = 1; j <= m; ++j) if (!used[j]) { int cur = a[i0][j] - u[i0] - v[j]; if (cur < minv[j]) { minv[j] = cur; way[j] = j0; } if (minv[j] < delta) { delta = minv[j]; j1 = j; } } for (int j = 0; j <= m; ++j) if (used[j]) { u[p[j]] += delta; v[j] -= delta; } else minv[j] -= delta; j0 = j1; } while (p[j0] != 0); do { int j1 = way[j0]; p[j0] = p[j1]; j0 = j1; } while (j0 != 0); } return -v[0]; } public static void main(String[] args) { List<Pair<Integer, Integer>> men; List<Pair<Integer, Integer>> houses; Pair<Integer, Integer> p; Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int M = sc.nextInt(); while (N != 0) { men = new ArrayList<Pair<Integer, Integer>>(); houses = new ArrayList<Pair<Integer, Integer>>(); for (int i = 0; i < N; i++) { String s = sc.next(); for (int j = 0; j < M; j++) { char c = s.charAt(j); if (c == 'm') { p = new Pair<Integer, Integer>(i, j); men.add(p); } if (c == 'H') { p = new Pair<Integer, Integer>(i, j); houses.add(p); } } } int count = houses.size(); int[][] a = new int[count + 1][count + 1]; for (int i = 1; i <= count; i++) { Pair<Integer, Integer> p1 = houses.get(i - 1); for (int j = 1; j <= count; j++) { Pair<Integer, Integer> p2 = men.get(j - 1); a[i][j] = distance(p1, p2); } } System.out.println(solveAssignmentProblem(a)); N = sc.nextInt(); M = sc.nextInt(); } sc.close(); } static int distance(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) { return Math.abs(p1.e1 - p2.e1) + Math.abs(p1.e2 - p2.e2); } } class Pair<S, T> { final S e1; final T e2; Pair(final S e1, final T e2) { this.e1 = e1; this.e2 = e2; } }