Going Home

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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0`

Sample Output

`21028`

#include <stdio.h>
#define maxn 200
#define inf (1 << 29)
#define Min(a, b) a < b ? a : b
#define Max(a, b) a > b ? a : b
struct T
{

int v, next;
}fn[maxn
* maxn];
struct P
{

int x, y;
}H[maxn], M[maxn];
char s[maxn];
int link[maxn], alink[maxn], lx[maxn], ly[maxn], sx[maxn], sy[maxn], g[maxn], map[maxn][maxn];
int n, m, slack;
int fsb(int a)
{

return a > 0 ? a : -a;
}
int dfs(int u)
{

int v, tmp;
sx[u]
= 1;

for (v = 0; v < m; v++)
{

if (!sy[v])
{
tmp
= lx[u] + ly[v] - map[u][v];

if (tmp == 0)
{
sy[v]
= 1;

{
= u;

return 1;
}
}

else
{
slack
= Min(slack, tmp);
}
}
}

return 0;
}
int KM()
{

int i, j, sum;

for (i = 0; i < n; i++)
{

for (j = 0, lx[i] = 0; j < m; j++)
{
lx[i]
= Max(lx[i], map[i][j]);
}
}

for (i = 0; i < m; i++)
{
ly[i]
= 0;
= -1;
}

for (i = 0; i < n; i++)
{

while (1)
{

for (j = 0; j < n; j++)
{
sx[j]
= 0;
}

for (j = 0; j < m; j++)
{
sy[j]
= 0;
}
slack
= inf;

if (dfs(i))
{

break;
}

for (j = 0; j < n; j++)
{

if (sx[j])
{
lx[j]
-= slack;
}
}

for (j = 0; j < m; j++)
{

if (sy[j])
{
ly[j]
+= slack;
}
}
}
}
sum
= 0;

for (i = 0; i < m; i++)
{

if (link[i] != -1)
{
sum
}
}

return sum;
}
int main()
{

int i, j, num1, num2;

while (scanf("%d%d"&n, &m), n || m)
{

for (i = 1, num1 = num2 = 0; i <= n; i++)
{
scanf(
"%s", s);

for (j = 0; s[j]; j++)
{

if (s[j] == 'H')
{
H[num1].x
= i, H[num1].y = j, num1++;
}

else if (s[j] == 'm')
{
M[num2].x
= i, M[num2].y = j, num2++
}
}
}
m
= num1, n = num2;

for (i = 0; i < n; i++)
{

for (j = 0; j < m; j++)
{
map[i][j]
= -1 * (fsb(M[i].x - H[j].x) + fsb(M[i].y- H[j].y));
}
}
printf(
"%d\n"-KM());
}

return 0;
}