hdu 4021 24 Puzzle ( 逆序数判断是否可解 )
24 Puzzle
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.
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.
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
Y
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 30
#define MAXN 20005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 0.000001
typedef long long ll;
using namespace std; int n,m,ans,p1,p2;
int a[maxn],b[maxn];
int mp[maxn]; bool presolve()
{
int i,j;
if(a[0]==0) swap(a[0],a[3]);
if(a[1]==0) swap(a[1],a[6]);
if(a[2]==0) swap(a[2],a[3]);
if(a[7]==0) swap(a[7],a[6]);
if(a[16]==0) swap(a[16],a[17]);
if(a[21]==0) swap(a[21],a[20]);
if(a[22]==0) swap(a[22],a[17]);
if(a[23]==0) swap(a[23],a[20]);
if(b[0]==0) swap(b[0],b[3]);
if(b[1]==0) swap(b[1],b[6]);
if(b[2]==0) swap(b[2],b[3]);
if(b[7]==0) swap(b[7],b[6]);
if(b[16]==0) swap(b[16],b[17]);
if(b[21]==0) swap(b[21],b[20]);
if(b[22]==0) swap(b[22],b[17]);
if(b[23]==0) swap(b[23],b[20]);
for(i=0;i<24;i++)
{
if(a[i]==0) p1=i;
if(b[i]==0) p2=i;
}
if(a[0]!=b[0]||a[1]!=b[1]||a[2]!=b[2]||a[7]!=b[7]||
a[16]!=b[16]||a[21]!=b[21]||a[22]!=b[22]||a[23]!=b[23])
return false ;
return true ;
}
int getstate(int x[])
{
int i,j,t,s,sum=0;
for(i=3;i<24;i++)
{
t=x[i];
if(t==0||i==7||i==16||i==21||i==22||i==23) continue ;
s=0;
for(j=i+1;j<24;j++)
{
if(x[j]==0||j==7||j==16||j==21||j==22||j==23) continue ;
if(x[j]<t) s++;
}
sum+=s;
}
return sum;
}
int main()
{
int i,j,t,s1,s2;
mp[3]=mp[4]=mp[5]=mp[6]=1;
mp[8]=mp[9]=mp[10]=mp[11]=2;
mp[12]=mp[13]=mp[14]=mp[15]=3;
mp[17]=mp[18]=mp[19]=mp[20]=4;
scanf("%d",&t);
while(t--)
{
for(i=0;i<24;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<24;i++)
{
scanf("%d",&b[i]);
}
if(presolve())
{
s1=getstate(a);
s2=getstate(b);
if((s1+s2)&1)
{
if(abs(mp[p1]-mp[p2])&1) printf("N\n");
else printf("Y\n");
}
else
{
if(!(abs(mp[p1]-mp[p2])&1)) printf("N\n");
else printf("Y\n");
}
}
else printf("Y\n");
}
return 0;
}
hdu 4021 24 Puzzle ( 逆序数判断是否可解 )的更多相关文章
- HDU 4911 Inversion (逆序数 归并排序)
Inversion 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/A Description bobo has a sequen ...
- hdu 6048 Puzzle 拼图 逆序数
关于拼图和逆序数的关系可以看看这个 http://www.guokr.com/question/579400/ 然后求逆序数在判断就行了 按题意生成原始排列,观察发现,每一轮数后方比该数小的数的数量( ...
- HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description The inve ...
- HDU 4911 (树状数组+逆序数)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...
- HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- [HDU POJ] 逆序数
HDU 1394 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276 ...
- 【归并排序】【逆序数】HDU 5775 Bubble Sort
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...
- HDU 1394 Minimum Inversion Number (线段树 单点更新 求逆序数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个n个数的序列,当中组成的数仅仅有0-n,我们能够进行这么一种操作:把第一个数移到最 ...
- hdu 1394(线段树) 最小逆序数
http://acm.hdu.edu.cn/showproblem.php?pid=1394 给出一列数组,数组里的数都是从0到n-1的,在依次把第一个数放到最后一位的过程中求最小的逆序数 线段树的应 ...
随机推荐
- Python进阶:@property 动态属性
Python进阶:@property 动态属性 Python 动态属性的概念可能会被面试问到,在项目当中也非常实用,但是在一般的编程教程中不会提到,可以进修一下. 先看一个简单的例子.创建一个 Stu ...
- 洛谷——P1927 防护伞
P1927 防护伞 题目描述 据说 2012 的灾难和太阳黑子的爆发有关.于是地球防卫小队决定制造一个特殊防护 伞,挡住太阳黑子爆发的区域,减少其对地球的影响.由于太阳相对于地球来说实在是太 大了,我 ...
- UGUI的优点新UI系统三效率高效果好
UGUI的优点新UI系统三效率高效果好 通过对批处理(batching).纹理图集(texture atlasing)和新的canvas组件的支持,新UI系统提供了一个经过优化的解决方案,使得开发者添 ...
- python itertools的使用(转)
1. chain的使用 import itertools listone = ['a','b','c'] listtwo = ['11','22','abc'] for item in itertoo ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- Eden的退役记
好久没更博客了, 这篇随笔不同于之前的学术性随笔.游记,只是来发泄一下自己的情感,回忆一下自己的OI经历…… 五年的OI生涯结束了 初一:懵懂的我刚接触了OI,被其功能吸引.由于运气好过了初赛,然而复 ...
- 【数论】【中国剩余定理】【LCM】hdu1788 Chinese remainder theorem again
根据题目容易得到N%Mi=Mi-a. 那么可得N%Mi+a=Mi. 两侧同时对Mi取余,可得(N+a)%Mi=0. 将N+a看成一个变量,就可以把原问题转化成求Mi的LCM,最后减去a即可. #inc ...
- [BZOJ5046]分糖果游戏
题目大意: 有a,b两个人分糖,每个人都有一个能量值. 每个人每一轮可以选择进行两种操作: 1.取走最左边的糖果,补充相应的能量值并获取相应的美味度. 2.跳过这一轮,能量值-1. 问在每个人都采取最 ...
- 8VC Venture Cup 2016 - Final Round C. Package Delivery 优先队列
C. Package Delivery 题目连接: http://www.codeforces.com/contest/627/problem/C Description Johnny drives ...
- while an existing transition or presentation is occurring; the navigation stack will not be updated
使用UIAlertController提示信息,在之后使用navigation进行逻辑跳转时,出现popToViewController:transition: called on <UINav ...