HDU 4021 24 Puzzle (拼图)

24 Puzzle

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   |   We have carefully selected several similar problems for you:   4022  4023  4025  4027  4028 
 


题目大意:



给定24个数的位置如图,现在给你24个数,0表示空格,问你是否能由起始位置到终点位置。


解题思路:

首先空格除外,八个角一定是一样的,然后其它的就得满足

(1)如果矩阵列数是奇数,逆序数必须同奇同偶,

(2)如果矩阵列数是偶数,逆序数加上0位置的行数之差必须同奇同偶。


解题代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int a[30],b[30];
int spe[]={0,2,1,7,16,22,23,21};
int oth[]={3,4,5,6,8,9,10,11,12,13,14,15,17,18,19,20};

bool solve(){
    if(a[0]==0) swap(a[0],a[3]);
    if(a[2]==0) swap(a[2],a[3]);
    if(a[1]==0) swap(a[1],a[6]);
    if(a[7]==0) swap(a[7],a[6]);
    if(a[16]==0) swap(a[16],a[17]);
    if(a[22]==0) swap(a[22],a[17]);
    if(a[23]==0) swap(a[23],a[20]);
    if(a[21]==0) swap(a[21],a[20]);

    if(b[0]==0) swap(b[0],b[3]);
    if(b[2]==0) swap(b[2],b[3]);
    if(b[1]==0) swap(b[1],b[6]);
    if(b[7]==0) swap(b[7],b[6]);
    if(b[16]==0) swap(b[16],b[17]);
    if(b[22]==0) swap(b[22],b[17]);
    if(b[23]==0) swap(b[23],b[20]);
    if(b[21]==0) swap(b[21],b[20]);

    for(int i=0;i<8;i++){
        if(a[spe[i]]!=b[spe[i]]) return false;
    }

    int posx=-1,posy=-1,nx=0,ny=0;
    vector <int> va,vb;
    for(int i=0;i<16;i++){
        int x=a[oth[i]],y=b[oth[i]];
        if(x==0) posx=i/4+1;
        else va.push_back(x);
        if(y==0) posy=i/4+1;
        else vb.push_back(y);
    }
    for(int i=0;i<va.size();i++)
        for(int j=i+1;j<va.size();j++)
            if(va[i]>va[j]) nx++;
    for(int i=0;i<vb.size();i++)
        for(int j=i+1;j<vb.size();j++)
            if(vb[i]>vb[j]) ny++;

    if(abs(posx-posy+nx-ny)&1) return false;
    else return true;
}

int main(){
    int T;
    scanf("%d",&T);
    while(T-- >0){
        for(int i=0;i<24;i++) scanf("%d",&a[i]);
        for(int i=0;i<24;i++) scanf("%d",&b[i]);
        if( solve() ) printf("N\n");
        else printf("Y\n");
    }
    return 0;
}






版权声明:欢迎关注我的博客,本文为博主toyking原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/toyking/p/4950994.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值