**
Just an Old Puzzle
**
题目描述
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.
输入
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.
输出
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’.
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
样例输出
Yes
No
数字华容道有着三个结论,可惜我不会推。。
作为一个菜鸡就直接把结论贴出来
1.若格子列数为奇数,则逆序数必须为偶数;
2.若格子列数为偶数,且逆序数为偶数,则当前空格所在行数与初始空格所在行数的差为偶数;
3.若格子列数为偶数,且逆序数为奇数,则当前空格所在行数与初始空格所在行数的差为奇数。
这是参考了https://blog.csdn.net/weixin_43164778/article/details/98171892,感谢作者,接下来贴上我自己写的代码。
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
const int N=16;
int a[N+5];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int x,y,ans=0;
for(int i=1;i<=16;i++)
{
scanf("%d",&a[i]);
if(a[i]==0) x=(i+3)/4,y=i%4+4*(i%4==0);
else
{
for(int j=i;j>0;j--)
{
if(a[j]==0) continue;
if(a[j]>a[i]) ans++;
}
}
}
if((4-x)%2==0)
{
if(ans%2==0) printf("Yes\n");
else printf("No\n");
}
else
{
if(ans%2==1) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}