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");
	}
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章