HDU4021 24 Puzzle The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest

24 Puzzle

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2 Accepted Submission(s): 1


Problem Description
Daniel likes to play a special board game, called 24 puzzle. 24 puzzle is such a game that there are tiles with the number 1 to 23 in a play board like the follow picture:

The ‘#’ denotes the positions that the tiles may be placed on. There are 24 possible positions in total, so one of them is not occupied by the tile. We can denote the empty position by zero.
Daniel could move the tiles to the empty position if the tile is on the top, bottom, left or right of the empty position. In this way Daniel can reorder the tiles on the board.
Usually he plays with this game by setting up a target states initially, and then trying to do a series of moves to achieve the target. Soon he finds that not all target states could be achieved.
He asks for your help, to determine whether he has set up an impossible target or not.

Input
The first line of input contains an integer denoting the number of test cases.
For each test case, the first line contains 24 integers denoting the initial states of the game board. The numbers are the describing the tiles from top to bottom, left to right. And the empty position is indicated by zero. You can assume that the number of each tile are different, and there must be exactly one empty position. The second line of test case also contains 24 integers denoting the target states.

Output
For each test case, if the target is impossible to achieve, output ‘Y’ in a single line, otherwise, output ‘N’.


Sample Input
2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
3 1 2 0 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
3 0 2 1 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Sample Output
N
Y

Source

Recommend
lcy


 

外围的8个格子是没有办法移动出来的,所以先把外围的0移动到中间去,再判断两个局面外围8个格子是否相同,

相同继续判断,中间就是15数码的判断,根据0所在行差值的奇偶性判断两个局面逆序数相同或者不同。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int t,n,m;
int sp[50],pos[50],can[10][10],dir[4][2]={0,1,0,-1,1,0,-1,0};
void init()
{
	int i,j,k=0;
	for(i=1;i<=4;i++)
		for(j=1;j<=4;j++)
			can[i][j]=1;
	can[0][1]=2;
	can[0][4]=2;
	can[1][0]=2;
	can[1][5]=2;
	can[4][0]=2;
	can[4][5]=2;
	can[5][1]=2;
	can[5][4]=2;
	for(i=0;i<6;i++)
		for(j=0;j<6;j++)
			if(can[i][j]==2)
				sp[k++]=i*6+j;
	for(i=k=0;i<6;i++)
		for(j=0;j<6;j++)
			if(can[i][j])
				pos[k++]=i*6+j;
}
int fun(char *a)
{
	int i,j,s=0;
	for(i=0;i<16;i++)
		for(j=i+1;j<16;j++)
			if(a[j]&&a[i]>a[j])
				s++;
	return s&1;
}
int ok(char a[6][6],char b[6][6])
{
	for(int i=0;i<8;i++)
		if(a[sp[i]/6][sp[i]%6]!=b[sp[i]/6][sp[i]%6])
			return 1;
	return 0;
}
int main()
{
	init();
	int i,j,k,p1,p2,r1,r2;
	char a[25],b[25],xa[6][6],xb[6][6];
	scanf("%d",&t);
	while(t--)
	{
		memset(xa,-1,sizeof(xa));
		memset(xb,-1,sizeof(xb));
		for(i=0;i<24;i++)
		{
			scanf("%d",&a[i]);
			xa[pos[i]/6][pos[i]%6]=a[i];
			if(!a[i])
				p1=i;
		}
		for(i=0;i<24;i++)
		{
			scanf("%d",&b[i]);
			xb[pos[i]/6][pos[i]%6]=b[i];
			if(!b[i])
				p2=i;
		}
		int x,y,xx,yy;
		x=pos[p1]/6;
		y=pos[p1]%6;
		if(can[x][y]==2)
			for(i=0;i<4;i++)
			{
				xx=x+dir[i][0];
				yy=y+dir[i][1];
				if(xx>=0&&yy>=0&&xx<6&&yy<6&&can[xx][yy])
				{
					swap(xa[x][y],xa[xx][yy]);
					break;
				}
			}
		x=pos[p2]/6;
		y=pos[p2]%6;
		if(can[x][y]==2)
			for(i=0;i<4;i++)
			{
				xx=x+dir[i][0];
				yy=y+dir[i][1];
				if(xx>=0&&yy>=0&&xx<6&&yy<6&&can[xx][yy])
				{
					swap(xb[x][y],xb[xx][yy]);
					break;
				}
			}
		if(ok(xa,xb))
		{
			puts("Y");
			continue;
		}
		for(k=0,i=1;i<=4;i++)
			for(j=1;j<=4;j++)
				a[k++]=xa[i][j];
		for(k=0,i=1;i<=4;i++)
			for(j=1;j<=4;j++)
				b[k++]=xb[i][j];
		for(i=0;i<16;i++)
			if(!a[i])
			{
				r1=i/4;
				break;
			}
		for(i=0;i<16;i++)
			if(!b[i])
			{
				r2=i/4;
				break;
			}
		if(fun(a)==fun(b))
			puts(abs(r1-r2)%2?"Y":"N");
		else
			puts(abs(r1-r2)%2?"N":"Y");
	}
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值