码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4021 24 Puzzle (拼图)

时间:2014-08-15 19:40:39      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   for   

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:
bubuko.com,布布扣

  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 
 


题目大意:

bubuko.com,布布扣

bubuko.com,布布扣

给定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 (拼图),布布扣,bubuko.com

HDU 4021 24 Puzzle (拼图)

标签:des   style   blog   http   color   os   io   for   

原文地址:http://blog.csdn.net/a1061747415/article/details/38589063

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!