[HDU][4021][24 Puzzle]

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4021

先确保两个表格的0都移到4*4内(而不是在八个顶点之一上),输出N的条件是:

  1.八个顶点完全一样。

  2.内部的4*4可以拼成。

参考:http://topic.csdn.net/u/20081019/22/719a3ab7-359d-4d23-8677-07ec0d7b2289.html

View Code
#include <stdio.h>
#include
<math.h>
#include
<algorithm>

using namespace std;

int comp(int a[], int b[])
{
int v[8] = {0,1,2,7,16,21,22,23};
for (int i=0; i<8; i++)
{
if (a[v[i]] != b[v[i]])
{
return false;
}
}
return true;
}

void move_zero(int a[])
{
int v[8] = {0,1,2,7,16,21,22,23};
int t[8] = {3,6,3,6,17,20,17,20};
for (int i=0; i<8; i++)
{
if (a[v[i]]==0)
{
a[v[i]]
= a[t[i]];
a[t[i]]
= 0;
}
}
}

int main(void)
{
//freopen("D:/a.txt", "r", stdin);
int ncase;
scanf(
"%d", &ncase);
for (int x=0; x<ncase; x++)
{
int source[24], target[24];
for (int i=0; i<24; i++)
{
scanf(
"%d", &source[i]);
}
for (int i=0; i<24; i++)
{
scanf(
"%d", &target[i]);
}
move_zero(source);
move_zero(target);
if (!comp(source, target))
{
printf(
"Y\n");
continue;
}
int vt[] = {3,4,5,6,8,9,10,11,12,13,14,15,17,18,19,20};
int s[16], t[16];
for (int i=0; i<16; i++)
{
s[i]
= source[vt[i]];
t[i]
= target[vt[i]];
}
int inver_s = 0, inver_t = 0, z_s, z_t;
for (int i=0; i<16; i++)
{
for (int j=0; j<16; j++)
{
if (s[i] > s[j] && i < j) inver_s++;
if (t[i] > t[j] && i < j) inver_t++;
if (s[i*4+j] == 0 && i < 4 && j < 4) z_s = i + j;
if (t[i*4+j] == 0 && i < 4 && j < 4) z_t = i + j;
}
}
//printf("%d %d %d %d\n", inver_s, inver_t, z_s, z_t);
int sum = inver_s + inver_t + z_s +z_t;
printf(
"%c\n", sum%2?'Y':'N');
}
return 0;
}

你可能感兴趣的:(HDU)