(beginer) 最小权匹配 UVA 1045 - The Great Wall Game

Hua and Shen have invented a simple solitaire board game that they call ``The Great Wall Game." The game is played with n stones on an n×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 all n stones in a straight line either horizontally, vertically, or diagonally using the fewest number of moves. An example for the case n = 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 integer n, 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 the n 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.

题意:有个n个石子放在一个nxn的板子上面,然后你能上下左右的移动这些石子,然后问最少要移多少步才能让这些石子在一条直线上(横的或竖的或对角线)。

思路:其实我不知道是不是可以直接的,我用了IDAstar,保险的做法而已,估价函数其实就是最小权匹配,可能答案直接输出这个估价函数就行了。先算出石子到每个位置的距离(直接横坐标的差+纵坐标的差),然后把每种“直线“的情况都匹配一下,假设显示第一行,那么我们就把二分图的左边是n个石子,右边是n个这一行的格子,权值是距离,然后套一下KM算法,最后所有情况取最小的。注意:由于要求最小值,而KM算法是求最大全匹配的,所以我们的距离取负数就行了。

代码:
#include<iostream>
#include<queue>
#include<cstring>
#include<math.h>
#include<string.h>
#include<vector>
#include<cstdio>
using namespace std;
const int inf = 1e8;
const int maxn = 17;
int n;
int d[maxn][maxn];
int w[maxn][maxn][maxn];
int ww[maxn][maxn];
int lx[maxn] , ly[maxn] , link[maxn] , slack;
bool S[maxn] , T[maxn];

inline int row(int x) { return x / n; }
inline int column(int x) { return x % n; }
inline int getx(int r,int c) { return r*n+c; }
inline int inRange(int r,int c)  { return 0<=r&&r<n && 0<=c&&c<n; }
int Move[2][4] = { {-1,0,1,0} , {0,1,0,-1} };

struct State
{
	int g , h;
	vector<int> stone;
	bool ocpy[maxn][maxn];
}start;

bool match(int x)
{
	S[x] = true;
	for (int y = 0 ; y < n ; ++y) {
		if (T[y]) continue;
		if (lx[x]+ly[y]==ww[x][y]) {
			T[y] = true;
			if (link[y]==-1 || match(link[y])) {
				link[y] = x;
				return true;
			}
		} else slack = min(slack,lx[x]+ly[y]-ww[x][y]);
	}
	return false;
}

int KM()
{
	memset(link,-1,sizeof(link));
	memset(ly,0,sizeof(ly));
	for (int i = 0 ; i < n ; ++i) {
		while (true) {
			for (int j = 0 ; j < n ; ++j) S[j] = T[j] = false;
			slack = inf;
			if (match(i)) break;
			for (int j = 0 ; j < n ; ++j) 
			{
				if (S[j]) lx[j] -= slack;
				if (T[j]) ly[j] += slack;
			}
		}
	}
	int ret = 0;
	for (int i = 0 ; i < n ; ++i)
		ret += ww[link[i]][i];
	return -ret;
}

int H(const State & st)
{
	for (int i = 0 ; i < n ; ++i) 
	{
		int stone = st.stone[i];
		for (int r = 0 ; r < n ; ++r)
			for (int c = 0 ; c < n ; ++c)
				w[i][r][c] = -abs(row(stone)-r)-abs(column(stone)-c);
	}
	int ret = inf;
	for (int r = 0 ; r < n ; ++r) {
		for (int i = 0 ; i < n ; ++i) lx[i] = -inf;
		for (int c = 0 ; c < n ; ++c) {
			for (int i = 0 ; i < n ; ++i)
			{
				lx[i] = max(lx[i],w[i][r][c]);
				ww[i][c] = w[i][r][c];
			}
		}
		ret = min(ret,KM());
	}
	if (ret==0) return ret;
	for (int c = 0 ; c < n ; ++c) {
		for (int i = 0 ; i < n ; ++i) lx[i] = -inf;
		for (int r = 0 ; r < n ; ++r) {
			for (int i = 0 ; i < n ; ++i) 
			{
				ww[i][r] = w[i][r][c];
				lx[i] = max(lx[i],w[i][r][c]);
			}
		}
		ret = min(ret,KM());
	}
	if (ret==0) return ret;
	for (int i = 0 ; i < n ; ++i) lx[i] = -inf;
	for (int r = 0 ; r < n ; ++r) {
		for (int i = 0 ; i < n ; ++i) {
			ww[i][r] = w[i][r][r];
			lx[i] = max(lx[i],w[i][r][r]);
		}
	}
	ret = min(ret,KM());
	if (ret==0) return ret;
	for (int i = 0 ; i < n ; ++i) lx[i] = -inf;
	for (int r = 0 ; r < n ; ++r) {
		for (int i = 0 ; i < n ; ++i) {
			ww[i][r] = w[i][r][n-1-r];
			lx[i] = max(lx[i],w[i][r][n-1-r]);
		}
	}
	ret = min(ret,KM());
	return ret;
}

bool dfs(const State & st, int maxdep)
{
	if (st.g+st.h>maxdep) return false;
	if (st.h==0) {
		printf("%d moves required.\n\n",st.g);
		return true;
	}
	for (int i = 0 ; i < n ; ++i)
	{
		int stone = st.stone[i];
		int r = row(stone) , c = column(stone);
		for (int j = 0 ; j < 4 ; ++j)
		{
			int rr = r+Move[0][j];
			int cc = c+Move[1][j];
			if (!inRange(rr,cc) || st.ocpy[rr][cc]) continue;
			State now = st;
			now.stone[i] = getx(rr,cc);
			now.ocpy[r][c] = false;
			now.ocpy[rr][cc] = true;
			++now.g;
			now.h = H(now);
			if (dfs(now,maxdep)) return true;
		}
	}
	return false;
}

void IDAStar()
{
	start.g = 0;
	start.h = H(start);
	int maxdep = start.h-1;
	while (true) {
		++maxdep;
		if (dfs(start,maxdep)) break;
	}
}

void input()
{
	int r , c;
	start.stone.clear();
	for (int i = 0 ; i < n ; ++i)
	{
		scanf("%d%d",&r,&c);
		--r , --c;
		memset(start.ocpy,0,sizeof(start.ocpy));
		start.ocpy[r][c] = true;
		start.stone.push_back(getx(r,c));
	}
}

int main()
{
	int k = 0;
	while (scanf("%d",&n)==1,n)
	{
		input();
		++k;
		printf("Board %d: ",k);
		IDAStar();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值