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

The
36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest

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

HDU 4021 24 Puzzle (拼图)

时间: 2024-11-10 11:38:08

HDU 4021 24 Puzzle (拼图)的相关文章

hdu 4662 MU Puzzle

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4662 MU PuzzleTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1296    Accepted Submission(s): 539 Problem Description Suppose there are the symbols

HDU 4021 八数码问题

24 Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 1466    Accepted Submission(s): 433 Problem Description Daniel likes to play a special board game, called 24 puzzle. 24 puzzle is such a

HDU 1538 A Puzzle for Pirates (海盗分金问题)

A Puzzle for Pirates Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 508    Accepted Submission(s): 167 Problem Description A bunch of pirates have gotten their hands on a hoard of gold pieces a

Java GUI 基础 Eight Puzzle (拼图游戏)

很简单使用java GUI 制作一个简单的拼图游戏 // main package HW1; import java.io.IOException; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class HW1 extends JFrame{ /** * */ public HW1_0586(String s

HDU 4662 MU Puzzle:找规律

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4662 题意: 初始字符串为"MI". 有三个操作: (1)将'M'之后的所有字符翻倍.For example: MIU to MIUIU. (2)将'III'变为一个'U'.For example: MUIIIU to MUUU. (3)删除'UU'.For example: MUUU to MU 给你一个字符串s,问你是否能将初始字符串"MI"通过一系列操作变为s.

[CareerCup] 8.6 Jigsaw Puzzle 拼图游戏

8.6 Implement a jigsaw puzzle. Design the data structures and explain an algorithm to solve the puzzle. You can assume that you have a f itsWith method which, when passed two puzzle pieces, returns true if the two pieces belong together. 这道题让我们设计一个拼图

hdu 4021 n数码

好题,6666 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2652410.html 题意:给出一个board,上面有24个位置,其中23个位置上放置了标有数字1~23的方块,一个为空位(用数字0表示),现在可以把空位与它旁边的方块交换,给出board的起始状态,问是否可以达到指定的状态. 思路:看起来很像著名的“八数码”问题,首先,针对八个特殊位置(死角),如果这里有空位就把它和相邻的位置交换,这样之后如果两个状态的对应死角上的数

人生第一个快速幂的题(HDU - 1097--A hard puzzle )

题意: 最简单的快速幂.给你两个数n和m,求n^m的最后一位: 解题思路: 额,快速幂就很简单了,这里只要最后一位可以一对每次运算都%10: 代码: #include<cstdio> #include<cstring> #include<algorithm> #include <iostream> using namespace std; #define N 100000 long long n,m; long long pow_(long long n,l

HDU 1538 A Puzzle for Pirates 经典海盗分金币

题目:这是一个经典问题,有n个海盗,分m块金子,其中他们会按一定的顺序提出自己的分配方案,如果50%以上的人赞成,则方案通过,开始分金子,如果不通过,则把提出方案的扔到海里,下一个人继续. 首先我们讲一下海盗分金决策的三个标准:保命,拿更多的金子,杀人,优先级是递减的. 同时分为两个状态稳定状态和不稳定状态:如果当n和m的组合使得最先决策的人(编号为n)不会被丢下海, 即游戏会立即结束, 就称这个状态时"稳定的". 反之, 问题会退化为n-1和m的组合, 直到达到一个稳定状态, 所以乘