uva 1045 - The Great Wall Game(KM)

来源:互联网 发布:图像算法笔试题 编辑:程序博客网 时间:2024/06/15 15:49

Hua and Shen have invented a simple solitaire board game that they call ``The Great Wall Game." The game is played withn stones on ann×n grid. The stones are placed at random in the squares of the grid, at most one stone per square. In a single move, any single stone can move into an unoccupied location one unit horizontally or vertically in the grid. The goal of the puzzle is to create a ``wall," i.e., to line up alln stones in a straight line either horizontally, vertically, or diagonally using the fewest number of moves. An example for the casen = 5 is shown in Figure 1(a). In Figure 1(b) it is shown that with six moves we can line all the stones up diagonally. No smaller number of moves suffices to create a line of five stones. (However, there are other solutions using six moves, e.g., we can line up all five stones in the third column using six moves.)

\epsfbox{p3276.eps}

Figure 1. Starting board (a) and a 6-move solution (b) for n = 5

There is just one problem - Hua and Shen have no idea what the minimum number of moves is for any given starting board. They would like you to write a program that can take any starting configuration and determine the fewest number of moves needed to create a wall.

Input

The input consists of multiple cases. Each case begins with a line containing an integern,1$ \le$n$ \le$15. The next line contains the row and column numbers of the first stone, followed by the row and column numbers of the second stone, and so on. Rows and columns are numbered as in the above diagram. The input data for the last case will be followed by a line containing a single zero.

Output

For each input case, display the case number (1, 2,...) followed by the minimum number of moves needed to line up then stones into a straight-line wall. Follow the format shown in the sample output. Print a blank line after each case.

Sample Input

5                                                                1 2 2 4 3 4 5 1 5 3                                             2                                                                1 1 1 2                                                         3                                                                3 1 1 2 2 2                                                       0

Sample Output

Board 1: 6 moves required.  Board 2: 0 moves required.  Board 3: 1 moves required.

 

要求最优匹配的最小值。

处理方法,把所有边的值取反,然后就是枚举所有情况,套模板的事了

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 15 + 5;const int INF = 1000000000;int W[maxn][maxn], n;int Lx[maxn], Ly[maxn]; // 顶标int left[maxn];         // left[i]为右边第i个点的匹配点编号(即和左边哪个点匹配)bool S[maxn], T[maxn];   // S[i]和T[i]为左/右第i个点是否已标记bool match(int i){  S[i] = true;  for(int j = 1; j <= n; j++) if (Lx[i]+Ly[j] == W[i][j] && !T[j]){    T[j] = true;    if (!left[j] || match(left[j])){      left[j] = i;      return true;    }  }  return false;}void update(){  int a = INF;  for(int i = 1; i <= n; i++) if(S[i])    for(int j = 1; j <= n; j++) if(!T[j])      a = min(a, Lx[i]+Ly[j] - W[i][j]);  for(int i = 1; i <= n; i++) {    if(S[i]) Lx[i] -= a;    if(T[i]) Ly[i] += a;  }}void KM() {  for(int i = 1; i <= n; i++) {    left[i] = Lx[i] = Ly[i] = 0;    for(int j = 1; j <= n; j++)      Lx[i] = max(Lx[i], W[i][j]);  }  for(int i = 1; i <= n; i++) {    for(;;) {      for(int j = 1; j <= n; j++) S[j] = T[j] = false;      if(match(i)) break; else update();    }  }}int abs(int x){    return x < 0?-x:x;}int orix[maxn],oriy[maxn];int main(){    int ans,sum,kase = 0;    while(scanf("%d",&n)){        if(n == 0)  break;        kase++;ans = INF;        for(int i = 1;i <= n;i++)            scanf("%d%d",&orix[i],&oriy[i]);        for(int i = 1;i <= n;i++){            sum = 0;            for(int j = 1;j <= n;j++){                for(int k = 1;k <= n;k++){                    int dist = abs(orix[j]-i)+abs(oriy[j]-k);                    W[j][k] = -dist;//把边权建为负的                }            }            KM();            for(int u= 1;u <= n;u++)                sum += -W[left[u]][u];            ans = min(ans,sum);        }        for(int i = 1;i <= n;i++){            sum = 0;            for(int j = 1;j <= n;j++){                for(int k = 1;k <= n;k++){                    int dist = abs(oriy[j]-i)+abs(orix[j]-k);                    W[j][k] = -dist;                }            }            KM();            for(int u = 1;u <= n;u++){                sum += -W[left[u]][u];            }            ans = min(ans,sum);        }        for(int i = 1;i <= n;i++){            for(int j = 1;j <= n;j++){                int dist = abs(orix[i]-j)+abs(oriy[i]-j);                W[i][j] = -dist;            }        }        KM();sum = 0;        for(int u = 1;u <= n;u++){            sum += -W[left[u]][u];        }        ans = min(ans,sum);        for(int i = 1;i <= n;i++){            for(int j = 1;j <= n;j++){                int dist = abs(orix[i]-(n-j+1))+abs(oriy[i]-j);                W[i][j] = -dist;            }        }        KM();sum = 0;        for(int u = 1;u <= n;u++){            sum += -W[left[u]][u];        }        ans = min(ans,sum);        printf("Board %d: %d moves required.\n\n",kase,ans);    }    return 0;}