HDU 4021 24 Puzzle

24 Puzzle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1012    Accepted Submission(s): 304


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
 
 
 

题意:给出一个board,上面有24个位置,其中23个位置上放置了标有数字1~23的方块,一个为空位(用数字0表示),现在可以把空位与它旁边的方块交换,给出board的起始状态,问是否可以达到指定的状态。

思路:看起来很像著名的“八数码”问题,首先,针对八个特殊位置(死角),如果这里有空位就把它和相邻的位置交换,这样之后如果两个状态的对应死角上的数字不同,那么显然是不能达到指定状态的,因为无法把死角处的数字换出去。

搞定了死角后就只剩下4×4的board了,这就变成了八数码问题的拓展——15数码。首先想想八数码是如何判断有解的:首先把所有数字(不包括空位的0)写成一行,就得到了一个1~8的排列,考虑空位的交换情况:1.左右交换,2.上下交换。对于左右交换而言,是不会改变写出的排列的逆序数的;而对上下交换,相当于在排列中向前或向后跳了两个位置,那么要么两个数都比它大或小,这样逆序数加2或减2,要么两个数一个比它大一个比它小,这样逆序数不变,综上,对于八数码问题,操作不会改变逆序数的奇偶性,所以只有初始状态和指定状态的逆序数奇偶性相同就有解。

弄清楚了八数码,扩展起来就容易了,现在我们将其扩展到N维(即N*N的board,N*N-1数码问题)。

首先无论N的奇偶,左右交换不改变逆序数,N为奇数时:上下交换逆序数增加N-1或减少N-1或不变,因为N为奇数,所以逆序数奇偶性不变;而N为偶数时:上下交换一次奇偶性改变一次。

结论:N为奇数时,初始状态与指定状态逆序数奇偶性相同即有解;N为偶数时,先计算出从初始状态到指定状态,空位要移动的行数m,如果初始状态的逆序数加上m与指定状态的逆序数奇偶性相同,则有解。

好了,现在这道题就简单了,计算逆序数和空格要移动的行数即可。

 

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[24];
int b[24];
int c[24];
int d[24];
int main()
{
   // freopen("A.in","r",stdin);
    //freopen("A.out","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=0;i<24;i++)
           scanf("%d",&a[i]);
        for(int i=0;i<24;i++)
           scanf("%d",&b[i]);

        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[20],a[21]);
        if(a[22]==0)swap(a[17],a[22]);
        if(a[23]==0)swap(a[20],a[23]);

        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[20],b[21]);
        if(b[22]==0)swap(b[17],b[22]);
        if(b[23]==0)swap(b[20],b[23]);
        bool flag=true;
        if(a[0]!=b[0])flag=false;
        if(a[1]!=b[1])flag=false;
        if(a[2]!=b[2])flag=false;
        if(a[7]!=b[7])flag=false;
        if(a[16]!=b[16])flag=false;
        if(a[21]!=b[21])flag=false;
        if(a[22]!=b[22])flag=false;
        if(a[23]!=b[23])flag=false;
        if(flag==false)
        {
            printf("Y\n");
            continue;
        }
        for(int i=0;i<4;i++)
        {
            c[i]=a[i+3];
            d[i]=b[i+3];
        }
        for(int i=4;i<12;i++)
        {
            c[i]=a[i+4];
            d[i]=b[i+4];
        }
        for(int i=12;i<16;i++)
        {
            c[i]=a[i+5];
            d[i]=b[i+5];
        }
        int cnt1=0;
        int cnt2=0;
        int m1=0,m2=0;
        for(int i=1;i<16;i++)
        {
            if(c[i]==0)
            {
                m1=i;
                continue;
            }
            for(int j=0;j<i;j++)
            {
                if(c[i]<c[j])cnt1++;
            }
        }
        for(int i=1;i<16;i++)
        {
            if(d[i]==0)
            {
                m2=i;
                continue;
            }
            for(int j=0;j<i;j++)
            {
                if(d[i]<d[j])cnt2++;
            }
        }
        m1/=4;//行数
        m2/=4;//行数
        int m=abs(m1-m2);

        if(((cnt1+m)%2)!=(cnt2%2))flag=false;
        if(flag)printf("N\n");
        else printf("Y\n");
    }
    return 0;
}

 

posted on 2012-08-23 14:29  kuangbin  阅读(786)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: