J
u
s
t
a
n
O
l
d
P
u
z
z
l
e
\boldsymbol{Just an Old Puzzle}
JustanOldPuzzle
P
r
o
b
l
e
m
D
e
s
c
r
i
p
t
i
o
n
{\color{blue}Problem Description}
ProblemDescription
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.
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的矩阵数字分分布情况,问能否通过移动白块(用0表示)在129步之内到达 target grid的局面
分析:
哎,不说辽,直接上结论吧 ヽ(ー_ー)ノ
拼图游戏的数学原理
tcl
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
typedef long long ll;
using namespace std;
int a[100];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int t;
scanf("%d",&t);
while(t--){
int nowx,nowy,disx,disy;
disy=disx=3;
for(int i=0;i<16;i++){
scanf("%d",&a[i]);
if(a[i]==0)
nowx=i/4,nowy=i%4;
}
int sum=0;
for(int i=0;i<15;i++)
for(int j=i+1;j<=15;j++)
if(a[i]>a[j]) sum++;
if((sum+nowx+nowy)%2==(disx+disy+1)%2)//15与0存在一个逆序对
printf("Yes\n");
else
printf("No\n");
}
return 0;
}