首页 > 代码库 > HDU 4021 24 Puzzle (拼图)
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.
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.
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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。