HDOJ 6620 Just an Old Puzzle

题目链接
Problem Description
You are given a 4 × 4 grid, which consists of 15 number cells and an empty cell.
All numbers are unique and ranged from 1 to 15.
In this board, the cells which are adjacent with the empty cell can move to the empty cell.
Your task is to make the input grid to the target grid shown in the figure below.
In the following example (sample input), you can get the target grid in two moves.

HDOJ 6620 Just an Old Puzzle_第1张图片

Input
The first line contains an integer T (1 <= T <= 10^5) denoting the number of test cases.
Each test case consists of four lines each containing four space-separated integers, denoting the input grid. 0 indicates the empty cell.

Output
For each test case, you have to print the answer in one line.
If you can’t get the target grid within 120 moves, then print ‘No’, else print ‘Yes’.

Sample Input
2
1 2 3 4
5 6 7 8
9 10 0 12
13 14 11 15
1 2 3 4
5 6 7 8
9 10 11 12
13 15 14 0

Sample Output
Yes
No

  • 题意
    给你个4*4的数字华容道,120步内是否有解
  • 思路
    为什么这么多人知道数字华容道有解的条件啊
    HDOJ 6620 Just an Old Puzzle_第2张图片
    原网站地址:https://www.jianshu.com/p/1c1849d876b2
    (支持原创)
    解决了有解的问题,接下来就是120步内能不能解的问题,答案是有的,据说4*4最复杂的也能在80步内解出来
    源自wiki
  • 代码
#include
#define ll long long
using namespace std;
int t;
int a[16];
int gett(){//判断逆序数的奇偶
    int num=0;
    for(int i=1;i<16;i++){
        if(a[i]==0) continue;
        for(int j=0;j<i;j++){
            if(a[j]==0) continue;
            if(a[j]>a[i]) num++;
        }
    }
    return num&1;
}
int main(){
    //freopen("D:/compare/14157.in","r",stdin);
    //freopen("D:/compare/oj.txt","w",stdout);
    scanf("%d",&t);
    while(t--){
        int as=16;
        for(int i=0;i<16;i++){
            scanf("%d",&a[i]);
            if(a[i]==0){
                as=i;
            }
        }
        if(gett() ==(3-as/4)%2 ){
            puts("Yes");
        }else{
            puts("No");
        }
    }
}

你可能感兴趣的:(刷题记录)