HDU 4021 八数码变形

/********************************************************************************
    水题,先把起始状态的0和终止状态的0全移到里面的4 * 4矩阵中,接着先判一下多出来的那几个角上的
元素是否有不同的,如果有不同的显然无解,否则,再用求逆序数+0的曼哈顿距离mod2判断是否有解
********************************************************************************/
#include <iostream>
#include <functional>
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;

#define lowbit(x) ( (x) & ( (x) ^ (x - 1) ) )

//typedef long long LL;		//GMU C++
//typedef unsigned long long ULL;
typedef __int64 LL;		//Visual C++ 2010
typedef unsigned __int64 ULL;
typedef double LF;

typedef pair<int, int> PII;
typedef pair<double, double> PDD;

typedef map<int, int>::iterator MI;
typedef vector<int>::iterator VI;
typedef list<LL>::iterator LI;
typedef set<int>::iterator SI;

const int INF_INT = 0x3f3f3f3f;
const LL INF_LL = 0x7fffffffffffffffLL;
const double oo = 10e9;
const double eps = 10e-7;
const double PI = acos(-1.0);

const int MAXN = 24;
const int key_pos[] = {0, 1, 2, 7, 16, 21, 22, 23};
const int ans_pos[] = {3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20};

void change(int *maze, int zpos)
{
	switch(zpos)
	{
	case 0:
		swap(maze[0], maze[3]);
		break ;

	case 1:
		swap(maze[1], maze[6]);
		break ;
	
	case 2:
		swap(maze[2], maze[3]);
		break ;

	case 7:
		swap(maze[7], maze[6]);
		break ;

	case 16:
		swap(maze[16], maze[17]);
		break ;

	case 21:
		swap(maze[21], maze[20]);
		break ;

	case 22:
		swap(maze[22], maze[17]);
		break ;
			
	case 23:
		swap(maze[23], maze[20]);
		break ;

	default:
		break ;
	}
	return ;
}
bool check(int *src, int *tar)
{
	for (int i = 0; i < 8; ++i)
	{
		if (src[key_pos[i]] != tar[key_pos[i]])
		{
			return false;
		}
	}
	return true;
}
int reverse(int *maze)
{
	int res = 0;
	for (int i = 1; i < 16; ++i)
	{
		for (int j = 0; j < i; ++j)
		{
			res += (maze[i] < maze[j]);
		}
	}
	return res;
}
void ace()
{
	int test, src[MAXN], tar[MAXN], beg[MAXN], end[MAXN];
	int zpos, kpos[2], ans[2];
	for (scanf("%d", &test); test--; )
	{
		for (int i = 0; i < MAXN; ++i)
		{
			scanf("%d", src + i);
			if (0 == src[i])
			{
				zpos = i;
			}
		}
		change(src, zpos);
		for (int i = 0; i < MAXN; ++i)
		{
			scanf("%d", tar + i);
			if (0 == tar[i])
			{
				zpos = i;
			}
		}
		change(tar, zpos);
		if (!check(src, tar))
		{
			puts("Y");
			continue ;
		}
		for (int i = 0; i < 16; ++i)
		{
			beg[i] = src[ans_pos[i]];
			if (0 == beg[i])
			{
				kpos[0] = i;
			}
			end[i] = tar[ans_pos[i]];
			if (0 == end[i])
			{
				kpos[1] = i;
			}
		}
		ans[0] = reverse(beg) + abs(kpos[0] / 4 - kpos[1] / 4) + abs(kpos[0] % 4 - kpos[1] % 4);
		ans[1] = reverse(end);
		if ((ans[0] & 0x1) != (ans[1] & 0x1))
		{
			puts("Y");
		}
		else
		{
			puts("N");
		}
	}
	return ;
}
int main()
{
	ace();
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值