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 alln 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 n1$ \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.





#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int MaxN=15;
int n,ans,cases;
int a[MaxN+1][MaxN+1],Lx[MaxN],Ly[MaxN+1],matchY[MaxN+1];
bool usedX[MaxN+1],usedY[MaxN+1];

bool path(int r)
{
	int i;
	usedX[r]=true;
	for(i=0;i<n;++i)
		if(Lx[r]+Ly[i]==a[r][i])
			if(!usedY[i])
			{
				usedY[i]=true;
				if(matchY[i]<0||path(matchY[i]))
				{
					matchY[i]=r;
					return true;
				}
			}
	return false;
}

void work()
{
	int i,j,k,res=0;
	for(i=0;i<n;++i)
	{	
		Lx[i]=-2147483647;
		for(j=0;j<n;++j)
			if(a[i][j]>Lx[i])
				Lx[i]=a[i][j];
	}
	memset(Ly,0,sizeof(matchY));
	memset(matchY,-1,sizeof(matchY));
	for(i=0;i<n;++i)
	for(;;)
	{
		memset(usedX,false,sizeof(usedX));
		memset(usedY,false,sizeof(usedY));
		if(path(i))
			break;
		int delta=2147483647,v;
		for(j=0;j<n;++j)
			if(usedX[j])
				for(k=0;k<n;++k)
					if(!usedY[k])
					{
						v=Lx[j]+Ly[k]+-a[j][k];
						if(v<delta)
							delta=v;
					}
		for(j=0;j<n;++j)
			if(usedX[j])
				Lx[j]-=delta;
		for(j=0;j<n;++j)
			if(usedY[j])
				Ly[j]+=delta;
	}
	for(i=0;i<n;++i)
		if(matchY[i]>=0)
			res+=a[matchY[i]][i];
	if(res>ans)
		ans=res;
}

int dis(int x,int y)
{
	if(x*y>=0)
		x+=y;
	else
		x-=y;
	if(x>0)
		return x;
	else
		return -x;
}

int main()
{
	int i,j,k;
	int P[MaxN+1][2];
	while(1)
	{
		scanf("%d",&n);
		if(n==0)
			break;
		ans=-(n*n+n);
		for(i=1;i<=n;i++)
			scanf("%d %d",&P[i][0],&P[i][1]);
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
				for(k=1;k<=n;k++)
					a[j-1][k-1]=-dis(P[j][0]-i,P[j][1]-k);
			work();
		}
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
				for(k=1;k<=n;k++)
					a[j-1][k-1]=-dis(P[j][0]-k,P[j][1]-i);
			work();
		}
		for(j=1;j<=n;j++)
			for(k=1;k<=n;k++)
				a[j-1][k-1]=-dis(P[j][0]-k,P[j][1]-k);
		work();
		for(j=1;j<=n;j++)
			for(k=1;k<=n;k++)
				a[j-1][k-1]=-dis(P[j][0]-k,P[j][1]-(n-k+1));
		work();
		printf("Board %d: %d moves required.\n\n",++cases,-ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值